httpx

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTooManyRequests   = errors.New("too many requests (429)")
	ErrServerUnavailable = errors.New("server unavailable (5xx)")
	ErrDecode            = errors.New("decode error")
	ErrClientConfig      = errors.New("client configuration error")
	ErrRateLimited       = errors.New("rate limited")
	ErrCircuitOpen       = errors.New("circuit breaker is open")
	ErrTimeout           = errors.New("request timeout")
	ErrContextCanceled   = errors.New("context canceled")
)

Error types for HTTP operations

Functions

func IsFatalError

func IsFatalError(err error) bool

IsFatalError checks if an error is fatal and should not be retried

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError checks if an error should trigger a retry

Types

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker implements a circuit breaker pattern

func NewCircuitBreaker

func NewCircuitBreaker(window time.Duration, failureThreshold int, resetTimeout time.Duration) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow checks if the circuit breaker allows the request

func (*CircuitBreaker) Failures

func (cb *CircuitBreaker) Failures() int

Failures returns the current failure count (for testing)

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure records a failed request

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a successful request

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

State returns the current state of the circuit breaker

type CircuitState

type CircuitState int

CircuitState represents the state of the circuit breaker

const (
	StateClosed CircuitState = iota
	StateOpen
	StateHalfOpen
)

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides a robust HTTP client with retry, backoff, rate limiting, circuit breaker, and session rotation

func NewClient

func NewClient(config *Config) *Client

NewClient creates a new HTTP client with the given configuration

func (*Client) Do

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

Do executes an HTTP request with retry, backoff, rate limiting, and circuit breaker

func (*Client) GetSessionStats

func (c *Client) GetSessionStats() map[string]interface{}

GetSessionStats returns session usage statistics

type Config

type Config struct {
	BaseURL               string
	Timeout               time.Duration
	IdleTimeout           time.Duration
	MaxConnsPerHost       int
	MaxAttempts           int
	BackoffBaseMs         int
	BackoffJitterMs       int
	MaxDelayMs            int
	QPS                   float64
	Burst                 int
	CircuitWindow         time.Duration
	FailureThreshold      int
	ResetTimeout          time.Duration
	UserAgent             string
	EnableSessionRotation bool
	NumSessions           int
}

Config holds HTTP client configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a sensible default configuration

func SessionRotationConfig

func SessionRotationConfig() *Config

SessionRotationConfig returns a configuration optimized for session rotation

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    string
	Err        error
}

HTTPError wraps HTTP status errors with additional context

func NewHTTPError

func NewHTTPError(statusCode int, message string, err error) *HTTPError

NewHTTPError creates a new HTTP error

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter implements a token bucket rate limiter

func NewRateLimiter

func NewRateLimiter(qps, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter

func (*RateLimiter) Wait

func (r *RateLimiter) Wait(ctx context.Context) error

Wait blocks until a token is available

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

SessionManager manages multiple HTTP sessions with cookie rotation This helps avoid rate limiting by rotating between different sessions

func NewSessionManager

func NewSessionManager(baseURL string, numSessions int) *SessionManager

NewSessionManager creates a new session manager with multiple sessions

func (*SessionManager) AddCustomCookie

func (sm *SessionManager) AddCustomCookie(name, value string) error

AddCustomCookie adds a custom cookie to all sessions

func (*SessionManager) ClearAllCookies

func (sm *SessionManager) ClearAllCookies() error

ClearAllCookies clears all cookies from all sessions

func (*SessionManager) GetCurrentSessionIndex

func (sm *SessionManager) GetCurrentSessionIndex() int

GetCurrentSessionIndex returns the current session index

func (*SessionManager) GetNextSession

func (sm *SessionManager) GetNextSession() *http.Client

GetNextSession returns the next session in rotation

func (*SessionManager) GetSessionCount

func (sm *SessionManager) GetSessionCount() int

GetSessionCount returns the number of sessions

func (*SessionManager) GetSessionStats

func (sm *SessionManager) GetSessionStats() map[string]interface{}

GetSessionStats returns statistics about session usage

func (*SessionManager) InitializeSessions

func (sm *SessionManager) InitializeSessions() error

InitializeSessions initializes all sessions by making a request to get initial cookies

func (*SessionManager) RotateSession

func (sm *SessionManager) RotateSession()

RotateSession manually rotates to the next session

type TransportError

type TransportError struct {
	Err error
}

TransportError represents a network transport error

func NewTransportError

func NewTransportError(err error) *TransportError

NewTransportError creates a new transport error

func (*TransportError) Error

func (e *TransportError) Error() string

func (*TransportError) Unwrap

func (e *TransportError) Unwrap() error

Jump to

Keyboard shortcuts

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