Context Timeout
ContextTimeout wraps the request context with a deadline before the rest of the chain runs.
Quick start
Section titled “Quick start”app.Use(middleware.ContextTimeout(2 * time.Second))Config fields
Section titled “Config fields”| Field | Meaning |
|---|---|
Skipper |
Skip timeout handling for selected requests |
Timeout |
Required timeout duration |
ErrorHandler |
Customize timeout failures |
Accessing timeout state
Section titled “Accessing timeout state”info, ok := middleware.ContextTimeoutCurrent(c)remaining, ok := middleware.ContextTimeoutRemaining(c)ContextTimeoutInfo contains:
TimeoutDeadline
Error behavior
Section titled “Error behavior”If the handler chain returns context.DeadlineExceeded, Zinc wraps it in *middleware.ContextTimeoutError.
The default error handler returns 503 Service Unavailable joined with the timeout error.
Example
Section titled “Example”app.Use(middleware.ContextTimeoutWithConfig(middleware.ContextTimeoutConfig{ Timeout: 2 * time.Second, ErrorHandler: func(c *zinc.Context, err *middleware.ContextTimeoutError) error { return zinc.ErrServiceUnavailable.WithMessage("request timed out") },}))