Skip to content

Commit

Permalink
use single field instead of two fields
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Jan 26, 2022
1 parent 5465428 commit d156b7d
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions client/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ var (

// ConsecCircuitBreaker is window sliding CircuitBreaker with failure threshold.
type ConsecCircuitBreaker struct {
// time.Time is a compund type, split into second and nano for using atomic.
lastFailureTimeSecond int64
lastFailureTimeNano int32
lastFailureTime int64

failures uint64
failureThreshold uint64
Expand Down Expand Up @@ -67,7 +65,7 @@ func (cb *ConsecCircuitBreaker) Call(fn func() error, d time.Duration) error {
}

func (cb *ConsecCircuitBreaker) ready() bool {
lastFailureTime := cb.loadLastFailureTime()
lastFailureTime := time.Unix(0, atomic.LoadInt64(&cb.lastFailureTime))
if time.Since(lastFailureTime) > cb.window {
cb.reset()
return true
Expand All @@ -80,14 +78,16 @@ func (cb *ConsecCircuitBreaker) ready() bool {
func (cb *ConsecCircuitBreaker) success() {
cb.reset()
}

func (cb *ConsecCircuitBreaker) fail() {
atomic.AddUint64(&cb.failures, 1)
cb.updateLastFailureTime(time.Now())
atomic.StoreInt64(&cb.lastFailureTime, time.Now().UnixNano())
}

func (cb *ConsecCircuitBreaker) Success() {
cb.success()
}

func (cb *ConsecCircuitBreaker) Fail() {
cb.fail()
}
Expand All @@ -98,15 +98,5 @@ func (cb *ConsecCircuitBreaker) Ready() bool {

func (cb *ConsecCircuitBreaker) reset() {
atomic.StoreUint64(&cb.failures, 0)
cb.updateLastFailureTime(time.Now())
}

func (cb *ConsecCircuitBreaker) updateLastFailureTime(cur time.Time) {
atomic.StoreInt64(&cb.lastFailureTimeSecond, cur.Unix())
atomic.StoreInt32(&cb.lastFailureTimeNano, int32(cur.UnixNano()))
}
func (cb *ConsecCircuitBreaker) loadLastFailureTime() time.Time {
nano := atomic.LoadInt32(&cb.lastFailureTimeNano)
second := atomic.LoadInt64(&cb.lastFailureTimeSecond)
return time.Unix(second, int64(nano))
atomic.StoreInt64(&cb.lastFailureTime, time.Now().UnixNano())
}

0 comments on commit d156b7d

Please sign in to comment.