From 51b09c734b7fe877e37c920b7243e23cbd0a0362 Mon Sep 17 00:00:00 2001 From: Stefan Scheidewig Date: Tue, 24 May 2022 22:44:45 +0200 Subject: [PATCH] Passing the context to sync callbacks --- cmd/tusd/cli/hooks.go | 5 +++-- pkg/handler/config.go | 5 +++-- pkg/handler/context.go | 3 +-- pkg/handler/unrouted_handler.go | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cmd/tusd/cli/hooks.go b/cmd/tusd/cli/hooks.go index 135ceaa..c63b728 100644 --- a/cmd/tusd/cli/hooks.go +++ b/cmd/tusd/cli/hooks.go @@ -1,6 +1,7 @@ package cli import ( + "context" "strconv" "strings" @@ -19,11 +20,11 @@ func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool { return false } -func preCreateCallback(event handler.HookEvent) (handler.HTTPResponse, error) { +func preCreateCallback(_ context.Context, event handler.HookEvent) (handler.HTTPResponse, error) { return invokeHookSync(hooks.HookPreCreate, event) } -func preFinishCallback(event handler.HookEvent) (handler.HTTPResponse, error) { +func preFinishCallback(_ context.Context, event handler.HookEvent) (handler.HTTPResponse, error) { return invokeHookSync(hooks.HookPreFinish, event) } diff --git a/pkg/handler/config.go b/pkg/handler/config.go index bb3f610..d57f683 100644 --- a/pkg/handler/config.go +++ b/pkg/handler/config.go @@ -1,6 +1,7 @@ package handler import ( + "context" "errors" "log" "net/url" @@ -52,13 +53,13 @@ type Config struct { // If the error is non-nil, the upload will not be created. This can be used to implement // validation of upload metadata etc. Furthermore, HTTPResponse will be ignored and // the error value can contain values for the HTTP response. - PreUploadCreateCallback func(hook HookEvent) (HTTPResponse, error) + PreUploadCreateCallback func(ctx context.Context, hook HookEvent) (HTTPResponse, error) // PreFinishResponseCallback will be invoked after an upload is completed but before // a response is returned to the client. This can be used to implement post-processing validation. // If the callback returns no error, optional values from HTTPResponse will be contained in the HTTP response. // If the error is non-nil, the error will be forwarded to the client. Furthermore, // HTTPResponse will be ignored and the error value can contain values for the HTTP response. - PreFinishResponseCallback func(hook HookEvent) (HTTPResponse, error) + PreFinishResponseCallback func(ctx context.Context, hook HookEvent) (HTTPResponse, error) } func (config *Config) validate() error { diff --git a/pkg/handler/context.go b/pkg/handler/context.go index 67050d5..5cf8786 100644 --- a/pkg/handler/context.go +++ b/pkg/handler/context.go @@ -19,8 +19,7 @@ type httpContext struct { func newContext(w http.ResponseWriter, r *http.Request) *httpContext { return &httpContext{ - // TODO: Try to reuse the request's context in the future - Context: context.Background(), + Context: r.Context(), res: w, req: r, body: nil, // body can be filled later for PATCH requests diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index c715e9a..51d81c6 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -302,7 +302,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request) } if handler.config.PreUploadCreateCallback != nil { - resp2, err := handler.config.PreUploadCreateCallback(newHookEvent(info, r)) + resp2, err := handler.config.PreUploadCreateCallback(c, newHookEvent(info, r)) if err != nil { handler.sendError(c, err) return @@ -677,7 +677,7 @@ func (handler *UnroutedHandler) finishUploadIfComplete(c *httpContext, resp HTTP handler.Metrics.incUploadsFinished() if handler.config.PreFinishResponseCallback != nil { - resp2, err := handler.config.PreFinishResponseCallback(newHookEvent(info, r)) + resp2, err := handler.config.PreFinishResponseCallback(c, newHookEvent(info, r)) if err != nil { return resp, err }