Skip to content

Commit 39f2241

Browse files
committed
comments
1 parent b89f220 commit 39f2241

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,19 @@ extension Lambda {
3737
// 1. create the handler from the factory
3838
// 2. report initialization error if one occured
3939
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+
}
4552
}
46-
}
4753
}
4854

4955
func run(logger: Logger, handler: Handler) -> EventLoopFuture<Void> {
@@ -58,6 +64,10 @@ extension Lambda {
5864
let context = Context(logger: logger, eventLoop: self.eventLoop, invocation: invocation)
5965
logger.debug("sending invocation to lambda handler \(handler)")
6066
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.
6171
.hop(to: self.eventLoop)
6272
.mapResult { result in
6373
if case .failure(let error) = result {

0 commit comments

Comments
 (0)