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

async/await support #186

merged 8 commits into from
Apr 27, 2021

Conversation

fabianfett
Copy link
Member

@fabianfett fabianfett commented Feb 18, 2021

async/await is awesome! Let's support this in Lambda.

Modifications:

  • Add an AsyncLambdaHandler. Will be renamed to LambdaHandler as soon as we drop the current callback based LambdaHandler.
  • The default way to use an AsyncLambdaHandler is to use @main to execute it. Don't use Lambda.run for it. We wan't to remove Lambda.run for 1.0.

@pokryfka
Copy link
Contributor

pokryfka commented Feb 22, 2021

Node.js developers will feel at home :-)

But we would need swift-no support it to be useful inside Lambda handler implementations, no?

Seems to be in the pipeline :-)
apple/swift-nio#1701

@fabianfett
Copy link
Member Author

This might be interesting to us. Potentially replacing the LambdaHandlerFactory:

swiftlang/swift#35398

@fabianfett fabianfett force-pushed the async-await branch 4 times, most recently from d6b3b48 to 3f849a0 Compare April 1, 2021 10:11
@fabianfett fabianfett requested review from tomerd and weissi April 1, 2021 10:15
@fabianfett fabianfett marked this pull request as ready for review April 1, 2021 10:16
@fabianfett fabianfett changed the title RFC: async/await support async/await support Apr 1, 2021
Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Awesome, this looks great. Left a few comments, mostly minor around overloading / @asyncHandler etc

/// - 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


#if compiler(>=5.5) && $AsyncAwait
/// 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.

@fabianfett fabianfett force-pushed the async-await branch 3 times, most recently from 2b6292d to b9e39fe Compare April 1, 2021 12:42
Package.swift Outdated

#if compiler(>=5.5) && $AsyncAwait
package.targets.first(where: { $0.name == "AWSLambdaRuntimeCore" })?
.dependencies.append(.product(name: "_NIOConcurrency", package: "swift-nio"))
Copy link
Contributor

Choose a reason for hiding this comment

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

you can always depend on it, _NIOConcurrency just doesn't export anything if A/A isn't on. But up to you

@@ -78,6 +78,64 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: LambdaHandler {
}
}

// MARK: - Async

#if compiler(>=5.5) && $AsyncAwait
Copy link
Contributor

@tomerd tomerd Apr 1, 2021

Choose a reason for hiding this comment

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

this is good stuff. should we also mark the closure ones deprecated >= 5.5? I would argue we should.

@tomerd tomerd added this to the 1.0.0 milestone Apr 6, 2021
Package.swift Outdated
@@ -15,7 +15,7 @@ let package = Package(
.library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.17.0")),
.package(url: "https://github.com/apple/swift-nio.git", .branch("main")),
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to wait for the next NIO release before we can merge. @weissi ;)

@fabianfett fabianfett force-pushed the async-await branch 2 times, most recently from 66b8fcc to 9e32a33 Compare April 17, 2021 20:27
@fabianfett
Copy link
Member Author

@swift-server-bot test this please

Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

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

nice one! lgtm!

@tomerd
Copy link
Contributor

tomerd commented Apr 27, 2021

awesome!

@fabianfett fabianfett merged commit 19fc295 into main Apr 27, 2021
@fabianfett fabianfett deleted the async-await branch April 27, 2021 20:47
@tomerd tomerd modified the milestones: 1.0.0-alpha.1, 0.5.0 Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants