-
Notifications
You must be signed in to change notification settings - Fork 114
async/await support #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async/await support #186
Changes from 1 commit
6ad092b
71d7805
e3b6b7c
469e7f8
0578764
d6f9d9a
2b66aeb
325b7b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,69 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: LambdaHandler { | |
} | ||
} | ||
|
||
// MARK: - Async | ||
|
||
extension Lambda { | ||
|
||
/// An async Lambda Closure that takes a `In: Decodable` and returns an `Out: Encodable` | ||
public typealias CodableAsyncClosure<In: Decodable, Out: Encodable> = (Lambda.Context, In) async throws -> Out | ||
|
||
/// Run a Lambda defined by implementing the `CodableAsyncClosure` function. | ||
/// | ||
/// - parameters: | ||
/// - closure: `CodableAsyncClosure` based Lambda. | ||
/// | ||
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine. | ||
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping CodableAsyncClosure<In, Out>) { | ||
self.run(CodableAsyncWrapper(closure)) | ||
} | ||
|
||
/// An asynchronous Lambda Closure that takes a `In: Decodable` and returns nothing. | ||
public typealias CodableVoidAsyncClosure<In: Decodable> = (Lambda.Context, In) async throws -> () | ||
|
||
/// Run a Lambda defined by implementing the `CodableVoidAsyncClosure` function. | ||
/// | ||
/// - parameters: | ||
/// - closure: `CodableVoidAsyncClosure` based Lambda. | ||
/// | ||
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine. | ||
public static func run<In: Decodable>(_ closure: @escaping CodableVoidAsyncClosure<In>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overloading on closure arguments is probably not a good idea |
||
self.run(CodableVoidAsyncWrapper(closure)) | ||
} | ||
} | ||
|
||
internal struct CodableAsyncWrapper<In: Decodable, Out: Encodable>: AsyncLambdaHandler { | ||
typealias In = In | ||
typealias Out = Out | ||
|
||
private let closure: Lambda.CodableAsyncClosure<In, Out> | ||
|
||
init(_ closure: @escaping Lambda.CodableAsyncClosure<In, Out>) { | ||
self.closure = closure | ||
} | ||
|
||
func handle(context: Lambda.Context, event: In) async throws -> Out { | ||
try await self.closure(context, event) | ||
} | ||
} | ||
|
||
internal struct CodableVoidAsyncWrapper<In: Decodable>: AsyncLambdaHandler { | ||
typealias In = In | ||
typealias Out = Void | ||
|
||
private let closure: Lambda.CodableVoidAsyncClosure<In> | ||
|
||
init(_ closure: @escaping Lambda.CodableVoidAsyncClosure<In>) { | ||
self.closure = closure | ||
} | ||
|
||
func handle(context: Lambda.Context, event: In) async throws -> Void { | ||
try await self.closure(context, event) | ||
} | ||
} | ||
|
||
// MARK: - Codable support | ||
|
||
/// Implementation of a`ByteBuffer` to `In` decoding | ||
extension EventLoopLambdaHandler where In: Decodable { | ||
@inlinable | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,39 @@ extension LambdaHandler { | |
} | ||
} | ||
|
||
// MARK: - AsyncLambdaHandler | ||
|
||
/// Strongly typed, processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` async. | ||
public protocol AsyncLambdaHandler: EventLoopLambdaHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is. Those protocols build up on top of each other:
|
||
|
||
/// The Lambda handling method | ||
/// Concrete Lambda handlers implement this method to provide the Lambda functionality. | ||
/// | ||
/// - parameters: | ||
/// - context: Runtime `Context`. | ||
/// - event: Event of type `In` representing the event or request. | ||
/// | ||
/// - Returns: A Lambda result ot type `Out`. | ||
func handle(context: Lambda.Context, event: In) async throws -> Out | ||
} | ||
|
||
extension AsyncLambdaHandler { | ||
public func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out> { | ||
@asyncHandler func _run(context: Lambda.Context, event: In, promise: EventLoopPromise<Out>) { | ||
do { | ||
let result = try await handle(context: context, event: event) | ||
promise.succeed(result) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
} | ||
|
||
let promise = context.eventLoop.makePromise(of: Out.self) | ||
_run(context: context, event: event, promise: promise) | ||
return promise.futureResult | ||
} | ||
} | ||
|
||
// MARK: - EventLoopLambdaHandler | ||
|
||
/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` asynchronously. | ||
|
Uh oh!
There was an error while loading. Please reload this page.