Passing the context to sync callbacks

This commit is contained in:
Stefan Scheidewig 2022-05-24 22:44:45 +02:00
parent 9ef0b54c7c
commit 51b09c734b
4 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"context"
"strconv" "strconv"
"strings" "strings"
@ -19,11 +20,11 @@ func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool {
return false 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) 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) return invokeHookSync(hooks.HookPreFinish, event)
} }

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"context"
"errors" "errors"
"log" "log"
"net/url" "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 // 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 // validation of upload metadata etc. Furthermore, HTTPResponse will be ignored and
// the error value can contain values for the HTTP response. // 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 // 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. // 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 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, // 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. // 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 { func (config *Config) validate() error {

View File

@ -19,8 +19,7 @@ type httpContext struct {
func newContext(w http.ResponseWriter, r *http.Request) *httpContext { func newContext(w http.ResponseWriter, r *http.Request) *httpContext {
return &httpContext{ return &httpContext{
// TODO: Try to reuse the request's context in the future Context: r.Context(),
Context: context.Background(),
res: w, res: w,
req: r, req: r,
body: nil, // body can be filled later for PATCH requests body: nil, // body can be filled later for PATCH requests

View File

@ -302,7 +302,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
} }
if handler.config.PreUploadCreateCallback != nil { 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 { if err != nil {
handler.sendError(c, err) handler.sendError(c, err)
return return
@ -677,7 +677,7 @@ func (handler *UnroutedHandler) finishUploadIfComplete(c *httpContext, resp HTTP
handler.Metrics.incUploadsFinished() handler.Metrics.incUploadsFinished()
if handler.config.PreFinishResponseCallback != nil { 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 { if err != nil {
return resp, err return resp, err
} }