package middleware import ( "context" "net/http" "time" ) // RequestTimeout bounds database work and waiting for a pooled connection. func RequestTimeout(timeout time.Duration) Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), timeout) defer cancel() next.ServeHTTP(w, r.WithContext(ctx)) }) } }