httpclient

package
v0.6.10 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package httpclient provides a robust HTTP client with retry logic, exponential backoff, configurable timeouts, and structured logging.

Index

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 New

func New(cfg Config) *Client

New creates a new resilient HTTP client with the given configuration

func NewWithDefaults

func NewWithDefaults() *Client

NewWithDefaults creates a new client with default configuration

func (*Client) Do

func (c *Client) Do(req *http.Request) (*Response, error)

Do performs an HTTP request with retry logic

func (*Client) DoWithContext

func (c *Client) DoWithContext(ctx context.Context, req *http.Request) (*Response, error)

DoWithContext performs an HTTP request with retry logic and context

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*Response, error)

Get performs a GET request

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

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, url string, body []byte) (*Response, error)

PostJSON performs a POST request with JSON content type

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout returns a new client with the specified timeout

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

type Response

type Response struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

Response wraps http.Response with the body already read

Jump to

Keyboard shortcuts

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