mirror of https://github.com/jpillora/backoff.git
Merge pull request #8 from dividinglimits/master
Short-circuit the backoff if Min backoff is greater than Max
This commit is contained in:
commit
a6f64285fd
|
@ -39,6 +39,10 @@ func (b *Backoff) Duration() time.Duration {
|
|||
// ForAttempt is threadsafe iff non-zero values for Factor, Max, and Min
|
||||
// are set before any calls to ForAttempt are made.
|
||||
func (b *Backoff) ForAttempt(attempt float64) time.Duration {
|
||||
if float64(b.Min) > float64(b.Max) {
|
||||
return b.Max
|
||||
}
|
||||
|
||||
//Zero-values are nonsensical, so we use
|
||||
//them to apply defaults
|
||||
if b.Min == 0 {
|
||||
|
@ -50,6 +54,7 @@ func (b *Backoff) ForAttempt(attempt float64) time.Duration {
|
|||
if b.Factor == 0 {
|
||||
b.Factor = 2
|
||||
}
|
||||
|
||||
//calculate this duration
|
||||
dur := float64(b.Min) * math.Pow(b.Factor, attempt)
|
||||
if b.Jitter == true {
|
||||
|
|
|
@ -66,6 +66,16 @@ func Test3(t *testing.T) {
|
|||
equals(t, b.Duration(), 100*time.Nanosecond)
|
||||
}
|
||||
|
||||
func Test4(t *testing.T) {
|
||||
b := &Backoff{
|
||||
Min: 500 * time.Second,
|
||||
Max: 100 * time.Second,
|
||||
Factor: 1,
|
||||
}
|
||||
|
||||
equals(t, b.Duration(), b.Max)
|
||||
}
|
||||
|
||||
func TestGetAttempt(t *testing.T) {
|
||||
b := &Backoff{
|
||||
Min: 100 * time.Millisecond,
|
||||
|
|
Loading…
Reference in New Issue