Documentation ¶
Index ¶
- Variables
- func DefaultReadyToTrip(counts Counts) bool
- type Agent
- type CircuitBreaker
- func (cb *CircuitBreaker) Do(req func() error) func() error
- func (cb *CircuitBreaker) SetInterval(interval time.Duration)
- func (cb *CircuitBreaker) SetMaxRequests(maxRequests uint32)
- func (cb *CircuitBreaker) SetOnStateChange(onStateChange func(from State, to State))
- func (cb *CircuitBreaker) SetReadyToTrip(readyToTrip func(counts Counts) bool)
- func (cb *CircuitBreaker) SetTimeout(timeout time.Duration)
- type Counts
- type OnRetryFunc
- type Retry
- type RetryIfFunc
- type RetryableFunc
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTooManyRequests is returned when the CB state is half open and the requests count is over // the cb maxRequests ErrTooManyRequests = errors.New("too many requests") // ErrOpenState is returned when the CB state is open ErrOpenState = errors.New("circuit breaker is open") )
var ( // ErrRequestFailed is returned when a request has reached the maximum number of retries, and still fails. ErrRequestFailed = errors.New("RequestFailed") )
Functions ¶
func DefaultReadyToTrip ¶
DefaultReadyToTrip is the default readyToTrip function, which returns true if the ConsecutiveFailures count is 5 or more, and false otherwise
Types ¶
type Agent ¶
type Agent struct { CircuitBreaker *CircuitBreaker Retry *Retry }
Agent is a struct used to make more resilient requests. It has the possibility to implement both the circuitbreaker and the retry stability pattern
func (Agent) Do ¶
Do is the main function of Agent and executes the request using the circuit breaker and/or the retry if they exist.
func (*Agent) SetCircuitBreaker ¶
func (agent *Agent) SetCircuitBreaker(circuitBreaker *CircuitBreaker)
SetCircuitBreaker sets the CircuitBreaker of the Agent
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker is a state machine to prevent sending requests that are likely to fail.
func NewCircuitBreaker ¶
func NewCircuitBreaker(name string) *CircuitBreaker
NewCircuitBreaker functions as a constructor for CircuitBreaker that returns a new instance of CircuitBreaker
func (*CircuitBreaker) Do ¶
func (cb *CircuitBreaker) Do(req func() error) func() error
Do executes the given request if the CircuitBreaker accepts it. Do returns an error instantly if the CircuitBreaker rejects the request. Otherwise, Do returns the result of the request. If a panic occurs in the request, the CircuitBreaker handles it as a failed request and causes the same panic again.
func (*CircuitBreaker) SetInterval ¶
func (cb *CircuitBreaker) SetInterval(interval time.Duration)
SetInterval sets the cyclic period of the closed state for the CircuitBreaker to clear the internal Counts. If set to zero the internal counts will not be cleared.
func (*CircuitBreaker) SetMaxRequests ¶
func (cb *CircuitBreaker) SetMaxRequests(maxRequests uint32)
SetMaxRequests the maximum amount of requests allowed while the circuitbreaker is in half-open state
func (*CircuitBreaker) SetOnStateChange ¶
func (cb *CircuitBreaker) SetOnStateChange(onStateChange func(from State, to State))
SetOnStateChange sets the function called when the CircuitBreaker changes state
func (*CircuitBreaker) SetReadyToTrip ¶
func (cb *CircuitBreaker) SetReadyToTrip(readyToTrip func(counts Counts) bool)
SetReadyToTrip sets the function for determining whether the circuitbreaker is ready to transition into open state.
func (*CircuitBreaker) SetTimeout ¶
func (cb *CircuitBreaker) SetTimeout(timeout time.Duration)
SetTimeout sets the period of the open state, after which the state of the CircuitBreaker becomes half-open
type Counts ¶
type Counts struct { Requests uint32 TotalSuccesses uint32 TotalFailures uint32 ConsecutiveSuccesses uint32 ConsecutiveFailures uint32 }
Counts holds the numbers of requests and their successes/failures. CircuitBreaker clears the internal Counts either on the change of the state or at the closed-state intervals. Counts ignores the results of the requests sent before clearing.
type OnRetryFunc ¶
OnRetryFunc is the function signature of the type of function that can be called on retries.
type Retry ¶
type Retry struct {
// contains filtered or unexported fields
}
Retry is a struct representing the retry functionality.
func (*Retry) Do ¶
func (retry *Retry) Do(retryableFunc RetryableFunc) func() error
Do is the main method of Retry and executes and retries the retryable function
func (*Retry) SetAttempts ¶
SetAttempts is a method for setting the amount of attempts for Retry. If set to 0, there will be no limit to the amount of retries.
func (*Retry) SetOnRetry ¶
SetOnRetry is a method for setting the onRetry function of Retry.
func (*Retry) SetRetryIf ¶
SetRetryIf is a method for setting the retryIf function of Retry.
type RetryIfFunc ¶
RetryIfFunc is the function signature of the type of function that can be used to determine whether or not the retryable function should be retried upon failure.
type RetryableFunc ¶
type RetryableFunc func() error
RetryableFunc is the function signature of a function that is retryable by this library