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

Change timeout by a function #65

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
18 changes: 12 additions & 6 deletions v2/gobreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Settings struct {
Name string
MaxRequests uint32
Interval time.Duration
Timeout time.Duration
Timeout func() time.Duration
ReadyToTrip func(counts Counts) bool
OnStateChange func(name string, from State, to State)
IsSuccessful func(err error) bool
Expand All @@ -118,7 +118,7 @@ type CircuitBreaker[T any] struct {
name string
maxRequests uint32
interval time.Duration
timeout time.Duration
timeout func() time.Duration
readyToTrip func(counts Counts) bool
isSuccessful func(err error) bool
onStateChange func(name string, from State, to State)
Expand Down Expand Up @@ -156,10 +156,14 @@ func NewCircuitBreaker[T any](st Settings) *CircuitBreaker[T] {
cb.interval = st.Interval
}

if st.Timeout <= 0 {
if st.Timeout == nil {
cb.timeout = defaultTimeout
} else {
cb.timeout = st.Timeout
if st.Timeout() <= 0 {
cb.timeout = defaultTimeout
} else {
cb.timeout = st.Timeout
}
}

if st.ReadyToTrip == nil {
Expand All @@ -185,9 +189,11 @@ func NewTwoStepCircuitBreaker[T any](st Settings) *TwoStepCircuitBreaker[T] {
cb: NewCircuitBreaker[T](st),
}
}
func defaultTimeout() time.Duration {
return 60 * time.Second
}

const defaultInterval = time.Duration(0) * time.Second
const defaultTimeout = time.Duration(60) * time.Second

func defaultReadyToTrip(counts Counts) bool {
return counts.ConsecutiveFailures > 5
Expand Down Expand Up @@ -374,7 +380,7 @@ func (cb *CircuitBreaker[T]) toNewGeneration(now time.Time) {
cb.expiry = now.Add(cb.interval)
}
case StateOpen:
cb.expiry = now.Add(cb.timeout)
cb.expiry = now.Add(cb.timeout())
default: // StateHalfOpen
cb.expiry = zero
}
Expand Down
12 changes: 7 additions & 5 deletions v2/gobreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func newCustom() *CircuitBreaker[bool] {
customSt.Name = "cb"
customSt.MaxRequests = 3
customSt.Interval = time.Duration(30) * time.Second
customSt.Timeout = time.Duration(90) * time.Second
customSt.Timeout = func() time.Duration { return time.Duration(90) * time.Second }
customSt.ReadyToTrip = func(counts Counts) bool {
numReqs := counts.Requests
failureRatio := float64(counts.TotalFailures) / float64(numReqs)
Expand All @@ -102,7 +102,9 @@ func newNegativeDurationCB() *CircuitBreaker[bool] {
var negativeSt Settings
negativeSt.Name = "ncb"
negativeSt.Interval = time.Duration(-30) * time.Second
negativeSt.Timeout = time.Duration(-90) * time.Second
negativeSt.Timeout = func() time.Duration {
return time.Duration(-90) * time.Second
}

return NewCircuitBreaker[bool](negativeSt)
}
Expand All @@ -128,7 +130,7 @@ func TestNewCircuitBreaker(t *testing.T) {
assert.Equal(t, "", defaultCB.name)
assert.Equal(t, uint32(1), defaultCB.maxRequests)
assert.Equal(t, time.Duration(0), defaultCB.interval)
assert.Equal(t, time.Duration(60)*time.Second, defaultCB.timeout)
assert.Equal(t, time.Duration(60)*time.Second, defaultCB.timeout())
assert.NotNil(t, defaultCB.readyToTrip)
assert.Nil(t, defaultCB.onStateChange)
assert.Equal(t, StateClosed, defaultCB.state)
Expand All @@ -139,7 +141,7 @@ func TestNewCircuitBreaker(t *testing.T) {
assert.Equal(t, "cb", customCB.name)
assert.Equal(t, uint32(3), customCB.maxRequests)
assert.Equal(t, time.Duration(30)*time.Second, customCB.interval)
assert.Equal(t, time.Duration(90)*time.Second, customCB.timeout)
assert.Equal(t, time.Duration(90)*time.Second, customCB.timeout())
assert.NotNil(t, customCB.readyToTrip)
assert.NotNil(t, customCB.onStateChange)
assert.Equal(t, StateClosed, customCB.state)
Expand All @@ -150,7 +152,7 @@ func TestNewCircuitBreaker(t *testing.T) {
assert.Equal(t, "ncb", negativeDurationCB.name)
assert.Equal(t, uint32(1), negativeDurationCB.maxRequests)
assert.Equal(t, time.Duration(0)*time.Second, negativeDurationCB.interval)
assert.Equal(t, time.Duration(60)*time.Second, negativeDurationCB.timeout)
assert.Equal(t, time.Duration(60)*time.Second, negativeDurationCB.timeout())
assert.NotNil(t, negativeDurationCB.readyToTrip)
assert.Nil(t, negativeDurationCB.onStateChange)
assert.Equal(t, StateClosed, negativeDurationCB.state)
Expand Down