@@ -37,13 +37,19 @@ extension Lambda {
37
37
// 1. create the handler from the factory
38
38
// 2. report initialization error if one occured
39
39
let context = InitializationContext ( logger: logger, eventLoop: self . eventLoop)
40
- return factory ( context) . hop ( to: self . eventLoop) . peekError { error in
41
- self . runtimeClient. reportInitializationError ( logger: logger, error: error) . peekError { reportingError in
42
- // We're going to bail out because the init failed, so there's not a lot we can do other than log
43
- // that we couldn't report this error back to the runtime.
44
- logger. error ( " failed reporting initialization error to lambda runtime engine: \( reportingError) " )
40
+ return factory ( context)
41
+ // hopping back to "our" EventLoop is importnant in case the factory returns
42
+ // a future that originiated from a different EventLoop
43
+ // this can happen if the factory uses a library (lets say a DB client) that manages it's own EventLoops
44
+ // for whatever reason and returns a future that originated from that foreign EventLoop.
45
+ . hop ( to: self . eventLoop)
46
+ . peekError { error in
47
+ self . runtimeClient. reportInitializationError ( logger: logger, error: error) . peekError { reportingError in
48
+ // We're going to bail out because the init failed, so there's not a lot we can do other than log
49
+ // that we couldn't report this error back to the runtime.
50
+ logger. error ( " failed reporting initialization error to lambda runtime engine: \( reportingError) " )
51
+ }
45
52
}
46
- }
47
53
}
48
54
49
55
func run( logger: Logger , handler: Handler ) -> EventLoopFuture < Void > {
@@ -58,6 +64,10 @@ extension Lambda {
58
64
let context = Context ( logger: logger, eventLoop: self . eventLoop, invocation: invocation)
59
65
logger. debug ( " sending invocation to lambda handler \( handler) " )
60
66
return handler. handle ( context: context, event: event)
67
+ // hopping back to the "our" EventLoop is importnant in case the handler returns
68
+ // a future that originiated from a different EventLoop
69
+ // this can happen if the handler uses a library (lets say a DB client) that manages it's own EventLoops
70
+ // for whatever reason and returns a future that originated from that foreign EventLoop.
61
71
. hop ( to: self . eventLoop)
62
72
. mapResult { result in
63
73
if case . failure( let error) = result {
0 commit comments