Skip to content

Commit

Permalink
Merge pull request #40 from bytedance/release/20210525
Browse files Browse the repository at this point in the history
release: 20210525
  • Loading branch information
PureWhiteWu authored May 25, 2021
2 parents 4226cae + c588cac commit d620a70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions cloud/circuitbreaker/breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ type breaker struct {

options Options

now func() time.Time // for test
now func() time.Time // Default value is time.Now, caller may use some high-performance custom time now func here
}

// newBreaker creates a base breaker with a specified options
func newBreaker(options Options) (*breaker, error) {
if options.now == nil {
options.now = time.Now
if options.Now == nil {
options.Now = time.Now
}

if options.BucketTime <= 0 {
Expand Down Expand Up @@ -90,7 +90,7 @@ func newBreaker(options Options) (*breaker, error) {
breaker := &breaker{
rw: syncx.NewRWMutex(),
metricer: window,
now: options.now,
now: options.Now,
state: Closed,
}

Expand All @@ -103,7 +103,7 @@ func newBreaker(options Options) (*breaker, error) {
ShouldTrip: options.ShouldTrip,
ShouldTripWithKey: options.ShouldTripWithKey,
BreakerStateChangeHandler: options.BreakerStateChangeHandler,
now: options.now,
Now: options.Now,
}

return breaker, nil
Expand Down Expand Up @@ -157,7 +157,7 @@ func (b *breaker) error(isTimeout bool, trip TripFunc) {
if b.options.BreakerStateChangeHandler != nil {
go b.options.BreakerStateChangeHandler(HalfOpen, Open, b.metricer)
}
b.openTime = time.Now()
b.openTime = b.now()
atomic.StoreInt32((*int32)(&b.state), int32(Open))
}
b.rw.Unlock()
Expand All @@ -170,7 +170,7 @@ func (b *breaker) error(isTimeout bool, trip TripFunc) {
if b.options.BreakerStateChangeHandler != nil {
go b.options.BreakerStateChangeHandler(Closed, Open, b.metricer)
}
b.openTime = time.Now()
b.openTime = b.now()
atomic.StoreInt32((*int32)(&b.state), int32(Open))
}
b.rw.Unlock()
Expand Down Expand Up @@ -211,7 +211,7 @@ func (b *breaker) isAllowed() bool {
rwx.Lock()
switch b.State() {
case Open:
now := time.Now()
now := b.now()
if b.openTime.Add(b.options.CoolingTimeout).After(now) {
rwx.Unlock()
return false
Expand All @@ -233,7 +233,7 @@ func (b *breaker) isAllowed() bool {
return false
}
case HalfOpen:
now := time.Now()
now := b.now()
if b.lastRetryTime.Add(b.options.DetectTimeout).After(now) {
rwx.Unlock()
return false
Expand Down
4 changes: 2 additions & 2 deletions cloud/circuitbreaker/circuitbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ type Options struct {
// but will consume more memory
EnableShardP bool

// for test
now func() time.Time
// Default value is time.Now, caller may use some high-performance custom time now func here
Now func() time.Time
}

const (
Expand Down

0 comments on commit d620a70

Please sign in to comment.