Skip to content

Commit

Permalink
reuse timer in Retry func
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Jan 6, 2019
1 parent 62661b4 commit 1e4cf3d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Retry(o Operation, b BackOff) error { return RetryNotify(o, b, nil) }
func RetryNotify(operation Operation, b BackOff, notify Notify) error {
var err error
var next time.Duration
var t *time.Timer

cb := ensureContext(b)

Expand All @@ -49,11 +50,15 @@ func RetryNotify(operation Operation, b BackOff, notify Notify) error {
notify(err, next)
}

t := time.NewTimer(next)
if t == nil {
t = time.NewTimer(next)
defer t.Stop()
} else {
t.Reset(next)
}

select {
case <-cb.Context().Done():
t.Stop()
return err
case <-t.C:
}
Expand Down

0 comments on commit 1e4cf3d

Please sign in to comment.