@@ -25,6 +25,8 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
2525 private let logger : Logger
2626 private let configuration : LambdaConfiguration
2727
28+ private let handlerProvider : ( LambdaInitializationContext ) -> EventLoopFuture < Handler >
29+
2830 private var state = State . idle {
2931 willSet {
3032 self . eventLoop. assertInEventLoop ( )
@@ -39,14 +41,40 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
3941 /// - eventLoop: An `EventLoop` to run the Lambda on.
4042 /// - logger: A `Logger` to log the Lambda events.
4143 public convenience init ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
42- self . init ( handlerType : handlerType, eventLoop: eventLoop, logger: logger, configuration: . init( ) )
44+ self . init ( handlerProvider : handlerType. makeHandler ( context : ) , eventLoop: eventLoop, logger: logger, configuration: . init( ) )
4345 }
4446
45- init ( handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger , configuration: LambdaConfiguration ) {
47+ /// Create a new `LambdaRuntime`.
48+ ///
49+ /// - parameters:
50+ /// - handlerProvider: A provider of the ``ByteBufferLambdaHandler`` the `LambdaRuntime` will manage.
51+ /// - eventLoop: An `EventLoop` to run the Lambda on.
52+ /// - logger: A `Logger` to log the Lambda events.
53+ public convenience init (
54+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < Handler > ,
55+ eventLoop: EventLoop ,
56+ logger: Logger
57+ ) {
58+ self . init (
59+ handlerProvider: handlerProvider,
60+ eventLoop: eventLoop,
61+ logger: logger,
62+ configuration: . init( )
63+ )
64+ }
65+
66+ init (
67+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < Handler > ,
68+ eventLoop: EventLoop ,
69+ logger: Logger ,
70+ configuration: LambdaConfiguration
71+ ) {
4672 self . eventLoop = eventLoop
4773 self . shutdownPromise = eventLoop. makePromise ( of: Int . self)
4874 self . logger = logger
4975 self . configuration = configuration
76+
77+ self . handlerProvider = handlerProvider
5078 }
5179
5280 deinit {
@@ -85,7 +113,7 @@ public final class LambdaRuntime<Handler: ByteBufferLambdaHandler> {
85113 let terminator = LambdaTerminator ( )
86114 let runner = LambdaRunner ( eventLoop: self . eventLoop, configuration: self . configuration)
87115
88- let startupFuture = runner. initialize ( handlerType : Handler . self, logger: logger, terminator: terminator)
116+ let startupFuture = runner. initialize ( handlerProvider : self . handlerProvider , logger: logger, terminator: terminator)
89117 startupFuture. flatMap { handler -> EventLoopFuture < Result < Int , Error > > in
90118 // after the startup future has succeeded, we have a handler that we can use
91119 // to `run` the lambda.
@@ -229,6 +257,75 @@ public enum LambdaRuntimeFactory {
229257 public static func makeRuntime< H: EventLoopLambdaHandler > ( _ handlerType: H . Type , eventLoop: any EventLoop , logger: Logger ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
230258 LambdaRuntime < CodableEventLoopLambdaHandler < H > > ( CodableEventLoopLambdaHandler< H> . self , eventLoop: eventLoop, logger: logger)
231259 }
260+
261+ /// Create a new `LambdaRuntime`.
262+ ///
263+ /// - parameters:
264+ /// - handlerProvider: A provider of the ``SimpleLambdaHandler`` the `LambdaRuntime` will manage.
265+ /// - eventLoop: An `EventLoop` to run the Lambda on.
266+ /// - logger: A `Logger` to log the Lambda events.
267+ @inlinable
268+ public static func makeRuntime< H: SimpleLambdaHandler > (
269+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < H > ,
270+ eventLoop: any EventLoop ,
271+ logger: Logger
272+ ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
273+ LambdaRuntime (
274+ handlerProvider: { context in
275+ handlerProvider ( context) . map {
276+ CodableSimpleLambdaHandler ( handler: $0, allocator: context. allocator)
277+ }
278+ } ,
279+ eventLoop: eventLoop,
280+ logger: logger
281+ )
282+ }
283+
284+ /// Create a new `LambdaRuntime`.
285+ ///
286+ /// - parameters:
287+ /// - handlerProvider: A provider of the ``LambdaHandler`` the `LambdaRuntime` will manage.
288+ /// - eventLoop: An `EventLoop` to run the Lambda on.
289+ /// - logger: A `Logger` to log the Lambda events.
290+ @inlinable
291+ public static func makeRuntime< H: LambdaHandler > (
292+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < H > ,
293+ eventLoop: any EventLoop ,
294+ logger: Logger
295+ ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
296+ LambdaRuntime (
297+ handlerProvider: { context in
298+ handlerProvider ( context) . map {
299+ CodableLambdaHandler ( handler: $0, allocator: context. allocator)
300+ }
301+ } ,
302+ eventLoop: eventLoop,
303+ logger: logger
304+ )
305+ }
306+
307+ /// Create a new `LambdaRuntime`.
308+ ///
309+ /// - parameters:
310+ /// - handlerProvider: A provider of the ``EventLoopLambdaHandler`` the `LambdaRuntime` will manage.
311+ /// - eventLoop: An `EventLoop` to run the Lambda on.
312+ /// - logger: A `Logger` to log the Lambda events.
313+ @inlinable
314+ public static func makeRuntime< H: EventLoopLambdaHandler > (
315+ handlerProvider: @escaping ( LambdaInitializationContext ) -> EventLoopFuture < H > ,
316+ eventLoop: any EventLoop ,
317+ logger: Logger
318+ ) -> LambdaRuntime < some ByteBufferLambdaHandler > {
319+ LambdaRuntime (
320+ handlerProvider: { context in
321+ handlerProvider ( context) . map {
322+ CodableEventLoopLambdaHandler ( handler: $0, allocator: context. allocator)
323+ }
324+ } ,
325+ eventLoop: eventLoop,
326+ logger: logger
327+ )
328+ }
232329}
233330
234331/// This is safe since lambda runtime synchronizes by dispatching all methods to a single `EventLoop`
0 commit comments