Skip to content

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

Merged
merged 8 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
async/await support
  • Loading branch information
fabianfett committed Apr 17, 2021
commit 6ad092b195e820bc3013feacd4c2681a6440f681
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ let package = Package(
.byName(name: "AWSLambdaRuntimeCore"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
]),
], swiftSettings: [.unsafeFlags(["-Xfrontend", "-enable-experimental-concurrency"])]),
.target(name: "AWSLambdaRuntimeCore", dependencies: [
.product(name: "Logging", package: "swift-log"),
.product(name: "Backtrace", package: "swift-backtrace"),
.product(name: "NIOHTTP1", package: "swift-nio"),
]),
], swiftSettings: [.unsafeFlags(["-Xfrontend", "-enable-experimental-concurrency"])]),
.testTarget(name: "AWSLambdaRuntimeCoreTests", dependencies: [
.byName(name: "AWSLambdaRuntimeCore"),
.product(name: "NIOTestUtils", package: "swift-nio"),
Expand Down
63 changes: 63 additions & 0 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
33 changes: 33 additions & 0 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, an AsyncLambdaHandler isn't running the async code on the EventLoop at the moment (that's impossible without custom executors), is this proto conformance still right?

Copy link
Member Author

@fabianfett fabianfett Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. Those protocols build up on top of each other:

  1. ByteBufferLambdaHandler (just receive ByteBuffer on the EnventLoop)
  2. EventLoopLambdaHandler (implements encoding and decoding to other types) - still on EventLoop
  3. AsyncLambdaHandler - implements EventLoopLambdaHandler by dispatching of the EventLoop.


/// 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.
Expand Down