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

validation on negative time duration #26

Merged
merged 1 commit into from
Nov 25, 2019
Merged
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
14 changes: 10 additions & 4 deletions gobreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func (c *Counts) clear() {
//
// Interval is the cyclic period of the closed state
// for the CircuitBreaker to clear the internal Counts.
// If Interval is 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
// If Interval is less than or equal to 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
//
// Timeout is the period of the open state,
// after which the state of the CircuitBreaker becomes half-open.
// If Timeout is 0, the timeout value of the CircuitBreaker is set to 60 seconds.
// If Timeout is less than or equal to 0, the timeout value of the CircuitBreaker is set to 60 seconds.
//
// ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state.
// If ReadyToTrip returns true, the CircuitBreaker will be placed into the open state.
Expand Down Expand Up @@ -135,7 +135,6 @@ func NewCircuitBreaker(st Settings) *CircuitBreaker {
cb := new(CircuitBreaker)

cb.name = st.Name
cb.interval = st.Interval
cb.onStateChange = st.OnStateChange

if st.MaxRequests == 0 {
Expand All @@ -144,7 +143,13 @@ func NewCircuitBreaker(st Settings) *CircuitBreaker {
cb.maxRequests = st.MaxRequests
}

if st.Timeout == 0 {
if st.Interval <= 0 {
cb.interval = defaultInterval
} else {
cb.interval = st.Interval
}

if st.Timeout <= 0 {
cb.timeout = defaultTimeout
} else {
cb.timeout = st.Timeout
Expand All @@ -168,6 +173,7 @@ func NewTwoStepCircuitBreaker(st Settings) *TwoStepCircuitBreaker {
}
}

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

func defaultReadyToTrip(counts Counts) bool {
Expand Down
22 changes: 22 additions & 0 deletions gobreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var defaultCB *CircuitBreaker
var customCB *CircuitBreaker
var negativeDurationCB *CircuitBreaker

type StateChange struct {
name string
Expand Down Expand Up @@ -98,9 +99,19 @@ func newCustom() *CircuitBreaker {
return NewCircuitBreaker(customSt)
}

func newNegativeDurationCB() *CircuitBreaker {
var negativeSt Settings
negativeSt.Name = "ncb"
negativeSt.Interval = time.Duration(-30) * time.Second
negativeSt.Timeout = time.Duration(-90) * time.Second

return NewCircuitBreaker(negativeSt)
}

func init() {
defaultCB = NewCircuitBreaker(Settings{})
customCB = newCustom()
negativeDurationCB = newNegativeDurationCB()
}

func TestStateConstants(t *testing.T) {
Expand Down Expand Up @@ -136,6 +147,17 @@ func TestNewCircuitBreaker(t *testing.T) {
assert.Equal(t, StateClosed, customCB.state)
assert.Equal(t, Counts{0, 0, 0, 0, 0}, customCB.counts)
assert.False(t, customCB.expiry.IsZero())

negativeDurationCB := newNegativeDurationCB()
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.NotNil(t, negativeDurationCB.readyToTrip)
assert.Nil(t, negativeDurationCB.onStateChange)
assert.Equal(t, StateClosed, negativeDurationCB.state)
assert.Equal(t, Counts{0, 0, 0, 0, 0}, negativeDurationCB.counts)
assert.True(t, negativeDurationCB.expiry.IsZero())
}

func TestDefaultCircuitBreaker(t *testing.T) {
Expand Down