@@ -21,12 +21,14 @@ extension Lambda {
21
21
internal final class Runner {
22
22
private let runtimeClient : RuntimeClient
23
23
private let eventLoop : EventLoop
24
+ private let allocator : ByteBufferAllocator
24
25
25
26
private var isGettingNextInvocation = false
26
27
27
28
init ( eventLoop: EventLoop , configuration: Configuration ) {
28
29
self . eventLoop = eventLoop
29
30
self . runtimeClient = RuntimeClient ( eventLoop: self . eventLoop, configuration: configuration. runtimeEngine)
31
+ self . allocator = ByteBufferAllocator ( )
30
32
}
31
33
32
34
/// Run the user provided initializer. This *must* only be called once.
@@ -36,13 +38,22 @@ extension Lambda {
36
38
logger. debug ( " initializing lambda " )
37
39
// 1. create the handler from the factory
38
40
// 2. report initialization error if one occured
39
- return factory ( self . eventLoop) . hop ( to: self . eventLoop) . peekError { error in
40
- self . runtimeClient. reportInitializationError ( logger: logger, error: error) . peekError { reportingError in
41
- // We're going to bail out because the init failed, so there's not a lot we can do other than log
42
- // that we couldn't report this error back to the runtime.
43
- logger. error ( " failed reporting initialization error to lambda runtime engine: \( reportingError) " )
41
+ let context = InitializationContext ( logger: logger,
42
+ eventLoop: self . eventLoop,
43
+ allocator: self . allocator)
44
+ return factory ( context)
45
+ // Hopping back to "our" EventLoop is importnant in case the factory returns a future
46
+ // that originated from a foreign EventLoop/EventLoopGroup.
47
+ // This can happen if the factory uses a library (let's say a database client) that manages its own threads/loops
48
+ // for whatever reason and returns a future that originated from that foreign EventLoop.
49
+ . hop ( to: self . eventLoop)
50
+ . peekError { error in
51
+ self . runtimeClient. reportInitializationError ( logger: logger, error: error) . peekError { reportingError in
52
+ // We're going to bail out because the init failed, so there's not a lot we can do other than log
53
+ // that we couldn't report this error back to the runtime.
54
+ logger. error ( " failed reporting initialization error to lambda runtime engine: \( reportingError) " )
55
+ }
44
56
}
45
- }
46
57
}
47
58
48
59
func run( logger: Logger , handler: Handler ) -> EventLoopFuture < Void > {
@@ -54,9 +65,17 @@ extension Lambda {
54
65
} . flatMap { invocation, event in
55
66
// 2. send invocation to handler
56
67
self . isGettingNextInvocation = false
57
- let context = Context ( logger: logger, eventLoop: self . eventLoop, invocation: invocation)
68
+ let context = Context ( logger: logger,
69
+ eventLoop: self . eventLoop,
70
+ allocator: self . allocator,
71
+ invocation: invocation)
58
72
logger. debug ( " sending invocation to lambda handler \( handler) " )
59
73
return handler. handle ( context: context, event: event)
74
+ // Hopping back to "our" EventLoop is importnant in case the handler returns a future that
75
+ // originiated from a foreign EventLoop/EventLoopGroup.
76
+ // This can happen if the handler uses a library (lets say a DB client) that manages its own threads/loops
77
+ // for whatever reason and returns a future that originated from that foreign EventLoop.
78
+ . hop ( to: self . eventLoop)
60
79
. mapResult { result in
61
80
if case . failure( let error) = result {
62
81
logger. warning ( " lambda handler returned an error: \( error) " )
@@ -82,15 +101,16 @@ extension Lambda {
82
101
}
83
102
84
103
private extension Lambda . Context {
85
- convenience init ( logger: Logger , eventLoop: EventLoop , invocation: Lambda . Invocation ) {
104
+ convenience init ( logger: Logger , eventLoop: EventLoop , allocator : ByteBufferAllocator , invocation: Lambda . Invocation ) {
86
105
self . init ( requestID: invocation. requestID,
87
106
traceID: invocation. traceID,
88
107
invokedFunctionARN: invocation. invokedFunctionARN,
89
108
deadline: DispatchWallTime ( millisSinceEpoch: invocation. deadlineInMillisSinceEpoch) ,
90
109
cognitoIdentity: invocation. cognitoIdentity,
91
110
clientContext: invocation. clientContext,
92
111
logger: logger,
93
- eventLoop: eventLoop)
112
+ eventLoop: eventLoop,
113
+ allocator: allocator)
94
114
}
95
115
}
96
116
0 commit comments