Skip to content
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

add initialization context #126

Merged
merged 7 commits into from
Jun 16, 2020
Merged

add initialization context #126

merged 7 commits into from
Jun 16, 2020

Conversation

tomerd
Copy link
Contributor

@tomerd tomerd commented Jun 12, 2020

motivation: in more complext initialization scearios you may want access to a logger or other utilities

changes:

  • introduce new InitializationContext type that can be extened in the future without breaking the API in semantic-major way
  • instead of passing in EventLoop to the handler factory, pass in a context that includes a Logger and and EventLoop
  • fix a bug where we dont hop back to the event loop when coming back from the handler
  • adjust tests to the new signature

motivation: in more complext initialization scearios you may want access to a logger or other utilities

changes:
* introduce new InitializationContext type that can be extened in the future without breaking the API in semantic-major way
* instead of passing in EventLoop to the handler factory, pass in a context that includes a Logger and and EventLoop
* fix a bug where we dont hope back to the event loop when coming back from the handler
* adjust tests to the new signature
@tomerd tomerd requested a review from fabianfett June 12, 2020 23:48
@@ -57,6 +58,7 @@ extension Lambda {
let context = Context(logger: logger, eventLoop: self.eventLoop, invocation: invocation)
logger.debug("sending invocation to lambda handler \(handler)")
return handler.handle(context: context, event: event)
.hop(to: self.eventLoop)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

👀

Copy link
Member

Choose a reason for hiding this comment

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

@tomerd The only situation in which we would need to hop is if developers bring their own EventLoopGroup. Is that something that is likely?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this can happen is if they use some library (lets say a DB client) that manages it's own ELG for whatever reason and they return a future that originated from that ELG. while this is not a pattern we encourage, we also can't stop anyone from doing it so to be safe we should hop imo

Copy link
Member

Choose a reason for hiding this comment

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

Vapor doesn't do a hop in its routes and quite a number of people hit that issue at some point. After that they know its an issue and don't do it again. There is an argument for education here instead of protecting against. But at the same time a hop onto the same EventLoop is hardly a performance hit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just as a reference: Where ever NIO takes a future (not that many APIs outside of bootstraps), it'll hop it to the correct event loop.

extension Lambda {
/// Lambda runtime initialization context.
/// The Lambda runtime generates and passes the `InitializationContext` to the Lambda handler as an argument.
public final class InitializationContext {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

also considered InitContext but this feels more correct since the function is called initialize

@tomerd tomerd requested review from ktoso and yim-lee June 12, 2020 23:49
@tomerd
Copy link
Contributor Author

tomerd commented Jun 12, 2020

cc @adam-fowler

@tomerd tomerd added the enhancement New feature or request label Jun 12, 2020
@tomerd tomerd added this to the 0.1.1 milestone Jun 12, 2020
Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

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

Thanks @tomerd for getting this out! The only thing I'm unsure about is if the hop is necessary.

@tomerd
Copy link
Contributor Author

tomerd commented Jun 12, 2020

@fabianfett one thing I considered was also exposing an allocator in the new context. do you think it would be a good addition or wait-and-see?

// hopping back to "our" EventLoop is importnant in case the factory returns
// a future that originiated from a different EventLoop
// this can happen if the factory uses a library (lets say a DB client) that manages it's own EventLoops
// for whatever reason and returns a future that originated from that foreign EventLoop.
Copy link
Contributor Author

@tomerd tomerd Jun 13, 2020

Choose a reason for hiding this comment

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

just added a comment (it was already hopping) the diff is mostly formatting

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/LambdaRunner.swift Outdated Show resolved Hide resolved
@fabianfett
Copy link
Member

fabianfett commented Jun 15, 2020

@fabianfett one thing I considered was also exposing an allocator in the new context. do you think it would be a good addition or wait-and-see?

@tomerd Yes. I think we should include it. If people need to make some json calls or whatever a allocator comes in handy.

tomerd and others added 3 commits June 15, 2020 17:12
Co-authored-by: Fabian Fett <fabianfett@mac.com>
Co-authored-by: Yim Lee <yim_lee@apple.com>
@tomerd
Copy link
Contributor Author

tomerd commented Jun 16, 2020

@tomerd Yes. I think we should include it. If people need to make some json calls or whatever a allocator comes in handy.

@fabianfett f21f4a3

@tomerd tomerd requested a review from fabianfett June 16, 2020 00:40
@tomerd tomerd merged commit d1a9276 into master Jun 16, 2020
@tomerd tomerd deleted the feature/init-context branch June 16, 2020 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants