Documentation
¶
Index ¶
- Variables
- type BreakerConfig
- type BreakerConfigFunc
- func WithCollector(collector MetricCollector) BreakerConfigFunc
- func WithFailureInterpreter(failureInterpreter FailureInterpreter) BreakerConfigFunc
- func WithHalfClosedRetryProbability(probability float64) BreakerConfigFunc
- func WithInvocationTimeout(timeout time.Duration) BreakerConfigFunc
- func WithMaxConcurrency(maxConcurrency int) BreakerConfigFunc
- func WithMaxConcurrencyTimeout(timeout time.Duration) BreakerConfigFunc
- func WithResetBackoff(resetBackoff backoff.Backoff) BreakerConfigFunc
- func WithTripCondition(tripCondition TripCondition) BreakerConfigFunc
- type BreakerFunc
- type CircuitBreaker
- type CircuitState
- type ConsecutiveFailureTripCondition
- type EventType
- type FailureInterpreter
- type FailureInterpreterFunc
- type FallbackFunc
- type MetricCollector
- type MultiCollector
- type NamedMetricCollector
- type NoopBreaker
- type NoopCollector
- type PercentageFailureTripCondition
- type Registry
- type TripCondition
- type WindowFailureTripCondition
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCircuitOpen occurs when the Call method fails immediately. ErrCircuitOpen = fmt.Errorf("circuit is open") // ErrInvocationTimeout occurs when the method takes too long to execute. ErrInvocationTimeout = fmt.Errorf("invocation has timed out") )
Functions ¶
This section is empty.
Types ¶
type BreakerConfig ¶
type BreakerConfig struct {
MaxConcurrency int
}
BreakerConfig is a struct that contains a copy of some of a breaker's initialization values. This struct may grow as metric collectors track additional breaker state.
type BreakerConfigFunc ¶
type BreakerConfigFunc func(*circuitBreaker)
func WithCollector ¶
func WithCollector(collector MetricCollector) BreakerConfigFunc
func WithFailureInterpreter ¶
func WithFailureInterpreter(failureInterpreter FailureInterpreter) BreakerConfigFunc
func WithHalfClosedRetryProbability ¶
func WithHalfClosedRetryProbability(probability float64) BreakerConfigFunc
func WithInvocationTimeout ¶
func WithInvocationTimeout(timeout time.Duration) BreakerConfigFunc
func WithMaxConcurrency ¶
func WithMaxConcurrency(maxConcurrency int) BreakerConfigFunc
func WithMaxConcurrencyTimeout ¶
func WithMaxConcurrencyTimeout(timeout time.Duration) BreakerConfigFunc
func WithResetBackoff ¶
func WithResetBackoff(resetBackoff backoff.Backoff) BreakerConfigFunc
func WithTripCondition ¶
func WithTripCondition(tripCondition TripCondition) BreakerConfigFunc
type BreakerFunc ¶
type CircuitBreaker ¶
type CircuitBreaker interface { // Trip manually trips the circuit breaker. The circuit breaker will remain open // until it is manually reset. Trip() // Reset the circuit breaker. Reset() // ShouldTry returns true if the circuit breaker is closed or half-closed with // some probability. Successive calls to this method may yield different results // depending on the registered trip condition. ShouldTry() bool // MarkResult takes the result of the protected section and marks it as a success if // the error is nil or if the failure interpreter decides not to trip on this error. MarkResult(err error) bool // Call attempts to call the given function if the circuit breaker is closed, or if // the circuit breaker is half-closed (with some probability). Otherwise, return an // ErrCircuitOpen. If the function times out, the circuit breaker will fail with an // ErrInvocationTimeout. If the function is invoked and yields a value before the // timeout elapses, that value is returned. Call(f BreakerFunc) error // CallAsync invokes the given function in a goroutine, returning a channel which // may receive one non-nil error value and then close. The channel will close without // writing a value on success. CallAsync(f BreakerFunc) <-chan error }
CircuitBreaker protects the invocation of a function and monitors failures. After a certain failure threshold is reached, future invocations will instead return an ErrErrCircuitOpen instead of attempting to invoke the function again.
func NewCircuitBreaker ¶
func NewCircuitBreaker(configs ...BreakerConfigFunc) CircuitBreaker
NewCircuitBreaker creates a new CircuitBreaker.
func NewNoopBreaker ¶
func NewNoopBreaker() CircuitBreaker
NewNoopBreaker creates a new non-tripping breaker.
type CircuitState ¶
type CircuitState int
const ( StateOpen CircuitState // Failure state StateClosed // Success state StateHalfClosed // Cautious, probabilistic retry state StateHardOpen // Forced failure state )
type ConsecutiveFailureTripCondition ¶
type ConsecutiveFailureTripCondition struct {
// contains filtered or unexported fields
}
ConsecutiveFailureTripCondition is a trip condition that trips the circuit breaker after a configurable number of failures occur in a row. A single successful call will break the failure chain.
func (*ConsecutiveFailureTripCondition) Failure ¶
func (tc *ConsecutiveFailureTripCondition) Failure()
func (*ConsecutiveFailureTripCondition) ShouldTrip ¶
func (tc *ConsecutiveFailureTripCondition) ShouldTrip() bool
func (*ConsecutiveFailureTripCondition) Success ¶
func (tc *ConsecutiveFailureTripCondition) Success()
type EventType ¶
type EventType int
EventType distinguishes interesting occurrences.
const ( // EventTypeAttempt occurs when Call or CallAsync is called. EventTypeAttempt EventType = iota // EventTypeSuccess occurs when a breaker func returns a nil error. EventTypeSuccess // EventTypeFailure occurs when a breaker func returns a non-nil error // or cannot be called due to breaker status or semaphore contention. EventTypeFailure // EventTypeError occurs when a breaker func returns a non-nil error // which is interpreted as an error against the breaker. EventTypeError // EventTypeBadRequest occurs when a breaker func returns a non-nil // error which is not interpreted as an error against the breaker. EventTypeBadRequest // EventTypeShortCircuit occurs when a circuit is open and no breaker // func is invoked. EventTypeShortCircuit // EventTypeTimeout occurs when execution of a breaker func times out. EventTypeTimeout // EventTypeRejection occurs when a breaker func cannot be invoked due // to semaphore contention. EventTypeRejection // EventTypeFallbackSuccess occurs when a fallback func returns a nil // error. EventTypeFallbackSuccess // EventTypeFallbackFailure occurs when a fallback func returns a non-nil // error. EventTypeFallbackFailure // EventTypeRunDuration marks the duration of a breaker func invocation. EventTypeRunDuration // EventTypeTotalDuration marks the duration of a Call or CallAsync method. EventTypeTotalDuration // EventTypeSemaphoreQueued occurs once a routine begins waiting for a // semaphore token. This event does not occur if a token is immediately // available. EventTypeSemaphoreQueued // EventTypeSemaphoreDequeued occurs once a routine stops waiting for a // semaphore token. This could be because the routine got a successful token, // or because the max timeout has elapsed. EventTypeSemaphoreDequeued // EventTypeSemaphoreAcquired occurs once a semaphore token is acquired and // the breaker func can be invoked. EventTypeSemaphoreAcquired // EventTypeSemaphoreReleased occurs after the breaker func is invoked. EventTypeSemaphoreReleased )
type FailureInterpreter ¶
type FailureInterpreter interface { // ShouldTrip determines if this error should cause a CircuitBreaker // to transition from an open to a closed state. ShouldTrip(error) bool }
FailureInterpreter is the interface that determines if an error should affect whether or not the circuit breaker will trip. This is useful if some errors are indictive of a system failure and others are not (e.g. HTTP 500 vs HTTP 400 responses).
func NewAnyErrorFailureInterpreter ¶
func NewAnyErrorFailureInterpreter() FailureInterpreter
NewAnyErrorFailureInterpreter creates a failure interpreter that trips on every error.
type FailureInterpreterFunc ¶
func (FailureInterpreterFunc) ShouldTrip ¶
func (f FailureInterpreterFunc) ShouldTrip(err error) bool
type FallbackFunc ¶
type MetricCollector ¶
type MetricCollector interface { // ReportNew fires when a breaker is first initialized so the collector // can track the immutable config of circuit breakers. ReportNew(BreakerConfig) // ReportCount fires each time a non-latency event is emitted. ReportCount(EventType) // ReportDuration fires on latency events with the time spent inside a // code path of-interest (inside a breaker func or inside a Call func). ReportDuration(EventType, time.Duration) // ReportState fires when a breaker changes state. ReportState(CircuitState) }
func NamedCollector ¶
func NamedCollector(name string, collector NamedMetricCollector) MetricCollector
NamedCollector converts a named metric collector into a metric collector. The name given to this constructor will be sent as the first argument to all of the named metric collector methods.
func NewMultiCollector ¶
func NewMultiCollector(collectors ...MetricCollector) MetricCollector
NewMultiCollector creates a new MultiCollector.
func NewNoopCollector ¶
func NewNoopCollector() MetricCollector
NewNoopCollector creates a new do-nothing collector.
type MultiCollector ¶
type MultiCollector struct {
// contains filtered or unexported fields
}
MultiCollector is a metric collector that wraps several other collector instances. Each event will be passed to every backend in the registered order.
func (*MultiCollector) ReportCount ¶
func (c *MultiCollector) ReportCount(eventType EventType)
func (*MultiCollector) ReportDuration ¶
func (c *MultiCollector) ReportDuration(eventType EventType, duration time.Duration)
func (*MultiCollector) ReportNew ¶
func (c *MultiCollector) ReportNew(config BreakerConfig)
func (*MultiCollector) ReportState ¶
func (c *MultiCollector) ReportState(state CircuitState)
type NamedMetricCollector ¶
type NamedMetricCollector interface { // ReportNew is MetricCollector.ReportNew with the name of the breaker // passed in as a first argument. ReportNew(string, BreakerConfig) // ReportCount is MetricCollector.ReportCount with the name of the breaker // passed in as a first argument. ReportCount(string, EventType) // ReportDuration is MetricCollector.ReportDuration with the name of the breaker // passed in as a first argument. ReportDuration(string, EventType, time.Duration) // ReportState is MetricCollector.ReportState with the name of the breaker // passed in as a first argument. ReportState(string, CircuitState) }
type NoopBreaker ¶
type NoopBreaker struct{}
NoopBreaker is a circuit breaker that never trips.
func (*NoopBreaker) Call ¶
func (b *NoopBreaker) Call(f BreakerFunc) error
func (*NoopBreaker) CallAsync ¶
func (b *NoopBreaker) CallAsync(f BreakerFunc) <-chan error
func (*NoopBreaker) MarkResult ¶
func (b *NoopBreaker) MarkResult(err error) bool
func (*NoopBreaker) Reset ¶
func (b *NoopBreaker) Reset()
func (*NoopBreaker) ShouldTry ¶
func (b *NoopBreaker) ShouldTry() bool
func (*NoopBreaker) Trip ¶
func (b *NoopBreaker) Trip()
type NoopCollector ¶
type NoopCollector struct{}
NoopCollector is a metric collector that does nothing.
func (*NoopCollector) ReportCount ¶
func (c *NoopCollector) ReportCount(EventType)
func (*NoopCollector) ReportDuration ¶
func (c *NoopCollector) ReportDuration(EventType, time.Duration)
func (*NoopCollector) ReportNew ¶
func (c *NoopCollector) ReportNew(BreakerConfig)
func (*NoopCollector) ReportState ¶
func (c *NoopCollector) ReportState(CircuitState)
type PercentageFailureTripCondition ¶
type PercentageFailureTripCondition struct {
// contains filtered or unexported fields
}
PercentageFailureTripCondition is a trip condition that trips the circuit breaker after the number of failures for the last fixed number of attempts exceeds a percentage threshold.
func (*PercentageFailureTripCondition) Failure ¶
func (tc *PercentageFailureTripCondition) Failure()
func (*PercentageFailureTripCondition) ShouldTrip ¶
func (tc *PercentageFailureTripCondition) ShouldTrip() bool
func (*PercentageFailureTripCondition) Success ¶
func (tc *PercentageFailureTripCondition) Success()
type Registry ¶
type Registry interface { // Configure will register a new breaker instance under the given name using // the given configuration. A breaker's configuration may not be changed after // being initialized. It is an error to register the same breaker twice, or try // to invoke Call or CallAsync with an unregistered breaker. Configure(name string, configs ...BreakerConfigFunc) error // Call will invoke `Call` on the breaker configured with the given name. If // the breaker returns a non-nil error, the fallback function is invoked with // the error as the value. It may be the case that the fallback function is // invoked without the breaker function failing (e.g. circuit open). Call(name string, f BreakerFunc, fallback FallbackFunc) error // CallAsync will create a channel that receives the error value from an similar // invocation of Call. See the Breaker docs for more details. CallAsync(name string, f BreakerFunc, fallback FallbackFunc) <-chan error }
func NewRegistry ¶
func NewRegistry() Registry
type TripCondition ¶
type TripCondition interface { // Invoked when the a call to a service completes successfully (note - all // successful calls, not just ones immediately after a failure period), or // when the breaker is hard reset. Stats should be reset at this point. Success() // Invoked when the circuit breaker was closed or half-closed and failure // occurs, or when the breaker is hard-tripped. Stats should be collected // at this point. Failure() // Determines the state of the breaker - returns true or false for open and // closed state, respectively. ShouldTrip() bool }
TripCondition is the interface that controls the open/closed state of the circuit breaker based on failure history.
func NewConsecutiveFailureTripCondition ¶
func NewConsecutiveFailureTripCondition(threshold int) TripCondition
NewConsecutiveFailureTripCondition creates a ConsecutiveFailureTripCondition.
func NewPercentageFailureTripCondition ¶
func NewPercentageFailureTripCondition(window int, threshold float64) TripCondition
NewPercentageFailureFailureTripCondition creates a PercentageFailureTripCondition.
func NewWindowFailureTripCondition ¶
func NewWindowFailureTripCondition(window time.Duration, threshold int) TripCondition
NewWindowFailureTripCondition creates a WindowFailureTripCondition.
type WindowFailureTripCondition ¶
type WindowFailureTripCondition struct {
// contains filtered or unexported fields
}
WindowFailureTripCondition is a trip condition that trips the circuit breaker after a configurable number of failures occur within a configurable rolling window. The circuit breaker will remain in the open state until the time has elapsed for a failure to fall out of recent memory.
func (*WindowFailureTripCondition) Failure ¶
func (tc *WindowFailureTripCondition) Failure()
func (*WindowFailureTripCondition) ShouldTrip ¶
func (tc *WindowFailureTripCondition) ShouldTrip() bool
func (*WindowFailureTripCondition) Success ¶
func (tc *WindowFailureTripCondition) Success()