timeout

package
v0.1.36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

timeout/client.go

timeout/context.go

timeout/timeout.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client() *http.Client

Client returns an HTTP client with default timeout settings.

func CombineSkippers

func CombineSkippers(skippers ...func(*http.Request) bool) func(*http.Request) bool

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

func FromContext(ctx context.Context) (time.Time, bool)

FromContext returns the deadline from the context, if any. Returns zero time and false if no deadline is set.

func Get

func Get(ctx context.Context, url string, timeout time.Duration) (*http.Response, error)

Get performs a GET request with the given timeout.

func HasDeadline

func HasDeadline(ctx context.Context) bool

HasDeadline returns true if the context has a deadline set.

func IsExpired

func IsExpired(ctx context.Context) bool

IsExpired returns true if the context deadline has passed.

func LongClient

func LongClient() *http.Client

LongClient returns an HTTP client for long-running requests. Uses longer timeouts suitable for file uploads, reports, etc.

func Middleware

func Middleware(cfg Config) func(http.Handler) http.Handler

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

func PropagateTimeout(parent context.Context) (context.Context, context.CancelFunc)

PropagateTimeout creates a new context that inherits the parent's deadline but is separately cancellable. Useful for spawning child operations.

func QuickClient

func QuickClient() *http.Client

QuickClient returns an HTTP client optimized for quick requests. Uses shorter timeouts suitable for internal service calls.

func Remaining

func Remaining(ctx context.Context) time.Duration

Remaining returns the time remaining until the context deadline. Returns 0 if no deadline is set or if the deadline has passed.

func Run

func Run(ctx context.Context, timeout time.Duration, fn func(context.Context) error) error

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

func ShrinkBy(ctx context.Context, reserve time.Duration) (context.Context, context.CancelFunc)

ShrinkBy reduces the remaining timeout by the given duration. Useful for reserving time for cleanup operations.

func Simple

func Simple() func(http.Handler) http.Handler

Simple returns timeout middleware with default settings (30 seconds).

func SkipMethods

func SkipMethods(methods ...string) func(*http.Request) bool

SkipMethods returns a skipper that skips the given HTTP methods.

func SkipPaths

func SkipPaths(paths ...string) func(*http.Request) bool

SkipPaths returns a skipper that skips the given paths.

func SkipSSE

func SkipSSE(r *http.Request) bool

SkipSSE returns true for Server-Sent Events requests.

func SkipWebSocket

func SkipWebSocket(r *http.Request) bool

SkipWebSocket returns true for WebSocket upgrade requests.

func WithExtended

func WithExtended(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)

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.

func WithTimeout

func WithTimeout(d time.Duration) func(http.Handler) http.Handler

WithTimeout returns timeout middleware with the specified 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.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

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.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL