Last active
May 17, 2016 12:23
-
-
Save gavrie/591fcab94d5bc8d2aa52016843f2d30a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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