Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultTimeoutResponse ¶
DefaultTimeoutResponse sends a default 503 Service Unavailable response.
func Middleware ¶
func Middleware(dt time.Duration, opts ...Option) fox.MiddlewareFunc
Middleware returns a fox.MiddlewareFunc with a specified timeout and options. This middleware function, when used, will ensure HTTP handlers don't exceed the given timeout duration.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithFilter ¶
WithFilter appends the provided filters to the middleware's filter list. A filter returning true will exclude the request from using the timeout handler. If no filters are provided, all requests will be handled. Keep in mind that filters are invoked for each request, so they should be simple and efficient.
func WithResponse ¶
func WithResponse(h fox.HandlerFunc) Option
WithResponse sets a custom response handler function for the middleware. This function will be invoked when a timeout occurs, allowing for custom responses to be sent back to the client. If not set, the middleware use DefaultTimeoutResponse.
func WithTimeoutResolver ¶ added in v0.17.0
WithTimeoutResolver sets a custom Resolver to determine the timeout dynamically based on fox.Context. If the resolver returns false, the default timeout is applied. Keep in mind that a resolver is invoked for each request, so they should be simple and efficient.
type Resolver ¶ added in v0.17.0
Resolver defines the interface for resolving a timeout duration dynamically based on fox.Context. A time.Duration is returned if a custom timeout is applicable, along with a boolean indicating if the duration was resolved.
type Timeout ¶
type Timeout struct {
// contains filtered or unexported fields
}
Timeout is a middleware that ensure HTTP handlers don't exceed the configured timeout duration.
func New ¶
New creates and initializes a new Timeout middleware with the given timeout duration and optional settings.3
func (*Timeout) Timeout ¶
func (t *Timeout) Timeout(next fox.HandlerFunc) fox.HandlerFunc
Timeout returns a fox.HandlerFunc that runs next with the given time limit.
The new handler calls next to handle each request, but if a call runs for longer than its time limit, the handler responds with a 503 Service Unavailable error and the given message in its body (if a custom response handler is not configured). After such a timeout, writes by next to its ResponseWriter will return http.ErrHandlerTimeout.
Timeout supports the http.Pusher interface but does not support the http.Hijacker or http.Flusher interfaces.
type TimeoutResolverFunc ¶ added in v0.17.0
The TimeoutResolverFunc type is an adapter to allow the use of ordinary functions as Resolver. If f is a function with the appropriate signature, TimeoutResolverFunc(f) is a TimeoutResolverFunc that calls f.