Created
May 15, 2015 02:18
-
-
Save jsok/a4806fd6e5026944c3bc to your computer and use it in GitHub Desktop.
backoff: Limit the number of attempts using cenkalti/backoff
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
import "github.com/cenkalti/backoff" | |
type limitedAttempts struct { | |
limit int | |
attempts int | |
strategy backoff.BackOff | |
} | |
func NewLimitedAttempts(limit int, strategy backoff.BackOff) *limitedAttempts { | |
return &limitedAttempts{limit: limit, strategy: strategy} | |
} | |
func (b *limitedAttempts) Reset() { b.attempts = 0 } | |
func (b *limitedAttempts) NextBackOff() time.Duration { | |
if b.attempts >= b.limit { | |
return backoff.Stop | |
} | |
b.attempts++ | |
return b.strategy.NextBackOff() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment