Documentation
¶
Overview ¶
Package retrykit provides retry with backoff and circuit breaker for Go.
Index ¶
- func Call[T any](cb *CircuitBreaker, fn func() (T, error)) (T, error)
- func Do[T any](ctx context.Context, fn func(ctx context.Context) (T, error), opts ...Option) (T, error)
- type Backoff
- type CircuitBreaker
- type CircuitBreakerOption
- func WithFailureThreshold(n int) CircuitBreakerOption
- func WithHalfOpenMaxAttempts(n int) CircuitBreakerOption
- func WithOnCircuitOpen(fn func(failures int)) CircuitBreakerOption
- func WithOnStateChange(fn func(from, to CircuitState)) CircuitBreakerOption
- func WithResetTimeout(d time.Duration) CircuitBreakerOption
- type CircuitOpenError
- type CircuitState
- type Option
- func Aggressive() []Option
- func DatabaseQuery() []Option
- func Gentle() []Option
- func NetworkRequest() []Option
- func WithBackoff(b Backoff) Option
- func WithInitialDelay(d time.Duration) Option
- func WithJitter(j bool) Option
- func WithMaxAttempts(n int) Option
- func WithMaxDelay(d time.Duration) Option
- func WithOnFailure(fn func(err error, attempts int)) Option
- func WithOnRetry(fn func(error, int)) Option
- func WithOnSuccess(fn func(attempt int)) Option
- func WithRetryOn(fn func(error) bool) Option
- type Options
- type RetryError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern.
func NewCircuitBreaker ¶
func NewCircuitBreaker(opts ...CircuitBreakerOption) *CircuitBreaker
NewCircuitBreaker creates a new CircuitBreaker with the given options.
func (*CircuitBreaker) Reset ¶ added in v0.3.0
func (cb *CircuitBreaker) Reset()
Reset manually resets the circuit breaker to the Closed state with zero failures.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() CircuitState
State returns the current circuit state.
func (*CircuitBreaker) String ¶
func (cb *CircuitBreaker) String() string
String returns a human-readable description of the circuit breaker state.
type CircuitBreakerOption ¶
type CircuitBreakerOption func(*CircuitBreaker)
CircuitBreakerOption configures a CircuitBreaker.
func WithFailureThreshold ¶
func WithFailureThreshold(n int) CircuitBreakerOption
WithFailureThreshold sets the number of failures before opening the circuit.
func WithHalfOpenMaxAttempts ¶
func WithHalfOpenMaxAttempts(n int) CircuitBreakerOption
WithHalfOpenMaxAttempts sets the max attempts allowed in half-open state.
func WithOnCircuitOpen ¶
func WithOnCircuitOpen(fn func(failures int)) CircuitBreakerOption
WithOnCircuitOpen sets a callback when the circuit opens.
func WithOnStateChange ¶
func WithOnStateChange(fn func(from, to CircuitState)) CircuitBreakerOption
WithOnStateChange sets a callback for state transitions.
func WithResetTimeout ¶
func WithResetTimeout(d time.Duration) CircuitBreakerOption
WithResetTimeout sets how long to wait before transitioning from open to half-open.
type CircuitOpenError ¶
type CircuitOpenError struct{}
CircuitOpenError is returned when the circuit breaker is open.
func (*CircuitOpenError) Error ¶
func (e *CircuitOpenError) Error() string
type CircuitState ¶
type CircuitState int
CircuitState represents the state of the circuit breaker.
const ( Closed CircuitState = iota Open HalfOpen )
func (CircuitState) String ¶
func (s CircuitState) String() string
type Option ¶
type Option func(*Options)
Option is a functional option for configuring retry behavior.
func Aggressive ¶
func Aggressive() []Option
Aggressive returns options for aggressive retry (5 attempts, fast backoff).
func DatabaseQuery ¶
func DatabaseQuery() []Option
DatabaseQuery returns options suited for database queries.
func Gentle ¶
func Gentle() []Option
Gentle returns options for gentle retry (3 attempts, slow backoff).
func NetworkRequest ¶
func NetworkRequest() []Option
NetworkRequest returns options suited for network requests.
func WithInitialDelay ¶
WithInitialDelay sets the initial delay between retries.
func WithMaxAttempts ¶
WithMaxAttempts sets the maximum number of attempts.
func WithMaxDelay ¶
WithMaxDelay sets the maximum delay between retries.
func WithOnFailure ¶ added in v0.2.0
WithOnFailure sets a callback invoked when all attempts have been exhausted.
func WithOnRetry ¶
WithOnRetry sets a callback invoked before each retry.
func WithOnSuccess ¶ added in v0.2.0
WithOnSuccess sets a callback invoked when the operation succeeds.
func WithRetryOn ¶
WithRetryOn sets a function to determine if an error should be retried.
type Options ¶
type Options struct {
MaxAttempts int
Backoff Backoff
InitialDelay time.Duration
MaxDelay time.Duration
Jitter bool
RetryOn func(error) bool
OnRetry func(err error, attempt int)
OnSuccess func(attempt int)
OnFailure func(err error, attempts int)
}
Options configures retry behavior.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns sensible default retry options.
type RetryError ¶
RetryError is returned when all attempts have been exhausted.
func (*RetryError) Error ¶
func (e *RetryError) Error() string
func (*RetryError) Unwrap ¶
func (e *RetryError) Unwrap() error