@@ -18,7 +18,13 @@ import NIOCore
18
18
// MARK: - LambdaHandler
19
19
20
20
#if compiler(>=5.5) && canImport(_Concurrency)
21
- /// Strongly typed, processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` async.
21
+ /// Strongly typed, processing protocol for a Lambda that takes a user defined
22
+ /// ``EventLoopLambdaHandler/Event`` and returns a user defined
23
+ /// ``EventLoopLambdaHandler/Output`` asynchronously.
24
+ ///
25
+ /// - note: Most users should implement this protocol instead of the lower
26
+ /// level protocols ``EventLoopLambdaHandler`` and
27
+ /// ``ByteBufferLambdaHandler``.
22
28
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
23
29
public protocol LambdaHandler : EventLoopLambdaHandler {
24
30
/// The Lambda initialization method
@@ -42,6 +48,14 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
42
48
43
49
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
44
50
extension LambdaHandler {
51
+ public static func makeHandler( context: Lambda . InitializationContext ) -> EventLoopFuture < Self > {
52
+ let promise = context. eventLoop. makePromise ( of: Self . self)
53
+ promise. completeWithTask {
54
+ try await Self ( context: context)
55
+ }
56
+ return promise. futureResult
57
+ }
58
+
45
59
public func handle( _ event: Event , context: LambdaContext ) -> EventLoopFuture < Output > {
46
60
let promise = context. eventLoop. makePromise ( of: Output . self)
47
61
promise. completeWithTask {
@@ -51,25 +65,30 @@ extension LambdaHandler {
51
65
}
52
66
}
53
67
54
- @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
55
- extension LambdaHandler {
56
- public static func main( ) {
57
- _ = Lambda . run ( handlerType: Self . self)
58
- }
59
- }
60
68
#endif
61
69
62
70
// MARK: - EventLoopLambdaHandler
63
71
64
- /// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` asynchronously.
65
- /// `EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `Event` decoding and ` Output` -> `ByteBuffer` encoding .
72
+ /// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user
73
+ /// defined ``Event`` and returns a user defined `` Output`` asynchronously .
66
74
///
67
- /// - note: To implement a Lambda, implement either `LambdaHandler` or the `EventLoopLambdaHandler` protocol.
68
- /// The `LambdaHandler` will offload the Lambda execution to a `DispatchQueue` making processing safer but slower
69
- /// The `EventLoopLambdaHandler` will execute the Lambda on the same `EventLoop` as the core runtime engine, making the processing faster but requires
70
- /// more care from the implementation to never block the `EventLoop`.
75
+ /// ``EventLoopLambdaHandler`` extends ``ByteBufferLambdaHandler``, performing
76
+ /// `ByteBuffer` -> ``Event`` decoding and ``Output`` -> `ByteBuffer` encoding.
77
+ ///
78
+ /// - note: To implement a Lambda, implement either ``LambdaHandler`` or the
79
+ /// ``EventLoopLambdaHandler`` protocol. The ``LambdaHandler`` will offload
80
+ /// the Lambda execution to an async Task making processing safer but slower (due to
81
+ /// fewer thread hops).
82
+ /// The ``EventLoopLambdaHandler`` will execute the Lambda on the same `EventLoop`
83
+ /// as the core runtime engine, making the processing faster but requires more care from the
84
+ /// implementation to never block the `EventLoop`. Implement this protocol only in performance
85
+ /// critical situations and implement ``LambdaHandler`` in all other circumstances.
71
86
public protocol EventLoopLambdaHandler : ByteBufferLambdaHandler {
87
+ /// The lambda functions input. In most cases this should be Codable. If your event originates from an
88
+ /// AWS service, have a look at [AWSLambdaEvents](https://github.com/swift-server/swift-aws-lambda-events),
89
+ /// which provides a number of commonly used AWS Event implementations.
72
90
associatedtype Event
91
+ /// The lambda functions output. Can be `Void`.
73
92
associatedtype Output
74
93
75
94
/// The Lambda handling method
@@ -135,9 +154,18 @@ extension EventLoopLambdaHandler where Output == Void {
135
154
136
155
/// An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns a `ByteBuffer?` asynchronously.
137
156
///
138
- /// - note: This is a low level protocol designed to power the higher level `EventLoopLambdaHandler` and `LambdaHandler` based APIs.
157
+ /// - note: This is a low level protocol designed to power the higher level ``EventLoopLambdaHandler`` and
158
+ /// ``LambdaHandler`` based APIs.
139
159
/// Most users are not expected to use this protocol.
140
160
public protocol ByteBufferLambdaHandler {
161
+ /// Create your Lambda handler for the runtime.
162
+ ///
163
+ /// Use this to initialize all your resources that you want to cache between invocations. This could be database
164
+ /// connections and HTTP clients for example. It is encouraged to use the given `EventLoop`'s conformance
165
+ /// to `EventLoopGroup` when initializing NIO dependencies. This will improve overall performance, as it
166
+ /// minimizes thread hopping.
167
+ static func makeHandler( context: Lambda . InitializationContext ) -> EventLoopFuture < Self >
168
+
141
169
/// The Lambda handling method
142
170
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
143
171
///
@@ -163,6 +191,20 @@ extension ByteBufferLambdaHandler {
163
191
}
164
192
}
165
193
194
+ extension ByteBufferLambdaHandler {
195
+ /// Initializes and runs the lambda function.
196
+ ///
197
+ /// If you precede your ``ByteBufferLambdaHandler`` conformer's declaration with the
198
+ /// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
199
+ /// attribute, the system calls the conformer's `main()` method to launch the lambda function.
200
+ ///
201
+ /// The lambda runtime provides a default implementation of the method that manages the launch
202
+ /// process.
203
+ public static func main( ) {
204
+ _ = Lambda . run ( configuration: . init( ) , handlerType: Self . self)
205
+ }
206
+ }
207
+
166
208
@usableFromInline
167
209
enum CodecError : Error {
168
210
case requestDecoding( Error )
0 commit comments