Documentation
¶
Overview ¶
Package httpclient provides a robust HTTP client with retry logic, exponential backoff, configurable timeouts, and structured logging.
Index ¶
- type Client
- func (c *Client) Do(req *http.Request) (*Response, error)
- func (c *Client) DoWithContext(ctx context.Context, req *http.Request) (*Response, error)
- func (c *Client) Get(ctx context.Context, url string) (*Response, error)
- func (c *Client) Post(ctx context.Context, url string, contentType string, body []byte) (*Response, error)
- func (c *Client) PostJSON(ctx context.Context, url string, body []byte) (*Response, error)
- func (c *Client) WithTimeout(timeout time.Duration) *Client
- type Config
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a resilient HTTP client with retry and backoff
func NewWithDefaults ¶
func NewWithDefaults() *Client
NewWithDefaults creates a new client with default configuration
func (*Client) DoWithContext ¶
DoWithContext performs an HTTP request with retry logic and context
func (*Client) Post ¶
func (c *Client) Post(ctx context.Context, url string, contentType string, body []byte) (*Response, error)
Post performs a POST request with the given body
type Config ¶
type Config struct {
// Timeout is the total request timeout (default: 60s)
Timeout time.Duration
// MaxRetries is the maximum number of retry attempts (default: 3)
MaxRetries int
// InitialBackoff is the initial backoff duration (default: 2s)
InitialBackoff time.Duration
// MaxBackoff is the maximum backoff duration (default: 30s)
MaxBackoff time.Duration
// BackoffMultiplier is the backoff multiplier (default: 2.0)
BackoffMultiplier float64
// JitterFraction adds randomness to backoff (default: 0.1 = 10%)
JitterFraction float64
// RetryStatusCodes are HTTP status codes that should trigger a retry
// Default: 429, 500, 502, 503, 504, 529
RetryStatusCodes []int
// LogRequests enables request/response logging at debug level
LogRequests bool
// Transport overrides the default http.Transport. Useful for testing
// (VCR record/replay), proxying, or custom TLS configuration.
Transport http.RoundTripper
}
Config configures the resilient HTTP client
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns sensible defaults for the HTTP client
Click to show internal directories.
Click to hide internal directories.