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:
15
server/middleware_timeout.go
Normal file
15
server/middleware_timeout.go
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user