Documentation
¶
Index ¶
- Variables
- func DefaultBreakerDecider(req *http.Request, resp *http.Response, err error) bool
- func DefaultRetryDecider(req *http.Request, resp *http.Response, err error) bool
- func NewClient(options ClientOptions) *http.Client
- type BackoffFunc
- type BreakerDecider
- type BreakerRoundTripper
- type CircuitBreaker
- type CircuitBreakerOptions
- type ClientOptions
- type MetadataRoundTripper
- type RetryDecider
- type RetryOptions
- type RetryRoundTripper
Constants ¶
This section is empty.
Variables ¶
var ErrBodyNotReplayable = errors.New("request body is not replayable")
ErrBodyNotReplayable indicates a request body cannot be retried.
var ErrCircuitOpen = errors.New("circuit breaker open")
ErrCircuitOpen indicates the circuit breaker is open.
Functions ¶
func DefaultBreakerDecider ¶
DefaultBreakerDecider trips on network errors or 5xx responses.
func DefaultRetryDecider ¶
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 ¶
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 ¶
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.
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.
type RetryDecider ¶
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.