aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/middleware/timeout.go
blob: 6a3f5deb4e56d9a0aacc9a9f3aa0d80f73890362 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package middleware

import (
	"context"
	"net/http"
	"time"
)

// Middleware to timeout requests after a given duration
func Timeout(next http.Handler, duration time.Duration) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx, cancel := context.WithTimeout(r.Context(), duration)
		defer cancel()

		next.ServeHTTP(w, r.WithContext(ctx))
	})
}