Files
mtusi2026lab2/service/internal/pkg/rest/middleware/timeout.go
T

19 lines
444 B
Go

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))
})
}
}