Skip to content

Commit 0436e02

Browse files
committed
when the attempt value is zero, check attemptsForError
1 parent cbf4dbf commit 0436e02

File tree

2 files changed

+10
-37
lines changed

2 files changed

+10
-37
lines changed

retry.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -136,38 +136,6 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
136136
return emptyT, err
137137
}
138138

139-
// Setting attempts to 0 means we'll retry until we succeed
140-
var lastErr error
141-
if config.attempts == 0 {
142-
for {
143-
t, err := retryableFunc()
144-
if err == nil {
145-
return t, nil
146-
}
147-
148-
if !IsRecoverable(err) {
149-
return emptyT, err
150-
}
151-
152-
if !config.retryIf(err) {
153-
return emptyT, err
154-
}
155-
156-
lastErr = err
157-
158-
config.onRetry(n, err)
159-
n++
160-
select {
161-
case <-config.timer.After(delay(config, n, err)):
162-
case <-config.context.Done():
163-
if config.wrapContextErrorWithLastError {
164-
return emptyT, Error{context.Cause(config.context), lastErr}
165-
}
166-
return emptyT, context.Cause(config.context)
167-
}
168-
}
169-
}
170-
171139
errorLog := Error{}
172140

173141
attemptsForError := make(map[error]uint, len(config.attemptsForError))
@@ -184,6 +152,10 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
184152

185153
errorLog = append(errorLog, unpackUnrecoverable(err))
186154

155+
if !IsRecoverable(err) {
156+
return emptyT, err
157+
}
158+
187159
if !config.retryIf(err) {
188160
break
189161
}
@@ -198,8 +170,9 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
198170
}
199171
}
200172

173+
// Setting attempts to 0 means we'll retry until we succeed
201174
// if this is last attempt - don't wait
202-
if n == config.attempts-1 {
175+
if config.attempts != 0 && n == config.attempts-1 {
203176
break
204177
}
205178
n++
@@ -213,7 +186,6 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
213186
return emptyT, append(errorLog, context.Cause(config.context))
214187
}
215188

216-
shouldRetry = shouldRetry && n < config.attempts
217189
}
218190

219191
if config.lastErrorOnly {

retry_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestRetryIf_ZeroAttempts(t *testing.T) {
104104
return err.Error() != "special"
105105
}),
106106
Delay(time.Nanosecond),
107+
LastErrorOnly(true),
107108
Attempts(0),
108109
)
109110
assert.Error(t, err)
@@ -215,16 +216,15 @@ func TestLastErrorOnly(t *testing.T) {
215216
func TestUnrecoverableError(t *testing.T) {
216217
attempts := 0
217218
testErr := errors.New("error")
218-
expectedErr := Error{testErr}
219219
err := Do(
220220
func() error {
221221
attempts++
222222
return Unrecoverable(testErr)
223223
},
224224
Attempts(2),
225225
)
226-
assert.Equal(t, expectedErr, err)
227-
assert.Equal(t, testErr, errors.Unwrap(err))
226+
assert.Error(t, err)
227+
assert.Equal(t, Unrecoverable(testErr), err)
228228
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
229229
}
230230

@@ -457,6 +457,7 @@ func TestContext(t *testing.T) {
457457
cancel()
458458
}
459459
}),
460+
LastErrorOnly(true),
460461
Context(ctx),
461462
Attempts(0),
462463
)

0 commit comments

Comments
 (0)