Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrTimeout = errors.New("request timeout") ErrInvalidTimeout = errors.New("invalid timeout duration") )
Functions ¶
func New ¶
func New(timeout time.Duration) fh.HandlerFunc
New returns timeout middleware using the given duration.
func NewWithConfig ¶
func NewWithConfig(cfg Config) fh.HandlerFunc
NewWithConfig returns production-safe timeout middleware.
Types ¶
type Config ¶
type Config struct {
// Timeout is the maximum time allowed for downstream middleware/handler work.
// If <= 0, DefaultTimeout is used unless RejectInvalidTimeout is true.
Timeout time.Duration
// StatusCode is used by the default timeout response.
// Defaults to 503.
StatusCode int
// Message is used by the default timeout response.
Message string
// HeaderName is set on timeout responses.
// Defaults to "X-Timeout".
HeaderName string
// HeaderValue is set on timeout responses.
// Defaults to the timeout duration string.
HeaderValue string
// Skipper skips timeout handling for selected requests.
Skipper Skipper
// OnTimeout customizes timeout response behavior.
OnTimeout TimeoutHandler
// OnError customizes non-timeout error handling.
OnError ErrorHandler
// RejectInvalidTimeout makes NewWithConfig return middleware that rejects
// every request with 500 if Timeout <= 0. Usually keep this false.
RejectInvalidTimeout bool
// PreserveContext controls whether the previous context is restored after
// the request returns. This should normally stay true.
PreserveContext bool
}
Config configures timeout middleware.
This middleware is intentionally cooperative and production-safe. It sets c.Context() to a deadline context and expects long-running handlers, database calls, queues, HTTP clients, and business logic to observe c.Context().Done().
It does not forcibly kill the handler goroutine because fh.Ctx is usually pooled and response writing after timeout can cause races or corrupted output.
type ErrorHandler ¶
ErrorHandler handles non-timeout errors returned by downstream handlers.
Click to show internal directories.
Click to hide internal directories.