httpclient

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyNotReplayable = errors.New("request body is not replayable")

ErrBodyNotReplayable indicates a request body cannot be retried.

View Source
var ErrCircuitOpen = errors.New("circuit breaker open")

ErrCircuitOpen indicates the circuit breaker is open.

Functions

func DefaultBreakerDecider

func DefaultBreakerDecider(req *http.Request, resp *http.Response, err error) bool

DefaultBreakerDecider trips on network errors or 5xx responses.

func DefaultRetryDecider

func DefaultRetryDecider(req *http.Request, resp *http.Response, err error) bool

DefaultRetryDecider retries idempotent methods on network errors or 5xx/429 responses.

func NewClient

func NewClient(options ClientOptions) *http.Client

NewClient builds an http.Client with retries and breaker support.

Types

type BackoffFunc

type BackoffFunc func(attempt int) time.Duration

BackoffFunc returns the backoff duration for a retry attempt.

func ExponentialBackoff

func ExponentialBackoff(base, max time.Duration) BackoffFunc

ExponentialBackoff returns a backoff function with exponential growth.

type BreakerDecider

type BreakerDecider func(req *http.Request, resp *http.Response, err error) bool

BreakerDecider decides whether a response/error should trip the breaker.

type BreakerRoundTripper

type BreakerRoundTripper struct {
	Base       http.RoundTripper
	Breaker    *CircuitBreaker
	ShouldTrip BreakerDecider
}

BreakerRoundTripper wraps a base transport with a CircuitBreaker.

func (*BreakerRoundTripper) RoundTrip

func (b *BreakerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the request with circuit breaker protection.

type CircuitBreaker

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

CircuitBreaker implements a simple failure-based breaker.

func NewCircuitBreaker

func NewCircuitBreaker(options CircuitBreakerOptions) *CircuitBreaker

NewCircuitBreaker builds a CircuitBreaker with defaults.

func (*CircuitBreaker) Allow

func (c *CircuitBreaker) Allow() error

Allow returns ErrCircuitOpen when the breaker is open.

func (*CircuitBreaker) Record

func (c *CircuitBreaker) Record(success bool)

Record reports the success/failure of a request.

type CircuitBreakerOptions

type CircuitBreakerOptions struct {
	MaxFailures   int
	ResetTimeout  time.Duration
	Now           func() time.Time
	OnStateChange func(state string)
}

CircuitBreakerOptions configures a CircuitBreaker.

type ClientOptions

type ClientOptions struct {
	Timeout           time.Duration
	Transport         http.RoundTripper
	Retry             RetryOptions
	Breaker           *CircuitBreaker
	ShouldTrip        BreakerDecider
	PropagateMetadata bool
}

ClientOptions configures a default HTTP client.

func DefaultClientOptions

func DefaultClientOptions() ClientOptions

DefaultClientOptions returns a baseline client configuration.

type MetadataRoundTripper

type MetadataRoundTripper struct {
	Base http.RoundTripper
}

MetadataRoundTripper injects request metadata headers into outgoing requests.

func (*MetadataRoundTripper) RoundTrip

func (m *MetadataRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the request with metadata propagation.

type RetryDecider

type RetryDecider func(req *http.Request, resp *http.Response, err error) bool

RetryDecider decides whether a request should be retried.

type RetryOptions

type RetryOptions struct {
	MaxRetries int
	Backoff    BackoffFunc
	RetryIf    RetryDecider
	OnRetry    func(attempt int, err error, resp *http.Response)
}

RetryOptions configures retry behavior.

func DefaultRetryOptions

func DefaultRetryOptions() RetryOptions

DefaultRetryOptions returns a retry configuration with exponential backoff.

type RetryRoundTripper

type RetryRoundTripper struct {
	Base    http.RoundTripper
	Options RetryOptions
	Sleep   func(time.Duration)
}

RetryRoundTripper retries requests based on RetryOptions.

func (*RetryRoundTripper) RoundTrip

func (r *RetryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the request with retry behavior.

Jump to

Keyboard shortcuts

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