Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Circuit ¶
type Circuit struct {
// contains filtered or unexported fields
}
Circuit is an error rate based circuit breaker implementation which uses sliding window of buckets to maintain the counters of requests. It implements the core APIs of the circuit breaker to check if the request is allowed and accepts the status of a request, which is used to decide to transition between states(healthy, unhealthy, and probing) based on the success and failure ratio of the requests.
func NewCircuit ¶
NewCircuit returns a circuit breaker based on the provided config. Returns an error if the provided config is invalid.
func (*Circuit) IsRequestAllowed ¶
IsRequestAllowed returns true if the request can be processed. Clients must invoke this method before processing a request and must continue processing request only when this method returns true. This method is safe for concurrent use.
func (*Circuit) ReportRequestStatus ¶
ReportRequestStatus must be invoked to report the request status where true means it was a successful request. This increments the counters of success or failure requests and processes the state to check the failure ratio and transition state if required. This method is safe for concurrent use.
type Circuiter ¶
type Circuiter interface {
// IsRequestAllowed returns true if the request can be processed. Clients
// must invoke this method before processing a request.
// Note: you must call ReportRequestStatus method to report of status of
// the request if the request was allowed by the circuit.
// This method is safe for concurrent use.
IsRequestAllowed() (allowed bool)
// ReportRequestStatus must be invoked to report the request status where
// true mean a successful request.
// This method will check the current failure rate and transition to Unhealthy
// or Probing state if needed.
// This method is safe for concurrent use.
ReportRequestStatus(requestStatus bool)
// Status returns the internal circuit status.
// This method is safe for concurrent use.
Status() *Status
}
Circuiter is a minimal circuit breaker interface where users can check if the request processing is allowed and report the status of the processed request.
type Config ¶
type Config struct {
// WindowSize defines the number of buckets in the sliding window.
WindowSize int `yaml:"windowSize"`
// BucketDuration defines the time duration of a bucket in the sliding window.
BucketDuration time.Duration `yaml:"bucketDuration"`
// RecoveryTime defines minimum amount of time duration circuit must be in the
// Unhealthy state before transitioning to Probing state.
RecoveryTime time.Duration `yaml:"recoveryTime"`
// Jitter defines the additional duration on top of the recovery timeout where
// the circuit must be in Unhealth state. A random value between 0 to Jitter
// will be applied on top of recovery time.
Jitter time.Duration `yaml:"jitter"`
// MaxProbeTime defines the maximum time duration a circuit can stay in Probing state.
MaxProbeTime time.Duration `yaml:"maxProbeTime"`
// MinimumRequests defines the minimum number of requests a Healthy circuit
// must be reported in the entire window before circuit checks the failure
// rate.
MinimumRequests int64 `yaml:"minimumRequests"`
// FailureRatio defines the ratio at which circuit must transition to Unhealthy state.
// The value ranges from [0.0, 1.0] where 1.0 means 100%.
FailureRatio float64 `yaml:"failureRatio"`
// ProbeRatios defines the list of RPS ratios to be probed during Probing state.
// The values in the list must be sorted in the increasing order..
// For instance when the average RPS in the window is 1000rps and a ratio of 0.1
// means probing 100requests (0.1 * 1000).
ProbeRatios []float64 `yaml:"probeRatios"`
// MinimumProbeRequests defines the minimum number of requests a Probing circuit
// must be reported in the entire window before circuit checks the failure rate
// to either move to next probe ratio or transition to Healthy state.
MinimumProbeRequests int64 `yaml:"minimumProbeRequests"`
}
Config represents configuration values for the circuit breaker.
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status represents the current state of the circuit and provides methods to transition between different states.
func (*Status) ProbeRatio ¶
ProbeRatio returns the probe ratio at which the circuit is currently Probing. It returns false in second return value when a valid probe index is not set. This method is safe for concurrent use.
func (*Status) ProbeTimeout ¶
ProbeTimeout returns the maximum time the circuit will stay in Probing state. This method is safe for concurrent use.
func (*Status) RecoveryTimeout ¶
RecoveryTimeout returns the time at which the circuit transitions from Unhealthy state to Probing state. This method is safe for concurrent use.