X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d90f02c0e969b649da61fdd272a9aab69c487de3..2b8fc576e242c0b8658eef9f1130143e009efc4d:/sdk/go/httpserver/logger.go diff --git a/sdk/go/httpserver/logger.go b/sdk/go/httpserver/logger.go index 78a1f77adb..1916880963 100644 --- a/sdk/go/httpserver/logger.go +++ b/sdk/go/httpserver/logger.go @@ -31,6 +31,16 @@ func HandlerWithContext(ctx context.Context, next http.Handler) http.Handler { }) } +// HandlerWithDeadline cancels the request context if the request +// takes longer than the specified timeout. +func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithDeadline(r.Context(), time.Now().Add(timeout)) + defer cancel() + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + // LogRequests wraps an http.Handler, logging each request and // response. func LogRequests(h http.Handler) http.Handler {