Documentation
¶
Overview ¶
timeout/client.go
timeout/context.go
timeout/timeout.go
Index ¶
- func Client() *http.Client
- func CombineSkippers(skippers ...func(*http.Request) bool) func(*http.Request) bool
- func Do(ctx context.Context, client *http.Client, req *http.Request, ...) (*http.Response, error)
- func FromContext(ctx context.Context) (time.Time, bool)
- func Get(ctx context.Context, url string, timeout time.Duration) (*http.Response, error)
- func HasDeadline(ctx context.Context) bool
- func IsExpired(ctx context.Context) bool
- func LongClient() *http.Client
- func Middleware(cfg Config) func(http.Handler) http.Handler
- func NewClient(cfg ClientConfig) *http.Client
- func PropagateTimeout(parent context.Context) (context.Context, context.CancelFunc)
- func QuickClient() *http.Client
- func Remaining(ctx context.Context) time.Duration
- func Run(ctx context.Context, timeout time.Duration, fn func(context.Context) error) error
- func RunWithResult[T any](ctx context.Context, timeout time.Duration, ...) (T, error)
- func ShrinkBy(ctx context.Context, reserve time.Duration) (context.Context, context.CancelFunc)
- func Simple() func(http.Handler) http.Handler
- func SkipMethods(methods ...string) func(*http.Request) bool
- func SkipPaths(paths ...string) func(*http.Request) bool
- func SkipSSE(r *http.Request) bool
- func SkipWebSocket(r *http.Request) bool
- func WithExtended(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)
- func WithShorter(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)
- func WithTimeout(d time.Duration) func(http.Handler) http.Handler
- type ClientConfig
- type Config
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombineSkippers ¶
CombineSkippers combines multiple skippers with OR logic.
func Do ¶
func Do(ctx context.Context, client *http.Client, req *http.Request, timeout time.Duration) (*http.Response, error)
Do performs an HTTP request with the given timeout. If client is nil, uses the default client.
func FromContext ¶
FromContext returns the deadline from the context, if any. Returns zero time and false if no deadline is set.
func HasDeadline ¶
HasDeadline returns true if the context has a deadline set.
func LongClient ¶
LongClient returns an HTTP client for long-running requests. Uses longer timeouts suitable for file uploads, reports, etc.
func Middleware ¶
Middleware returns timeout middleware with the given configuration.
func NewClient ¶
func NewClient(cfg ClientConfig) *http.Client
NewClient creates an HTTP client with the given timeout configuration.
func PropagateTimeout ¶
PropagateTimeout creates a new context that inherits the parent's deadline but is separately cancellable. Useful for spawning child operations.
func QuickClient ¶
QuickClient returns an HTTP client optimized for quick requests. Uses shorter timeouts suitable for internal service calls.
func Remaining ¶
Remaining returns the time remaining until the context deadline. Returns 0 if no deadline is set or if the deadline has passed.
func Run ¶
Run executes a function with the given timeout. Returns context.DeadlineExceeded if the function doesn't complete in time.
func RunWithResult ¶
func RunWithResult[T any](ctx context.Context, timeout time.Duration, fn func(context.Context) (T, error)) (T, error)
RunWithResult executes a function with the given timeout and returns its result.
func ShrinkBy ¶
ShrinkBy reduces the remaining timeout by the given duration. Useful for reserving time for cleanup operations.
func SkipMethods ¶
SkipMethods returns a skipper that skips the given HTTP methods.
func SkipWebSocket ¶
SkipWebSocket returns true for WebSocket upgrade requests.
func WithExtended ¶
WithExtended creates a context with a new deadline extended from now. Note: This creates a new context that may outlive the parent's deadline. Use carefully - the parent context's cancellation will still propagate.
func WithShorter ¶
WithShorter creates a context with a deadline that is the shorter of the existing deadline (if any) and the given duration.
Types ¶
type ClientConfig ¶
type ClientConfig struct {
// Total timeout for the entire request (including redirects).
// Default: 30 seconds.
Timeout time.Duration
// DialTimeout is the maximum time to establish a connection.
// Default: 10 seconds.
DialTimeout time.Duration
// TLSHandshakeTimeout is the maximum time for TLS handshake.
// Default: 10 seconds.
TLSHandshakeTimeout time.Duration
// ResponseHeaderTimeout is the maximum time to wait for response headers.
// Default: 10 seconds.
ResponseHeaderTimeout time.Duration
// IdleConnTimeout is the maximum time an idle connection will remain open.
// Default: 90 seconds.
IdleConnTimeout time.Duration
// MaxIdleConns controls the maximum number of idle connections.
// Default: 100.
MaxIdleConns int
// MaxIdleConnsPerHost controls idle connections per host.
// Default: 10.
MaxIdleConnsPerHost int
// MaxConnsPerHost limits total connections per host.
// Default: 0 (no limit).
MaxConnsPerHost int
// ExpectContinueTimeout is the time to wait for 100-continue response.
// Default: 1 second.
ExpectContinueTimeout time.Duration
}
ClientConfig configures an HTTP client with various timeouts.
func DefaultClientConfig ¶
func DefaultClientConfig() ClientConfig
DefaultClientConfig returns sensible client timeout defaults.
type Config ¶
type Config struct {
// Timeout is the maximum duration for handling a request.
// Default: 30 seconds.
Timeout time.Duration
// OnTimeout is called when a request times out.
// If nil, a default 503 Service Unavailable response is sent.
OnTimeout func(w http.ResponseWriter, r *http.Request)
// Skipper determines whether to skip timeout for a request.
// Useful for SSE, WebSocket upgrades, or long-polling endpoints.
Skipper func(r *http.Request) bool
// ErrorHandler handles panics that occur during request processing.
// If nil, panics are re-raised after timeout cleanup.
ErrorHandler func(w http.ResponseWriter, r *http.Request, err interface{})
}
Config configures the timeout middleware.
type Transport ¶
type Transport struct {
// Base is the underlying transport. If nil, http.DefaultTransport is used.
Base http.RoundTripper
// Timeout is the per-request timeout.
Timeout time.Duration
}
Transport wraps an http.RoundTripper to enforce per-request timeouts.
func WithTransportTimeout ¶
func WithTransportTimeout(base http.RoundTripper, timeout time.Duration) *Transport
WithTransportTimeout wraps an existing transport with per-request timeout.