Skip to content

Commit 461d58d

Browse files
author
Francesco Cosentino
committed
cosmetics
1 parent 49274e8 commit 461d58d

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

retry.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type Retrier struct {
5050
mutex sync.RWMutex
5151
// timer is the timer used to timeout the retry function.
5252
timer *time.Timer
53+
// err is the error returned by the retry function.
54+
err error
5355
}
5456

5557
// NewRetrier returns a new Retrier.
@@ -93,21 +95,18 @@ func (r *Retrier) Retry(fn func() error, temporaryErrors ...string) error {
9395
// defer stop the timer for the timeout.
9496
defer r.timer.Stop()
9597

96-
// Initialize a variable to store the error returned by the function.
97-
var err error
98-
9998
// Retry the function until it returns a nil error or the maximum number of retries is reached.
10099
for i := 0; i < r.MaxRetries; i++ {
101100
// Call the function.
102-
err = fn()
101+
r.err = fn()
103102

104103
// If the function returns a nil error, return nil.
105-
if err == nil {
104+
if r.err == nil {
106105
return nil
107106
}
108107

109108
// Check if the error returned by the function is temporary when the list of temporary errors is not empty.
110-
if len(temporaryErrors) > 0 && !r.IsTemporaryError(err, temporaryErrors...) {
109+
if len(temporaryErrors) > 0 && !r.IsTemporaryError(r.err, temporaryErrors...) {
111110
break
112111
}
113112

@@ -116,7 +115,7 @@ func (r *Retrier) Retry(fn func() error, temporaryErrors ...string) error {
116115
select {
117116
case <-r.timer.C:
118117
// Return an error if the timeout is reached.
119-
return fmt.Errorf("timeout reached after %v: %w", r.Timeout, err)
118+
return fmt.Errorf("timeout reached after %v: %w", r.Timeout, r.err)
120119
case <-time.After(sleepDuration):
121120
// Continue the loop if the sleep duration expires.
122121
}
@@ -125,7 +124,7 @@ func (r *Retrier) Retry(fn func() error, temporaryErrors ...string) error {
125124
// Return an error indicating that the maximum number of retries was reached.
126125
retryErr := retryErrorPool.Get().(*RetryError)
127126
retryErr.MaxRetries = r.MaxRetries
128-
retryErr.Err = err
127+
retryErr.Err = r.err
129128
return retryErr
130129
}
131130

0 commit comments

Comments
 (0)