Files
httpx/server/middleware_timeout.go
Aleksey Shakhmatov 7f12b0c87a 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>
2026-03-22 21:47:33 +03:00

16 lines
410 B
Go

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