@@ -18,7 +18,13 @@ import NIOCore
1818// MARK: - LambdaHandler
1919
2020#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``.
2228@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
2329public protocol LambdaHandler : EventLoopLambdaHandler {
2430 /// The Lambda initialization method
@@ -42,6 +48,14 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
4248
4349@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
4450extension 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+
4559 public func handle( _ event: Event , context: LambdaContext ) -> EventLoopFuture < Output > {
4660 let promise = context. eventLoop. makePromise ( of: Output . self)
4761 promise. completeWithTask {
@@ -51,25 +65,30 @@ extension LambdaHandler {
5165 }
5266}
5367
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- }
6068#endif
6169
6270// MARK: - EventLoopLambdaHandler
6371
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 .
6674///
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.
7186public 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.
7290 associatedtype Event
91+ /// The lambda functions output. Can be `Void`.
7392 associatedtype Output
7493
7594 /// The Lambda handling method
@@ -135,9 +154,18 @@ extension EventLoopLambdaHandler where Output == Void {
135154
136155/// An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns a `ByteBuffer?` asynchronously.
137156///
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.
139159/// Most users are not expected to use this protocol.
140160public 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+
141169 /// The Lambda handling method
142170 /// Concrete Lambda handlers implement this method to provide the Lambda functionality.
143171 ///
@@ -163,6 +191,20 @@ extension ByteBufferLambdaHandler {
163191 }
164192}
165193
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+
166208@usableFromInline
167209enum CodecError : Error {
168210 case requestDecoding( Error )
0 commit comments