X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8f0f3b495d0c715cc57d0d384bc5839acf9ece3f..57792708500261a817e6957e65c80c7f798a36e9:/sdk/go/httpserver/logger.go diff --git a/sdk/go/httpserver/logger.go b/sdk/go/httpserver/logger.go index ef2ec41704..b71adf7118 100644 --- a/sdk/go/httpserver/logger.go +++ b/sdk/go/httpserver/logger.go @@ -47,7 +47,13 @@ func (hn hijackNotifier) Hijack() (net.Conn, *bufio.ReadWriter, error) { // HandlerWithDeadline cancels the request context if the request // takes longer than the specified timeout without having its // connection hijacked. +// +// If timeout is 0, there is no deadline: HandlerWithDeadline is a +// no-op. func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler { + if timeout == 0 { + return next + } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(r.Context()) defer cancel() @@ -68,18 +74,15 @@ func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler } func SetResponseLogFields(ctx context.Context, fields logrus.Fields) { - m := ctx.Value(&mutexContextKey) - if mutex, ok := m.(sync.Mutex); ok { - mutex.Lock() - defer mutex.Unlock() - ctxfields := ctx.Value(&responseLogFieldsContextKey) - if c, ok := ctxfields.(logrus.Fields); ok { - for k, v := range fields { - c[k] = v - } - } - } else { - // We can't lock, don't set the fields + m, _ := ctx.Value(&mutexContextKey).(*sync.Mutex) + c, _ := ctx.Value(&responseLogFieldsContextKey).(logrus.Fields) + if m == nil || c == nil { + return + } + m.Lock() + defer m.Unlock() + for k, v := range fields { + c[k] = v } } @@ -101,7 +104,7 @@ func LogRequests(h http.Handler) http.Handler { ctx := req.Context() ctx = context.WithValue(ctx, &requestTimeContextKey, time.Now()) ctx = context.WithValue(ctx, &responseLogFieldsContextKey, logrus.Fields{}) - ctx = context.WithValue(ctx, &mutexContextKey, sync.Mutex{}) + ctx = context.WithValue(ctx, &mutexContextKey, &sync.Mutex{}) ctx = ctxlog.Context(ctx, lgr) req = req.WithContext(ctx)