httpclient

package
v0.25.1 Latest Latest
Warning

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

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

Documentation

Overview

Package httpclient provides a shared HTTP client with sensible defaults for connection pooling, timeouts, and retry logic.

Index

Constants

View Source
const (
	// Default timeout for HTTP requests.
	DefaultTimeout = 120 * time.Second

	// Default maximum number of idle connections across all hosts.
	DefaultMaxIdleConns = 100

	// Default maximum number of idle connections per host.
	DefaultMaxIdleConnsPerHost = 20

	// Default timeout for idle connections.
	DefaultIdleConnTimeout = 30 * time.Second

	// Default timeout for TLS handshakes.
	DefaultTLSHandshakeTimeout = 10 * time.Second

	// Default timeout for connection attempts.
	DefaultDialTimeout = 30 * time.Second

	// Default keep-alive timeout for connections.
	DefaultKeepAlive = 30 * time.Second

	// Default response header timeout.
	DefaultResponseHeaderTimeout = 30 * time.Second

	// Default timeout for expecting a 100-continue response.
	DefaultExpectContinueTimeout = 1 * time.Second

	// Default retry configuration.
	DefaultRetryAttempts = 3
	DefaultRetryDelay    = 2 * time.Second
	DefaultMaxRetryDelay = 30 * time.Second
	DefaultRetryJitter   = 1 * time.Second
)

Default configuration values for HTTP clients.

Variables

View Source
var DefaultClient = NewClient(nil)

DefaultClient returns an HTTP client with sensible defaults. This is a shared instance that can be used across the application.

View Source
var RetryableErrorPatterns = []string{
	"context deadline exceeded",
	"context canceled",
	"http2: server sent GOAWAY",
	"connection reset by peer",
	"connection refused",
	"unexpected EOF",
	"io: read/write on closed pipe",
	"network is unreachable",
	"no such host",
	"timeout",
	"ECONNRESET",
	"ECONNREFUSED",
	"ETIMEDOUT",
	"Error 500",
	"Error 503",
	"Error 429",
}

RetryableErrorPatterns contains error patterns that indicate a transient failure.

Functions

func DoWithRetry

func DoWithRetry(ctx context.Context, cfg *RetryConfig, operation string, fn func() error) error

DoWithRetry executes a function with retry logic for transient errors. The operation function should return an error if it fails. If the error is retryable, the function will be retried with exponential backoff.

func DownloadClient

func DownloadClient() *http.Client

DownloadClient returns an HTTP client optimized for downloading large files. It has a longer timeout and optimized settings for file transfers.

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError determines if an error is transient and should be retried.

func NewClient

func NewClient(cfg *Config) *http.Client

NewClient creates a new HTTP client with the given configuration.

func NewDefaultClient

func NewDefaultClient() *http.Client

NewDefaultClient creates a new HTTP client with default settings. Use this when you need a separate client instance from the shared DefaultClient.

Types

type Config

type Config struct {
	// Timeout is the total timeout for HTTP requests.
	Timeout time.Duration

	// MaxIdleConns is the maximum number of idle connections across all hosts.
	MaxIdleConns int

	// MaxIdleConnsPerHost is the maximum number of idle connections per host.
	MaxIdleConnsPerHost int

	// IdleConnTimeout is the timeout for idle connections.
	IdleConnTimeout time.Duration

	// TLSHandshakeTimeout is the timeout for TLS handshakes.
	TLSHandshakeTimeout time.Duration

	// DialTimeout is the timeout for connection attempts.
	DialTimeout time.Duration

	// KeepAlive is the keep-alive timeout for connections.
	KeepAlive time.Duration

	// ResponseHeaderTimeout is the timeout for response headers.
	ResponseHeaderTimeout time.Duration

	// ExpectContinueTimeout is the timeout for expecting 100-continue responses.
	ExpectContinueTimeout time.Duration

	// RetryAttempts is the number of retry attempts for transient errors.
	// Set to 0 to disable retries.
	RetryAttempts int

	// RetryDelay is the initial delay between retry attempts.
	RetryDelay time.Duration

	// MaxRetryDelay is the maximum delay between retry attempts.
	MaxRetryDelay time.Duration

	// RetryJitter is the random jitter added to retry delays.
	RetryJitter time.Duration
}

Config holds configuration for creating an HTTP client.

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig creates a Config with default values.

type Option

type Option func(*Config)

Option is a function that modifies a Config.

func WithIdleConnTimeout

func WithIdleConnTimeout(timeout time.Duration) Option

WithIdleConnTimeout sets the idle connection timeout.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle connections.

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) Option

WithMaxIdleConnsPerHost sets the maximum number of idle connections per host.

func WithMaxRetryDelay

func WithMaxRetryDelay(delay time.Duration) Option

WithMaxRetryDelay sets the maximum retry delay.

func WithRetryAttempts

func WithRetryAttempts(attempts int) Option

WithRetryAttempts sets the number of retry attempts.

func WithRetryDelay

func WithRetryDelay(delay time.Duration) Option

WithRetryDelay sets the initial retry delay.

func WithRetryJitter

func WithRetryJitter(jitter time.Duration) Option

WithRetryJitter sets the retry jitter.

func WithTLSHandshakeTimeout

func WithTLSHandshakeTimeout(timeout time.Duration) Option

WithTLSHandshakeTimeout sets the TLS handshake timeout.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the request timeout.

type RetryConfig

type RetryConfig struct {
	// Attempts is the number of retry attempts (not including the initial attempt).
	Attempts int

	// Delay is the initial delay between retry attempts.
	Delay time.Duration

	// MaxDelay is the maximum delay between retry attempts.
	MaxDelay time.Duration

	// Jitter is the random jitter added to retry delays.
	Jitter time.Duration
}

RetryConfig holds configuration for retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig returns a RetryConfig with default values.

Jump to

Keyboard shortcuts

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