Documentation
¶
Overview ¶
Package httpclient provides a shared HTTP client with sensible defaults for connection pooling, timeouts, and retry logic.
Index ¶
- Constants
- Variables
- func DoWithRetry(ctx context.Context, cfg *RetryConfig, operation string, fn func() error) error
- func DownloadClient() *http.Client
- func IsRetryableError(err error) bool
- func NewClient(cfg *Config) *http.Client
- func NewDefaultClient() *http.Client
- type Config
- type Option
- func WithIdleConnTimeout(timeout time.Duration) Option
- func WithMaxIdleConns(n int) Option
- func WithMaxIdleConnsPerHost(n int) Option
- func WithMaxRetryDelay(delay time.Duration) Option
- func WithResponseHeaderTimeout(timeout time.Duration) Option
- func WithRetryAttempts(attempts int) Option
- func WithRetryDelay(delay time.Duration) Option
- func WithRetryJitter(jitter time.Duration) Option
- func WithTLSHandshakeTimeout(timeout time.Duration) Option
- func WithTimeout(timeout time.Duration) Option
- type RetryConfig
Constants ¶
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 ¶
var DefaultClient = NewClient(nil)
DefaultClient returns an HTTP client with sensible defaults. This is a shared instance that can be used across the application.
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 ¶
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 ¶
DownloadClient returns an HTTP client optimized for downloading large files. It has a longer timeout and optimized settings for file transfers.
func IsRetryableError ¶
IsRetryableError determines if an error is transient and should be retried.
func NewDefaultClient ¶
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.
type Option ¶
type Option func(*Config)
Option is a function that modifies a Config.
func WithIdleConnTimeout ¶
WithIdleConnTimeout sets the idle connection timeout.
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections.
func WithMaxIdleConnsPerHost ¶
WithMaxIdleConnsPerHost sets the maximum number of idle connections per host.
func WithMaxRetryDelay ¶
WithMaxRetryDelay sets the maximum retry delay.
func WithResponseHeaderTimeout ¶
WithResponseHeaderTimeout sets the timeout for response headers.
func WithRetryAttempts ¶
WithRetryAttempts sets the number of retry attempts.
func WithRetryDelay ¶
WithRetryDelay sets the initial retry delay.
func WithRetryJitter ¶
WithRetryJitter sets the retry jitter.
func WithTLSHandshakeTimeout ¶
WithTLSHandshakeTimeout sets the TLS handshake timeout.
func WithTimeout ¶
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.