Skip to content

Commit 842f187

Browse files
committed
Merge branch 'master' into fork
2 parents 6e99605 + bba6d6a commit 842f187

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

retry.go

Lines changed: 5 additions & 32 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 @@ shouldRetry:
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,6 +170,7 @@ shouldRetry:
198170
}
199171
}
200172

173+
// Setting attempts to 0 means we'll retry until we succeed
201174
// if this is last attempt - don't wait
202175
if n == config.attempts-1 {
203176
break shouldRetry

retry_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func TestRetryIf_ZeroAttempts(t *testing.T) {
140140
return err.Error() != "special"
141141
}),
142142
Delay(time.Nanosecond),
143+
LastErrorOnly(true),
143144
Attempts(0),
144145
)
145146
if err == nil {
@@ -286,23 +287,16 @@ func TestLastErrorOnly(t *testing.T) {
286287
func TestUnrecoverableError(t *testing.T) {
287288
attempts := 0
288289
testErr := errors.New("error")
289-
expectedErr := Error{testErr}
290290
err := Do(
291291
func() error {
292292
attempts++
293293
return Unrecoverable(testErr)
294294
},
295295
Attempts(2),
296296
)
297-
if err.Error() != expectedErr.Error() {
298-
t.Errorf("got %v, want %v", err, expectedErr)
299-
}
300-
if testErr != errors.Unwrap(err) {
301-
t.Errorf("unwrapped error: got %v, want %v", errors.Unwrap(err), testErr)
302-
}
303-
if attempts != 1 {
304-
t.Errorf("attempts with unrecoverable error: got %d, want 1", attempts)
305-
}
297+
assert.Error(t, err)
298+
assert.Equal(t, Unrecoverable(testErr), err)
299+
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
306300
}
307301

308302
func TestCombineFixedDelays(t *testing.T) {
@@ -587,6 +581,7 @@ func TestContext(t *testing.T) {
587581
cancel()
588582
}
589583
}),
584+
LastErrorOnly(true),
590585
Context(ctx),
591586
Attempts(0),
592587
)

0 commit comments

Comments
 (0)