Skip to content

Instantly share code, notes, and snippets.

@gavrie
Last active May 17, 2016 12:23
Show Gist options
  • Save gavrie/591fcab94d5bc8d2aa52016843f2d30a to your computer and use it in GitHub Desktop.
Save gavrie/591fcab94d5bc8d2aa52016843f2d30a to your computer and use it in GitHub Desktop.
package retry
import "time"
type retrier struct {
timeout time.Duration
retries int
}
func New(timeout time.Duration, retries int) *retrier {
return &retrier{
timeout: timeout,
retries: retries,
}
}
func (br *retrier) nextTimeout() time.Duration {
br.retries--
return br.timeout
}
func (br *retrier) keepTrying() bool {
return br.retries > 0
}
func (br *retrier) TotalTimeout() (total time.Duration) {
r := *br // Make a copy to preserve unchanged original
for r.keepTrying() {
total += r.nextTimeout()
}
return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment