13697: Cancel request context after API.RequestTimeout.
[arvados.git] / sdk / go / httpserver / logger.go
index 78a1f77adb9ca9942d00fe559c3192b658e8e5fe..1916880963494333ce9d4356f1d1c9d2eba55f3b 100644 (file)
@@ -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 {