httpx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package httpx provides a shared HTTP client with retry logic, timeouts, and helpers

Index

Constants

This section is empty.

Variables

View Source
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 New

func New(logger *zap.Logger) (*Client, error)

New creates a new HTTP client with default configuration.

func NewWithConfig added in v1.0.0

func NewWithConfig(cfg Config, logger *zap.Logger) (*Client, error)

NewWithConfig creates a new HTTP client with custom configuration.

Returns an error if the configuration is invalid.

func (*Client) DoJSON

func (c *Client) DoJSON(ctx context.Context, req *http.Request, result interface{}) error

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.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, result interface{}) error

Get is a convenience wrapper for GET requests

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.

func (Config) Validate added in v1.0.0

func (c Config) Validate() error

Validate checks if the configuration is valid.

Returns a ConfigError if any field contains an invalid value.

type ConfigError added in v1.0.0

type ConfigError struct {
	Err   error
	Field string
}

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

Jump to

Keyboard shortcuts

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