aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/middleware/timeout.go
diff options
context:
space:
mode:
Diffstat (limited to 'middleware/timeout.go')
-rw-r--r--middleware/timeout.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/middleware/timeout.go b/middleware/timeout.go
new file mode 100644
index 0000000..6a3f5de
--- /dev/null
+++ b/middleware/timeout.go
@@ -0,0 +1,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))
+ })
+}