Skip to content

Commit

Permalink
Merge pull request imroc#210 from jfilipczyk/bugfix/do-not-retry-when…
Browse files Browse the repository at this point in the history
…-count-zero

fix: do not retry when RetryCount eq 0
  • Loading branch information
imroc authored Feb 20, 2023
2 parents f0c0c12 + 4d93b33 commit 67b393b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
11 changes: 6 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/imroc/req/v3/internal/dump"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
"io"
"net/http"
urlpkg "net/url"
Expand All @@ -17,6 +13,11 @@ import (
"reflect"
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/imroc/req/v3/internal/dump"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
)

// Request struct is used to compose and fire individual request from
Expand Down Expand Up @@ -577,7 +578,7 @@ func (r *Request) do() (resp *Response, err error) {
resp, err = r.client.roundTrip(r)
}

if r.retryOption == nil || (r.RetryAttempt >= r.retryOption.MaxRetries && r.retryOption.MaxRetries > 0) { // absolutely cannot retry.
if r.retryOption == nil || (r.RetryAttempt >= r.retryOption.MaxRetries && r.retryOption.MaxRetries >= 0) { // absolutely cannot retry.
return
}

Expand Down
28 changes: 27 additions & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package req

import (
"bytes"
"github.com/imroc/req/v3/internal/tests"
"io"
"math"
"net/http"
"testing"
"time"

"github.com/imroc/req/v3/internal/tests"
)

func TestRetryBackOff(t *testing.T) {
Expand Down Expand Up @@ -170,3 +171,28 @@ func TestRetryFalse(t *testing.T) {
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
}

func TestRetryTurnedOffWhenRetryCountEqZero(t *testing.T) {
resp, err := tc().R().
SetRetryCount(0).
SetRetryCondition(func(resp *Response, err error) bool {
t.Fatal("retry condition should not be executed")
return true
}).
Get("https://non-exists-host.com.cn")
tests.AssertNotNil(t, err)
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)

resp, err = tc().
SetCommonRetryCount(0).
SetCommonRetryCondition(func(resp *Response, err error) bool {
t.Fatal("retry condition should not be executed")
return true
}).
R().
Get("https://non-exists-host.com.cn")
tests.AssertNotNil(t, err)
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
}

0 comments on commit 67b393b

Please sign in to comment.