lib/retry: export fields

The fields are only ever read by Waiter, and
setting the fields makes the calling code read much better without
having to create a bunch of constants that only ever get used once.
This commit is contained in:
Daniel Nephin 2020-10-01 19:03:44 -04:00
parent ca26dfb4a2
commit 7b4aca2088
1 changed files with 18 additions and 18 deletions

View File

@ -47,10 +47,10 @@ func (j *JitterRandomStagger) AddJitter(baseTime time.Duration) time.Duration {
// RetryWaiter will record failed and successful operations and provide // RetryWaiter will record failed and successful operations and provide
// a channel to wait on before a failed operation can be retried. // a channel to wait on before a failed operation can be retried.
type Waiter struct { type Waiter struct {
minFailures uint MinFailures uint
minWait time.Duration MinWait time.Duration
maxWait time.Duration MaxWait time.Duration
jitter Jitter Jitter Jitter
failures uint failures uint
} }
@ -69,35 +69,35 @@ func NewRetryWaiter(minFailures int, minWait, maxWait time.Duration, jitter Jitt
} }
return &Waiter{ return &Waiter{
minFailures: uint(minFailures), MinFailures: uint(minFailures),
minWait: minWait, MinWait: minWait,
maxWait: maxWait, MaxWait: maxWait,
failures: 0, failures: 0,
jitter: jitter, Jitter: jitter,
} }
} }
// calculates the necessary wait time before the // calculates the necessary wait time before the
// next operation should be allowed. // next operation should be allowed.
func (rw *Waiter) calculateWait() time.Duration { func (rw *Waiter) calculateWait() time.Duration {
waitTime := rw.minWait waitTime := rw.MinWait
if rw.failures > rw.minFailures { if rw.failures > rw.MinFailures {
shift := rw.failures - rw.minFailures - 1 shift := rw.failures - rw.MinFailures - 1
waitTime = rw.maxWait waitTime = rw.MaxWait
if shift < 31 { if shift < 31 {
waitTime = (1 << shift) * time.Second waitTime = (1 << shift) * time.Second
} }
if waitTime > rw.maxWait { if waitTime > rw.MaxWait {
waitTime = rw.maxWait waitTime = rw.MaxWait
} }
if rw.jitter != nil { if rw.Jitter != nil {
waitTime = rw.jitter.AddJitter(waitTime) waitTime = rw.Jitter.AddJitter(waitTime)
} }
} }
if waitTime < rw.minWait { if waitTime < rw.MinWait {
waitTime = rw.minWait waitTime = rw.MinWait
} }
return waitTime return waitTime