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
Support setting an optional base context for functions. #287
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat! Just have some comments around the Function
type.
lambda/function.go
Outdated
} | ||
|
||
// NewFunction which creates a Function with a given Handler | ||
func NewFunction(handler Handler) *Function { | ||
return &Function{handler: handler} | ||
} | ||
|
||
// NewFunctionWithContext which creates a Function with a given Handler and sets the base Context. | ||
func NewFunctionWithContext(ctx context.Context, handler Handler) *Function { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be private? NewFunction
doesn't really have a reason to be in the public API, so it'd be nice to not increase surface area further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmoffatt removed this function. I decided to go for the http.Request.WithContext
approach instead, except that WithContext
is not exported on the Function
. I think that's a little cleaner 👍
lambda/function.go
Outdated
} | ||
|
||
// Context returns the base context used for the fn. | ||
func (fn *Function) Context() context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be private, or moved inside of Invoke
. The public visibility of the other Function
methods is done only as a requirement of the net/ipc
library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmoffatt made this un exported 👍
Codecov Report
@@ Coverage Diff @@
## master #287 +/- ##
==========================================
+ Coverage 74.51% 74.60% +0.08%
==========================================
Files 13 13
Lines 624 638 +14
==========================================
+ Hits 465 476 +11
- Misses 121 123 +2
- Partials 38 39 +1
Continue to review full report at Codecov.
|
Issue #, if available:
n/a
Description of changes:
Adds support for setting a base context to be given to functions that take context as their first argument. Current implementation is to provide a
context.Background()
. This implements a similar feature as seen innet/http
where ahttp.Server
can be given aBaseContext
function which returns acontext.Context
to be given to all http requests: https://golang.org/src/net/http/server.go#L2572This avoids the need for injecting context values into the context before a function is called.
This is a non breaking API change as new explicit methods have been added to support passing in a base context.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.