-
Notifications
You must be signed in to change notification settings - Fork 547
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,31 @@ import ( | |
// Function struct which wrap the Handler | ||
type Function struct { | ||
handler Handler | ||
context context.Context | ||
} | ||
|
||
// 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 { | ||
return &Function{ | ||
context: ctx, | ||
handler: handler, | ||
} | ||
} | ||
|
||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. This should be private, or moved inside of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bmoffatt made this un exported 👍 |
||
if fn.context == nil { | ||
return context.Background() | ||
} | ||
|
||
return fn.context | ||
} | ||
|
||
// Ping method which given a PingRequest and a PingResponse parses the PingResponse | ||
func (fn *Function) Ping(req *messages.PingRequest, response *messages.PingResponse) error { | ||
*response = messages.PingResponse{} | ||
|
@@ -44,7 +62,7 @@ func (fn *Function) Invoke(req *messages.InvokeRequest, response *messages.Invok | |
}() | ||
|
||
deadline := time.Unix(req.Deadline.Seconds, req.Deadline.Nanos).UTC() | ||
invokeContext, cancel := context.WithDeadline(context.Background(), deadline) | ||
invokeContext, cancel := context.WithDeadline(fn.Context(), deadline) | ||
defer cancel() | ||
|
||
lc := &lambdacontext.LambdaContext{ | ||
|
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 thatWithContext
is not exported on theFunction
. I think that's a little cleaner 👍