Skip to content

Instantly share code, notes, and snippets.

@jsok
Created May 15, 2015 02:18
Show Gist options
  • Save jsok/a4806fd6e5026944c3bc to your computer and use it in GitHub Desktop.
Save jsok/a4806fd6e5026944c3bc to your computer and use it in GitHub Desktop.
backoff: Limit the number of attempts using cenkalti/backoff
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