Documentation
¶
Overview ¶
Package httpx provides a shared HTTP client with retry logic, timeouts, and helpers
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidTimeout indicates a timeout value is not positive. ErrInvalidTimeout = errors.New("timeout must be positive") // ErrInvalidMaxRetries indicates MaxRetries is negative. ErrInvalidMaxRetries = errors.New("MaxRetries cannot be negative") // ErrInvalidMaxResponseSize indicates MaxResponseSize is not positive. ErrInvalidMaxResponseSize = errors.New("MaxResponseSize must be positive") // ErrInvalidMaxIdleConns indicates MaxIdleConns is not positive. ErrInvalidMaxIdleConns = errors.New("MaxIdleConns must be positive") // ErrInvalidRetryInterval indicates retry interval is not positive. ErrInvalidRetryInterval = errors.New("retry interval must be positive") )
Sentinel errors for httpx configuration validation.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps an HTTP client with retry logic and performance optimizations
func NewWithConfig ¶ added in v1.0.0
NewWithConfig creates a new HTTP client with custom configuration.
Returns an error if the configuration is invalid.
func (*Client) DoJSON ¶
DoJSON performs an HTTP request and unmarshals the JSON response. It includes retry logic with exponential backoff for transient errors.
The method automatically handles: - Exponential backoff with jitter for retryable errors (429, 5xx) - Request timeouts to prevent hanging - Response size limits to prevent memory exhaustion (10MB default) - Context cancellation for early termination
Retryable status codes: 429 (Too Many Requests), 500-504 (Server Errors) Non-retryable errors: 4xx (except 429), JSON decode errors, network errors
The request context controls the overall timeout, while individual retry attempts have their own timeouts configured via Config.RequestTimeout.
type Config ¶ added in v1.0.0
type Config struct {
// Timeouts
DialTimeout time.Duration
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
RequestTimeout time.Duration
// Retry configuration
MaxRetries int
InitialInterval time.Duration
MaxInterval time.Duration
// Request limits
MaxResponseSize int64
// Connection pooling
MaxIdleConns int
MaxIdleConnsPerHost int
IdleConnTimeout time.Duration
// UserAgent to use in HTTP requests (optional, defaults to "hypermcp")
UserAgent string
// DisableCompression, if true, prevents the Transport from requesting compression
// with an "Accept-Encoding: gzip" request header when the Request contains no
// existing Accept-Encoding value. Defaults to false (compression enabled).
DisableCompression bool
// ForceAttemptHTTP2 controls whether HTTP/2 is enabled when a non-zero
// Dial, DialTLS, or DialContext func or TLSClientConfig is provided.
// Defaults to true.
ForceAttemptHTTP2 bool
}
Config holds HTTP client configuration options.
func DefaultConfig ¶ added in v1.0.0
func DefaultConfig() Config
DefaultConfig returns sensible default configuration for the HTTP client.
type ConfigError ¶ added in v1.0.0
ConfigError wraps httpx configuration validation errors with context.
func (*ConfigError) Error ¶ added in v1.0.0
func (e *ConfigError) Error() string
func (*ConfigError) Unwrap ¶ added in v1.0.0
func (e *ConfigError) Unwrap() error