fastbreaker implements the Circuit Breaker pattern in Go.
go get github.com/bluekiri/fastbreaker
The interface fastbreaker.FastBreaker
is a state machine to prevent sending requests that are likely to fail.
The function fastbreaker.New
creates a new fastbreaker.FastBreaker
.
func fastbreaker.New(configuration fastbreaker.Configuration) fastbreaker.FastBreaker
You can configure fastbreaker.FastBreaker
by the struct fastbreaker.Configuration
:
type Configuration struct {
NumBuckets int
BucketDuration time.Duration
DurationOfBreak time.Duration
ShouldTrip ShouldTripFunc
}
-
NumBuckets
is the number of buckets of the rolling window. IfNumBuckets
is less than 1, thefastbreaker.DefaultNumBuckets
is used. -
BucketDuration
is the duration (truncated to the second) of every bucket. IfBucketDuration
is less than 1s, thefastbreaker.DefaultBucketDuration
is used. -
DurationOfBreak
is the time (truncated to the second) of the open state, after which the state becomes half-open. IfDurationOfBreak
is less than 1s, thefastbreaker.DefaultDurationOfBreak
is used. -
ShouldTrip
is called whenever a request fails in the closed state with the number of executions and the number of failures. IfShouldTrip
returns true,fastbreaker.FastBreaker
state becomes open. IfShouldTrip
isnil
,fastbreaker.DefaultShouldTrip
is used.fastbreaker.DefaultShouldTrip
returns true when the number of executions is greater than or equal to 10 and at least half the number of executions have failed.
var cb fastbreaker.FastBreaker
func Get(url string) ([]byte, error) {
feedback, err := cb.Allow()
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
feedback(false)
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
feedback(false)
return nil, err
}
feedback(true)
return body, nil
}
The MIT License (MIT)
See LICENSE for details.