Documentation
¶
Overview ¶
Package breaker implements the Circuit Breaker design pattern.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOpenState is the error returned when the Breaker is open. ErrOpenState = errors.New("breaker: circuit breaker is open") // ErrTooManyRequests is the error returned when too many test requests are // made when the Breaker is half-open. ErrTooManyRequests = errors.New("breaker: too many requests") )
Functions ¶
This section is empty.
Types ¶
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is a circuit breaker.
Example ¶
package main
import (
"time"
"github.com/hamba/pkg/breaker"
)
func main() {
b := breaker.NewBreaker(breaker.ThresholdFuse(1), breaker.WithSleep(100*time.Millisecond))
err := b.Run(func() error {
// Your code protected by the circuit breaker...
return nil // Return any errors
})
if err != nil {
// Handle the error
}
}
func NewBreaker ¶
NewBreaker creates a new Breaker.
type Counter ¶
type Counter struct {
// Requests is the total number of requests made.
Requests uint64
// Successes is the total number of successes returned.
Successes uint64
// Failures is the total number of failures returned.
Failures uint64
// ConsecutiveSuccesses is the number of consecutive successes returned.
ConsecutiveSuccesses uint64
// ConsecutiveFailures is the number of consecutive failures returned.
ConsecutiveFailures uint64
}
Counter holds the number number of requests, successes and failures of a breaker.
Counter is reset from time to time, and totals should not be used as a full totals.
type FuseFunc ¶
FuseFunc is an adapter allowing to use a function as a Fuse.
func ConsecutiveFuse ¶
ConsecutiveFuse trips the Breaker when the consecutive number of failures exceeds the given count.
func RateFuse ¶
RateFuse trips the Breaker when the percentage of failures exceeds the given rate.
rate should be between 0 and 100.
func ThresholdFuse ¶
ThresholdFuse trips the Breaker when the total number of failures exceeds the given count.