forked from traefik/traefik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b6af20
commit 364958c
Showing
11 changed files
with
209 additions
and
120 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package job | ||
|
||
import ( | ||
"github.com/cenk/backoff" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ backoff.BackOff = (*BackOff)(nil) | ||
) | ||
|
||
const ( | ||
defaultMinJobInterval = 30 * time.Second | ||
) | ||
|
||
// BackOff is an exponential backoff implementation for long running jobs. | ||
// In long running jobs, an operation() that fails after a long Duration should not increments the backoff period. | ||
// If operation() takes more than MinJobInterval, Reset() is called in NextBackOff(). | ||
type BackOff struct { | ||
*backoff.ExponentialBackOff | ||
MinJobInterval time.Duration | ||
} | ||
|
||
// NewBackOff creates an instance of BackOff using default values. | ||
func NewBackOff(backOff *backoff.ExponentialBackOff) *BackOff { | ||
backOff.MaxElapsedTime = 0 | ||
return &BackOff{ | ||
ExponentialBackOff: backOff, | ||
MinJobInterval: defaultMinJobInterval, | ||
} | ||
} | ||
|
||
// NextBackOff calculates the next backoff interval. | ||
func (b *BackOff) NextBackOff() time.Duration { | ||
if b.GetElapsedTime() >= b.MinJobInterval { | ||
b.Reset() | ||
} | ||
return b.ExponentialBackOff.NextBackOff() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package job | ||
|
||
import ( | ||
"github.com/cenk/backoff" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestJobBackOff(t *testing.T) { | ||
var ( | ||
testInitialInterval = 500 * time.Millisecond | ||
testRandomizationFactor = 0.1 | ||
testMultiplier = 2.0 | ||
testMaxInterval = 5 * time.Second | ||
testMinJobInterval = 1 * time.Second | ||
) | ||
|
||
exp := NewBackOff(backoff.NewExponentialBackOff()) | ||
exp.InitialInterval = testInitialInterval | ||
exp.RandomizationFactor = testRandomizationFactor | ||
exp.Multiplier = testMultiplier | ||
exp.MaxInterval = testMaxInterval | ||
exp.MinJobInterval = testMinJobInterval | ||
exp.Reset() | ||
|
||
var expectedResults = []time.Duration{500, 500, 500, 1000, 2000, 4000, 5000, 5000, 500, 1000, 2000, 4000, 5000, 5000} | ||
for i, d := range expectedResults { | ||
expectedResults[i] = d * time.Millisecond | ||
} | ||
|
||
for i, expected := range expectedResults { | ||
// Assert that the next backoff falls in the expected range. | ||
var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected)) | ||
var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected)) | ||
if i < 3 || i == 8 { | ||
time.Sleep(2 * time.Second) | ||
} | ||
var actualInterval = exp.NextBackOff() | ||
if !(minInterval <= actualInterval && actualInterval <= maxInterval) { | ||
t.Error("error") | ||
} | ||
// assertEquals(t, expected, exp.currentInterval) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters