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

[circuitbreaker] Options export time now func, caller may use some high-performance custom time now func #39

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions cloud/circuitbreaker/breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type breaker struct {

// 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
2 changes: 1 addition & 1 deletion cloud/circuitbreaker/circuitbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Options struct {
EnableShardP bool

// for test
PureWhiteWu marked this conversation as resolved.
Show resolved Hide resolved
now func() time.Time
Now func() time.Time
}

const (
Expand Down