@@ -41,20 +41,20 @@ public enum Lambda {
41
41
self . run ( handler: handler)
42
42
}
43
43
44
- /// Run a Lambda defined by implementing the `LambdaHandler` protocol.
44
+ /// Run a Lambda defined by implementing the `LambdaHandler` protocol via a `LambdaHandlerFactory` .
45
45
///
46
46
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
47
47
@inlinable
48
- public static func run( _ provider : @escaping LambdaHandlerProvider ) {
49
- self . run ( provider : provider )
48
+ public static func run( _ factory : @escaping LambdaHandlerFactory ) {
49
+ self . run ( factory : factory )
50
50
}
51
51
52
- /// Run a Lambda defined by implementing the `LambdaHandler` protocol.
52
+ /// Run a Lambda defined by implementing the `LambdaHandler` protocol via a factory .
53
53
///
54
54
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
55
55
@inlinable
56
- public static func run( _ provider : @escaping ( EventLoop ) throws -> LambdaHandler ) {
57
- self . run ( provider : provider )
56
+ public static func run( _ factory : @escaping ( EventLoop ) throws -> LambdaHandler ) {
57
+ self . run ( factory : factory )
58
58
}
59
59
60
60
// for testing and internal use
@@ -68,16 +68,16 @@ public enum Lambda {
68
68
@usableFromInline
69
69
@discardableResult
70
70
internal static func run( configuration: Configuration = . init( ) , handler: LambdaHandler ) -> LambdaLifecycleResult {
71
- return self . run ( configuration: configuration, provider : { _, callback in callback ( . success( handler) ) } )
71
+ return self . run ( configuration: configuration, factory : { _, callback in callback ( . success( handler) ) } )
72
72
}
73
73
74
74
// for testing and internal use
75
75
@usableFromInline
76
76
@discardableResult
77
- internal static func run( configuration: Configuration = . init( ) , provider : @escaping ( EventLoop ) throws -> LambdaHandler ) -> LambdaLifecycleResult {
78
- self . run ( provider : { ( eventloop: EventLoop , callback: ( Result < LambdaHandler , Error > ) -> Void ) -> Void in
77
+ internal static func run( configuration: Configuration = . init( ) , factory : @escaping ( EventLoop ) throws -> LambdaHandler ) -> LambdaLifecycleResult {
78
+ self . run ( factory : { ( eventloop: EventLoop , callback: ( Result < LambdaHandler , Error > ) -> Void ) -> Void in
79
79
do {
80
- let handler = try provider ( eventloop)
80
+ let handler = try factory ( eventloop)
81
81
callback ( . success( handler) )
82
82
} catch {
83
83
callback ( . failure( error) )
@@ -88,22 +88,22 @@ public enum Lambda {
88
88
// for testing and internal use
89
89
@usableFromInline
90
90
@discardableResult
91
- internal static func run( configuration: Configuration = . init( ) , provider : @escaping LambdaHandlerProvider ) -> LambdaLifecycleResult {
91
+ internal static func run( configuration: Configuration = . init( ) , factory : @escaping LambdaHandlerFactory ) -> LambdaLifecycleResult {
92
92
do {
93
93
let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 ) // only need one thread, will improve performance
94
94
defer { try ! eventLoopGroup. syncShutdownGracefully ( ) }
95
- let result = try self . runAsync ( eventLoopGroup: eventLoopGroup, configuration: configuration, provider : provider ) . wait ( )
95
+ let result = try self . runAsync ( eventLoopGroup: eventLoopGroup, configuration: configuration, factory : factory ) . wait ( )
96
96
return . success( result)
97
97
} catch {
98
98
return . failure( error)
99
99
}
100
100
}
101
101
102
- internal static func runAsync( eventLoopGroup: EventLoopGroup , configuration: Configuration , provider : @escaping LambdaHandlerProvider ) -> EventLoopFuture < Int > {
102
+ internal static func runAsync( eventLoopGroup: EventLoopGroup , configuration: Configuration , factory : @escaping LambdaHandlerFactory ) -> EventLoopFuture < Int > {
103
103
Backtrace . install ( )
104
104
var logger = Logger ( label: " Lambda " )
105
105
logger. logLevel = configuration. general. logLevel
106
- let lifecycle = Lifecycle ( eventLoop: eventLoopGroup. next ( ) , logger: logger, configuration: configuration, provider : provider )
106
+ let lifecycle = Lifecycle ( eventLoop: eventLoopGroup. next ( ) , logger: logger, configuration: configuration, factory : factory )
107
107
let signalSource = trap ( signal: configuration. lifecycle. stopSignal) { signal in
108
108
logger. info ( " intercepted signal: \( signal) " )
109
109
lifecycle. stop ( )
@@ -150,16 +150,16 @@ public enum Lambda {
150
150
private let eventLoop : EventLoop
151
151
private let logger : Logger
152
152
private let configuration : Configuration
153
- private let provider : LambdaHandlerProvider
153
+ private let factory : LambdaHandlerFactory
154
154
155
155
private var _state = State . idle
156
156
private let stateLock = Lock ( )
157
157
158
- init ( eventLoop: EventLoop , logger: Logger , configuration: Configuration , provider : @escaping LambdaHandlerProvider ) {
158
+ init ( eventLoop: EventLoop , logger: Logger , configuration: Configuration , factory : @escaping LambdaHandlerFactory ) {
159
159
self . eventLoop = eventLoop
160
160
self . logger = logger
161
161
self . configuration = configuration
162
- self . provider = provider
162
+ self . factory = factory
163
163
}
164
164
165
165
deinit {
@@ -188,7 +188,7 @@ public enum Lambda {
188
188
var logger = self . logger
189
189
logger [ metadataKey: " lifecycleId " ] = . string( self . configuration. lifecycle. id)
190
190
let runner = LambdaRunner ( eventLoop: self . eventLoop, configuration: self . configuration)
191
- return runner. initialize ( logger: logger, provider : self . provider ) . flatMap { handler in
191
+ return runner. initialize ( logger: logger, factory : self . factory ) . flatMap { handler in
192
192
self . state = . active( runner, handler)
193
193
return self . run ( )
194
194
}
@@ -338,23 +338,19 @@ public enum Lambda {
338
338
}
339
339
}
340
340
341
- /// A result type for a Lambda that returns a `[UInt8]`.
342
341
public typealias LambdaResult = Result < [ UInt8 ] , Error >
343
342
344
343
public typealias LambdaCallback = ( LambdaResult ) -> Void
345
344
346
- /// A processing closure for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously.
345
+ /// A processing closure for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously via`LambdaCallback` .
347
346
public typealias LambdaClosure = ( Lambda . Context , [ UInt8 ] , LambdaCallback ) -> Void
348
347
349
- /// A result type for a Lambda initialization.
350
- public typealias LambdaInitResult = Result < LambdaHandler , Error >
351
-
352
348
/// A callback to provide the result of Lambda initialization.
353
- public typealias LambdaInitCallBack = ( LambdaInitResult ) -> Void
349
+ public typealias LambdaInitCallBack = ( Result < LambdaHandler , Error > ) -> Void
354
350
355
- public typealias LambdaHandlerProvider = ( EventLoop , LambdaInitCallBack ) -> Any
351
+ public typealias LambdaHandlerFactory = ( EventLoop , LambdaInitCallBack ) -> Void
356
352
357
- /// A processing protocol for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously.
353
+ /// A processing protocol for a Lambda that takes a `[UInt8]` and returns a `LambdaResult` result type asynchronously via `LambdaCallback` .
358
354
public protocol LambdaHandler {
359
355
/// Handles the Lambda request.
360
356
func handle( context: Lambda . Context , payload: [ UInt8 ] , callback: @escaping LambdaCallback )
0 commit comments