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>
16 lines
410 B
Go
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")
|
|
}
|
|
}
|