Skip to content

Lambda factory as a protocol requirement. #244

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 5 commits into from
Jan 13, 2022

Conversation

fabianfett
Copy link
Member

As previously discussed...

We want to use the same lambda application lifecycle for all lambda implementations. Currently we support @main only for the LambdaHandler protocol. This PR brings the @main functionality to the lower level protocols.

Modifications:

  • Add a factory method requirement to the ByteBufferLambdaHandler protocol
  • Remove the factory callback (HandlerFactory) from the LambdaRuntime and Lambda
  • Make the LambdaRuntime generic over the LambdaHandler type
  • Adjust tests
  • Write better docu

Result:

  • The application lifecycle is the same for all protocol levels.

@fabianfett fabianfett requested a review from tomerd January 3, 2022 08:36
/// - parameters:
/// - factory: A `ByteBufferLambdaHandler` factory.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
Copy link
Contributor

Choose a reason for hiding this comment

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

update docs ^^

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

/// - factory: A `ByteBufferLambdaHandler` factory.
///
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
internal static func run<Handler: ByteBufferLambdaHandler>(configuration: Configuration = .init(), handlerType: Handler.Type) -> Result<Int, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

is handlerType used?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess needed for the generics?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It is there to make the user declare the Generic type. Otherwise the user would need to write:

Lambda.run<Handler>()

/// connections and HTTP clients for example. It is encouraged to use the given `EventLoop`'s conformance
/// to `EventLoopGroup` when initializing NIO dependencies. This will improve overall performance, as it
/// minimizes thread hopping.
static func factory(context: Lambda.InitializationContext) -> EventLoopFuture<Self>
Copy link
Contributor

@tomerd tomerd Jan 6, 2022

Choose a reason for hiding this comment

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

maybe makeHandler or makeInstance? factory is generally not popular term in Swift. cc @weissi for ideas

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, generally "make" is the word to use; makeHandler would work too.

/// The lambda runtime provides a default implementation of the method that manages the launch
/// process.
public static func main() {
_ = Lambda.run(configuration: .init(), handlerType: Self.self)
Copy link
Contributor

Choose a reason for hiding this comment

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

should we mark Lambda::run @discardableResult?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the Lambda.run method should not return anything. But I haven't made my mind up about this fully. Would like to tackle later.

Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

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

nice! some small suggestions

@fabianfett fabianfett merged commit d06d22c into swift-server:main Jan 13, 2022
@fabianfett fabianfett deleted the ff-lambda-factory branch January 13, 2022 18:10
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.

3 participants