From b31e65bf8a330fa51a27d762d6cf8e4be5c09adf Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Sun, 5 Apr 2020 09:24:18 +0100 Subject: [PATCH] Add setter for Context In d9259e34b65e7fb45ed7c6b95f4f2306f9a859db a getter was added for the context, this adds a setter. cc @LucasRoesler Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- handler.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/handler.go b/handler.go index b422db0..cff9155 100644 --- a/handler.go +++ b/handler.go @@ -33,6 +33,17 @@ func (r *Request) Context() context.Context { return r.ctx } +// WithContext overides the context for the Request struct +func (r *Request) WithContext(ctx context.Context) { + // AE: Not keen on panic mid-flow in user-code, however stdlib also appears to do + // this. https://golang.org/src/net/http/request.go + // This is not setting a precedent for broader use of "panic" to handle errors. + if ctx == nil { + panic("nil context") + } + r.ctx = ctx +} + // FunctionHandler used for a serverless Go method invocation type FunctionHandler interface { Handle(req Request) (Response, error)