@@ -19,12 +19,14 @@ import NIOCore
19
19
/// `LambdaRuntime` manages the Lambda process lifecycle.
20
20
///
21
21
/// Use this API, if you build a higher level web framework which shall be able to run inside the Lambda environment.
22
- public final class LambdaRuntime < Handler: ByteBufferLambdaHandler > {
22
+ public final class LambdaRuntime < Handler: LambdaRuntimeHandler > {
23
23
private let eventLoop : EventLoop
24
24
private let shutdownPromise : EventLoopPromise < Int >
25
25
private let logger : Logger
26
26
private let configuration : LambdaConfiguration
27
27
28
+ private let handlerProvider : ( LambdaInitializationContext ) -> EventLoopFuture < Handler >
29
+
28
30
private var state = State . idle {
29
31
willSet {
30
32
self . eventLoop. assertInEventLoop ( )
@@ -35,18 +37,41 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
35
37
/// Create a new `LambdaRuntime`.
36
38
///
37
39
/// - parameters:
38
- /// - handlerType: The ``ByteBufferLambdaHandler `` type the `LambdaRuntime` shall create and manage.
40
+ /// - handlerProvider: A provider of the ``Handler `` the `LambdaRuntime` will manage.
39
41
/// - eventLoop: An `EventLoop` to run the Lambda on.
40
42
/// - logger: A `Logger` to log the Lambda events.
41
- public convenience init ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
42
- self . init ( handlerType: handlerType, eventLoop: eventLoop, logger: logger, configuration: . init( ) )
43
+ @usableFromInline
44
+ convenience init (
45
+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < Handler > ,
46
+ eventLoop: EventLoop ,
47
+ logger: Logger
48
+ ) {
49
+ self . init (
50
+ handlerProvider: handlerProvider,
51
+ eventLoop: eventLoop,
52
+ logger: logger,
53
+ configuration: . init( )
54
+ )
43
55
}
44
56
45
- init ( handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger , configuration: LambdaConfiguration ) {
57
+ /// Create a new `LambdaRuntime`.
58
+ ///
59
+ /// - parameters:
60
+ /// - handlerProvider: A provider of the ``Handler`` the `LambdaRuntime` will manage.
61
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
62
+ /// - logger: A `Logger` to log the Lambda events.
63
+ init (
64
+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < Handler > ,
65
+ eventLoop: EventLoop ,
66
+ logger: Logger ,
67
+ configuration: LambdaConfiguration
68
+ ) {
46
69
self . eventLoop = eventLoop
47
70
self . shutdownPromise = eventLoop. makePromise ( of: Int . self)
48
71
self . logger = logger
49
72
self . configuration = configuration
73
+
74
+ self . handlerProvider = handlerProvider
50
75
}
51
76
52
77
deinit {
@@ -85,7 +110,7 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
85
110
let terminator = LambdaTerminator ( )
86
111
let runner = LambdaRunner ( eventLoop: self . eventLoop, configuration: self . configuration)
87
112
88
- let startupFuture = runner. initialize ( handlerType : Handler . self, logger: logger, terminator: terminator)
113
+ let startupFuture = runner. initialize ( handlerProvider : self . handlerProvider , logger: logger, terminator: terminator)
89
114
startupFuture. flatMap { handler -> EventLoopFuture < Result < Int , Error > > in
90
115
// after the startup future has succeeded, we have a handler that we can use
91
116
// to `run` the lambda.
@@ -175,7 +200,7 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
175
200
private enum State {
176
201
case idle
177
202
case initializing
178
- case active( LambdaRunner , any ByteBufferLambdaHandler )
203
+ case active( LambdaRunner , any LambdaRuntimeHandler )
179
204
case shuttingdown
180
205
case shutdown
181
206
@@ -204,8 +229,16 @@ public enum LambdaRuntimeFactory {
204
229
/// - eventLoop: An `EventLoop` to run the Lambda on.
205
230
/// - logger: A `Logger` to log the Lambda events.
206
231
@inlinable
207
- public static func makeRuntime< H: SimpleLambdaHandler > ( _ handlerType: H . Type , eventLoop: any EventLoop , logger: Logger ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
208
- LambdaRuntime < CodableSimpleLambdaHandler < H > > ( CodableSimpleLambdaHandler< H> . self , eventLoop: eventLoop, logger: logger)
232
+ public static func makeRuntime< Handler: SimpleLambdaHandler > (
233
+ _ handlerType: Handler . Type ,
234
+ eventLoop: any EventLoop ,
235
+ logger: Logger
236
+ ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
237
+ LambdaRuntime< CodableSimpleLambdaHandler< Handler>>(
238
+ handlerProvider: CodableSimpleLambdaHandler< Handler> . makeHandler( context: ) ,
239
+ eventLoop: eventLoop,
240
+ logger: logger
241
+ )
209
242
}
210
243
211
244
/// Create a new `LambdaRuntime`.
@@ -215,8 +248,16 @@ public enum LambdaRuntimeFactory {
215
248
/// - eventLoop: An `EventLoop` to run the Lambda on.
216
249
/// - logger: A `Logger` to log the Lambda events.
217
250
@inlinable
218
- public static func makeRuntime< H: LambdaHandler > ( _ handlerType: H . Type , eventLoop: any EventLoop , logger: Logger ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
219
- LambdaRuntime < CodableLambdaHandler < H > > ( CodableLambdaHandler< H> . self , eventLoop: eventLoop, logger: logger)
251
+ public static func makeRuntime< Handler: LambdaHandler > (
252
+ _ handlerType: Handler . Type ,
253
+ eventLoop: any EventLoop ,
254
+ logger: Logger
255
+ ) -> LambdaRuntime < some LambdaRuntimeHandler > {
256
+ LambdaRuntime< CodableLambdaHandler< Handler>>(
257
+ handlerProvider: CodableLambdaHandler< Handler> . makeHandler( context: ) ,
258
+ eventLoop: eventLoop,
259
+ logger: logger
260
+ )
220
261
}
221
262
222
263
/// Create a new `LambdaRuntime`.
@@ -226,8 +267,79 @@ public enum LambdaRuntimeFactory {
226
267
/// - eventLoop: An `EventLoop` to run the Lambda on.
227
268
/// - logger: A `Logger` to log the Lambda events.
228
269
@inlinable
229
- public static func makeRuntime< H: EventLoopLambdaHandler > ( _ handlerType: H . Type , eventLoop: any EventLoop , logger: Logger ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
230
- LambdaRuntime < CodableEventLoopLambdaHandler < H > > ( CodableEventLoopLambdaHandler< H> . self , eventLoop: eventLoop, logger: logger)
270
+ public static func makeRuntime< Handler: EventLoopLambdaHandler > (
271
+ _ handlerType: Handler . Type ,
272
+ eventLoop: any EventLoop ,
273
+ logger: Logger
274
+ ) -> LambdaRuntime < some LambdaRuntimeHandler > {
275
+ LambdaRuntime< CodableEventLoopLambdaHandler< Handler>>(
276
+ handlerProvider: CodableEventLoopLambdaHandler< Handler> . makeHandler( context: ) ,
277
+ eventLoop: eventLoop,
278
+ logger: logger
279
+ )
280
+ }
281
+
282
+ /// Create a new `LambdaRuntime`.
283
+ ///
284
+ /// - parameters:
285
+ /// - handlerType: The ``ByteBufferLambdaHandler`` type the `LambdaRuntime` shall create and manage.
286
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
287
+ /// - logger: A `Logger` to log the Lambda events.
288
+ @inlinable
289
+ public static func makeRuntime< Handler: ByteBufferLambdaHandler > (
290
+ _ handlerType: Handler . Type ,
291
+ eventLoop: any EventLoop ,
292
+ logger: Logger
293
+ ) -> LambdaRuntime < some LambdaRuntimeHandler > {
294
+ LambdaRuntime < Handler > (
295
+ handlerProvider: Handler . makeHandler ( context: ) ,
296
+ eventLoop: eventLoop,
297
+ logger: logger
298
+ )
299
+ }
300
+
301
+ /// Create a new `LambdaRuntime`.
302
+ ///
303
+ /// - parameters:
304
+ /// - handlerProvider: A provider of the ``Handler`` the `LambdaRuntime` will manage.
305
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
306
+ /// - logger: A `Logger` to log the Lambda events.
307
+ @inlinable
308
+ public static func makeRuntime< Handler: LambdaRuntimeHandler > (
309
+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < Handler > ,
310
+ eventLoop: any EventLoop ,
311
+ logger: Logger
312
+ ) -> LambdaRuntime < Handler > {
313
+ LambdaRuntime (
314
+ handlerProvider: handlerProvider,
315
+ eventLoop: eventLoop,
316
+ logger: logger
317
+ )
318
+ }
319
+
320
+ /// Create a new `LambdaRuntime`.
321
+ ///
322
+ /// - parameters:
323
+ /// - handlerProvider: A provider of the ``Handler`` the `LambdaRuntime` will manage.
324
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
325
+ /// - logger: A `Logger` to log the Lambda events.
326
+ @inlinable
327
+ public static func makeRuntime< Handler: LambdaRuntimeHandler > (
328
+ handlerProvider: @escaping ( LambdaInitializationContext ) async throws -> Handler ,
329
+ eventLoop: any EventLoop ,
330
+ logger: Logger
331
+ ) -> LambdaRuntime < Handler > {
332
+ LambdaRuntime (
333
+ handlerProvider: { context in
334
+ let promise = eventLoop. makePromise ( of: Handler . self)
335
+ promise. completeWithTask {
336
+ try await handlerProvider ( context)
337
+ }
338
+ return promise. futureResult
339
+ } ,
340
+ eventLoop: eventLoop,
341
+ logger: logger
342
+ )
231
343
}
232
344
}
233
345
0 commit comments