Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current time setting #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions gobreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (c *Counts) clear() {
// If IsSuccessful returns true, the error is counted as a success.
// Otherwise the error is counted as a failure.
// If IsSuccessful is nil, default IsSuccessful is used, which returns false for all non-nil errors.
//
// Now is called whenever the current time is requested.
// Default Now returns time.Now().
type Settings struct {
Name string
MaxRequests uint32
Expand All @@ -111,6 +114,7 @@ type Settings struct {
ReadyToTrip func(counts Counts) bool
OnStateChange func(name string, from State, to State)
IsSuccessful func(err error) bool
Now func() time.Time
}

// CircuitBreaker is a state machine to prevent sending requests that are likely to fail.
Expand All @@ -122,6 +126,7 @@ type CircuitBreaker struct {
readyToTrip func(counts Counts) bool
isSuccessful func(err error) bool
onStateChange func(name string, from State, to State)
now func() time.Time

mutex sync.Mutex
state State
Expand Down Expand Up @@ -174,7 +179,13 @@ func NewCircuitBreaker(st Settings) *CircuitBreaker {
cb.isSuccessful = st.IsSuccessful
}

cb.toNewGeneration(time.Now())
if st.Now == nil {
cb.now = time.Now
} else {
cb.now = st.Now
}

cb.toNewGeneration(cb.now())

return cb
}
Expand Down Expand Up @@ -207,7 +218,7 @@ func (cb *CircuitBreaker) State() State {
cb.mutex.Lock()
defer cb.mutex.Unlock()

now := time.Now()
now := cb.now()
state, _ := cb.currentState(now)
return state
}
Expand Down Expand Up @@ -277,7 +288,7 @@ func (cb *CircuitBreaker) beforeRequest() (uint64, error) {
cb.mutex.Lock()
defer cb.mutex.Unlock()

now := time.Now()
now := cb.now()
state, generation := cb.currentState(now)

if state == StateOpen {
Expand All @@ -294,7 +305,7 @@ func (cb *CircuitBreaker) afterRequest(before uint64, success bool) {
cb.mutex.Lock()
defer cb.mutex.Unlock()

now := time.Now()
now := cb.now()
state, generation := cb.currentState(now)
if generation != before {
return
Expand Down
8 changes: 6 additions & 2 deletions gobreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type StateChange struct {
var stateChange StateChange

func pseudoSleep(cb *CircuitBreaker, period time.Duration) {
if !cb.expiry.IsZero() {
cb.expiry = cb.expiry.Add(-period)
prevPeriod := cb.now().Sub(time.Now())
cb.now = func() time.Time {
return time.Now().Add(prevPeriod + period)
}
}

Expand Down Expand Up @@ -94,6 +95,9 @@ func newCustom() *CircuitBreaker {
customSt.OnStateChange = func(name string, from State, to State) {
stateChange = StateChange{name, from, to}
}
customSt.Now = func() time.Time {
return time.Now()
}

return NewCircuitBreaker(customSt)
}
Expand Down