@@ -19,67 +19,34 @@ 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 {
22
+ public final class LambdaRuntime < Handler : ByteBufferLambdaHandler > {
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 handlerType : any ByteBufferLambdaHandler . Type
29
-
30
28
private var state = State . idle {
31
29
willSet {
32
30
self . eventLoop. assertInEventLoop ( )
33
31
precondition ( newValue. order > self . state. order, " invalid state \( newValue) after \( self . state. order) " )
34
32
}
35
33
}
36
34
37
- /// Create a new `LambdaRuntime`.
38
- ///
39
- /// - parameters:
40
- /// - handlerType: The ``SimpleLambdaHandler`` type the `LambdaRuntime` shall create and manage.
41
- /// - eventLoop: An `EventLoop` to run the Lambda on.
42
- /// - logger: A `Logger` to log the Lambda events.
43
- public convenience init < Handler: SimpleLambdaHandler > ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
44
- self . init ( CodableSimpleLambdaHandler< Handler> . self , eventLoop: eventLoop, logger: logger)
45
- }
46
-
47
- /// Create a new `LambdaRuntime`.
48
- ///
49
- /// - parameters:
50
- /// - handlerType: The ``LambdaHandler`` type the `LambdaRuntime` shall create and manage.
51
- /// - eventLoop: An `EventLoop` to run the Lambda on.
52
- /// - logger: A `Logger` to log the Lambda events.
53
- public convenience init < Handler: LambdaHandler > ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
54
- self . init ( CodableLambdaHandler< Handler> . self , eventLoop: eventLoop, logger: logger)
55
- }
56
-
57
- /// Create a new `LambdaRuntime`.
58
- ///
59
- /// - parameters:
60
- /// - handlerType: The ``EventLoopLambdaHandler`` type the `LambdaRuntime` shall create and manage.
61
- /// - eventLoop: An `EventLoop` to run the Lambda on.
62
- /// - logger: A `Logger` to log the Lambda events.
63
- public convenience init < Handler: EventLoopLambdaHandler > ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
64
- self . init ( CodableEventLoopLambdaHandler< Handler> . self , eventLoop: eventLoop, logger: logger)
65
- }
66
-
67
35
/// Create a new `LambdaRuntime`.
68
36
///
69
37
/// - parameters:
70
38
/// - handlerType: The ``ByteBufferLambdaHandler`` type the `LambdaRuntime` shall create and manage.
71
39
/// - eventLoop: An `EventLoop` to run the Lambda on.
72
40
/// - logger: A `Logger` to log the Lambda events.
73
- public convenience init ( _ handlerType: ( some ByteBufferLambdaHandler ) . Type, eventLoop: EventLoop , logger: Logger ) {
41
+ public convenience init ( _ handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger ) {
74
42
self . init ( handlerType: handlerType, eventLoop: eventLoop, logger: logger, configuration: . init( ) )
75
43
}
76
44
77
- init ( handlerType: ( some ByteBufferLambdaHandler ) . Type, eventLoop: EventLoop , logger: Logger , configuration: LambdaConfiguration ) {
45
+ init ( handlerType: Handler . Type , eventLoop: EventLoop , logger: Logger , configuration: LambdaConfiguration ) {
78
46
self . eventLoop = eventLoop
79
47
self . shutdownPromise = eventLoop. makePromise ( of: Int . self)
80
48
self . logger = logger
81
49
self . configuration = configuration
82
- self . handlerType = handlerType
83
50
}
84
51
85
52
deinit {
@@ -118,7 +85,7 @@ public final class LambdaRuntime {
118
85
let terminator = LambdaTerminator ( )
119
86
let runner = LambdaRunner ( eventLoop: self . eventLoop, configuration: self . configuration)
120
87
121
- let startupFuture = runner. initialize ( handlerType: self . handlerType , logger: logger, terminator: terminator)
88
+ let startupFuture = runner. initialize ( handlerType: Handler . self , logger: logger, terminator: terminator)
122
89
startupFuture. flatMap { handler -> EventLoopFuture < Result < Int , Error > > in
123
90
// after the startup future has succeeded, we have a handler that we can use
124
91
// to `run` the lambda.
@@ -229,5 +196,40 @@ public final class LambdaRuntime {
229
196
}
230
197
}
231
198
199
+ public enum LambdaRuntimeFactory {
200
+ /// Create a new `LambdaRuntime`.
201
+ ///
202
+ /// - parameters:
203
+ /// - handlerType: The ``SimpleLambdaHandler`` type the `LambdaRuntime` shall create and manage.
204
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
205
+ /// - logger: A `Logger` to log the Lambda events.
206
+ @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)
209
+ }
210
+
211
+ /// Create a new `LambdaRuntime`.
212
+ ///
213
+ /// - parameters:
214
+ /// - handlerType: The ``LambdaHandler`` type the `LambdaRuntime` shall create and manage.
215
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
216
+ /// - logger: A `Logger` to log the Lambda events.
217
+ @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)
220
+ }
221
+
222
+ /// Create a new `LambdaRuntime`.
223
+ ///
224
+ /// - parameters:
225
+ /// - handlerType: The ``EventLoopLambdaHandler`` type the `LambdaRuntime` shall create and manage.
226
+ /// - eventLoop: An `EventLoop` to run the Lambda on.
227
+ /// - logger: A `Logger` to log the Lambda events.
228
+ @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)
231
+ }
232
+ }
233
+
232
234
/// This is safe since lambda runtime synchronizes by dispatching all methods to a single `EventLoop`
233
235
extension LambdaRuntime : @unchecked Sendable { }
0 commit comments