Add server Timeout middleware for context-based request deadlines

Wraps http.TimeoutHandler to return 503 when handlers exceed the
configured duration. Unlike http.Server.WriteTimeout, this allows
handlers to complete gracefully via context cancellation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-22 21:47:33 +03:00
parent 1b322c8c81
commit 7f12b0c87a
2 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package server
import (
"net/http"
"time"
)
// Timeout returns a middleware that limits request processing time.
// If the handler does not complete within d, the client receives a
// 503 Service Unavailable response. It wraps http.TimeoutHandler.
func Timeout(d time.Duration) Middleware {
return func(next http.Handler) http.Handler {
return http.TimeoutHandler(next, d, "Service Unavailable\n")
}
}