Skip to content

Commit b8f7c11

Browse files
authored
Don't wait after last retry (#444)
* Don't wait after last retry * Fix tests Signed-off-by: Pavel <[email protected]>
1 parent c1fa358 commit b8f7c11

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

retry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ func Backoff(operation func() (*Response, error), options ...Option) error {
129129
hook(resp, err)
130130
}
131131

132+
// Don't need to wait when no retries left.
133+
// Still run retry hooks even on last retry to keep compatibility.
134+
if attempt == opts.maxRetries {
135+
return err
136+
}
137+
132138
waitTime, err2 := sleepDuration(resp, opts.waitTime, opts.maxWaitTime, attempt)
133139
if err2 != nil {
134140
if err == nil {

retry_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,39 @@ func TestBackoffSuccess(t *testing.T) {
3232
assertEqual(t, externalCounter, attempts)
3333
}
3434

35+
func TestBackoffNoWaitForLastRetry(t *testing.T) {
36+
attempts := 1
37+
externalCounter := 0
38+
numRetries := 1
39+
40+
canceledCtx, cancel := context.WithCancel(context.Background())
41+
defer cancel()
42+
43+
resp := &Response{
44+
Request: &Request{
45+
ctx: canceledCtx,
46+
client: &Client{
47+
RetryAfter: func(*Client, *Response) (time.Duration, error) {
48+
return 6, nil
49+
},
50+
},
51+
},
52+
}
53+
54+
retryErr := Backoff(func() (*Response, error) {
55+
externalCounter++
56+
return resp, nil
57+
}, RetryConditions([]RetryConditionFunc{func(response *Response, err error) bool {
58+
if externalCounter == attempts + numRetries {
59+
// Backoff returns context canceled if goes to sleep after last retry.
60+
cancel()
61+
}
62+
return true
63+
}}), Retries(numRetries))
64+
65+
assertNil(t, retryErr)
66+
}
67+
3568
func TestBackoffTenAttemptsSuccess(t *testing.T) {
3669
attempts := 10
3770
externalCounter := 0
@@ -169,8 +202,8 @@ func TestClientRetryGet(t *testing.T) {
169202
assertNotNil(t, resp.Body())
170203
assertEqual(t, 0, len(resp.Header()))
171204

172-
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
173-
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
205+
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
206+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\""))
174207
}
175208

176209
func TestClientRetryWait(t *testing.T) {
@@ -639,8 +672,8 @@ func TestClientRetryCount(t *testing.T) {
639672
// 2 attempts were made
640673
assertEqual(t, attempt, 2)
641674

642-
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
643-
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
675+
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
676+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\""))
644677
}
645678

646679
func TestClientErrorRetry(t *testing.T) {
@@ -693,8 +726,8 @@ func TestClientRetryHook(t *testing.T) {
693726

694727
assertEqual(t, 3, attempt)
695728

696-
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
697-
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
729+
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
730+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\""))
698731
}
699732

700733
func filler(*Response, error) bool {

0 commit comments

Comments
 (0)