Skip to content

Commit 576bc31

Browse files
authored
Backport maxretries fix 7.12 (#242)
* Transport: Add tests for Max Retries validation * Transport: Fix maxretries mechanism * Transport: Test: Keep ReadAll invocation from ioutil until next major version of Go
1 parent 0c90071 commit 576bc31

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

estransport/estransport.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
231231
}
232232
}
233233

234-
for i := 1; i <= c.maxRetries; i++ {
234+
for i := 0; i <= c.maxRetries; i++ {
235235
var (
236236
conn *Connection
237237
shouldRetry bool
@@ -253,7 +253,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
253253
c.setReqURL(conn.URL, req)
254254
c.setReqAuth(conn.URL, req)
255255

256-
if !c.disableRetry && i > 1 && req.Body != nil && req.Body != http.NoBody {
256+
if !c.disableRetry && i > 0 && req.Body != nil && req.Body != http.NoBody {
257257
body, err := req.GetBody()
258258
if err != nil {
259259
return nil, fmt.Errorf("cannot get request body: %s", err)
@@ -336,7 +336,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
336336

337337
// Delay the retry if a backoff function is configured
338338
if c.retryBackoff != nil {
339-
time.Sleep(c.retryBackoff(i))
339+
time.Sleep(c.retryBackoff(i + 1))
340340
}
341341
}
342342

estransport/estransport_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func TestTransportRetries(t *testing.T) {
6464
fmt.Println("> GET", req.URL)
6565
fmt.Printf("< %s (tries: %d)\n", bytes.TrimSpace(body), counter)
6666

67-
if counter != 3 {
68-
t.Errorf("Unexpected number of retries, want=3, got=%d", counter)
67+
if counter != 4 {
68+
t.Errorf("Unexpected number of attempts, want=4, got=%d", counter)
6969
}
7070
})
7171
}

estransport/estransport_internal_test.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestTransportPerformRetries(t *testing.T) {
529529
t.Fatalf("Unexpected error: %s", err)
530530
}
531531

532-
if i != numReqs {
532+
if i != numReqs+1 {
533533
t.Errorf("Unexpected number of requests, want=%d, got=%d", numReqs, i)
534534
}
535535

@@ -575,7 +575,8 @@ func TestTransportPerformRetries(t *testing.T) {
575575
t.Errorf("Unexpected response: %+v", res)
576576
}
577577

578-
if i != numReqs {
578+
// Should be initial HTTP request + 3 retries
579+
if i != numReqs+1 {
579580
t.Errorf("Unexpected number of requests, want=%d, got=%d", numReqs, i)
580581
}
581582
})
@@ -604,8 +605,8 @@ func TestTransportPerformRetries(t *testing.T) {
604605
}
605606
_ = res
606607

607-
if n := len(bodies); n != 3 {
608-
t.Fatalf("expected 3 requests, got %d", n)
608+
if n := len(bodies); n != 4 {
609+
t.Fatalf("expected 4 requests, got %d", n)
609610
}
610611
for i, body := range bodies {
611612
if body != "FOOBAR" {
@@ -674,14 +675,15 @@ func TestTransportPerformRetries(t *testing.T) {
674675
t.Run("Delay the retry with a backoff function", func(t *testing.T) {
675676
var (
676677
i int
677-
numReqs = 3
678+
numReqs = 4
678679
start = time.Now()
679-
expectedDuration = time.Duration(numReqs*100) * time.Millisecond
680+
expectedDuration = time.Duration((numReqs-1)*100) * time.Millisecond
680681
)
681682

682683
u, _ := url.Parse("http://foo.bar")
683684
tp, _ := New(Config{
684-
URLs: []*url.URL{u, u, u},
685+
MaxRetries: numReqs,
686+
URLs: []*url.URL{u, u, u},
685687
Transport: &mockTransp{
686688
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
687689
i++
@@ -796,3 +798,73 @@ func TestMetaHeader(t *testing.T) {
796798
}
797799
})
798800
}
801+
802+
func TestMaxRetries(t *testing.T) {
803+
tests := []struct {
804+
name string
805+
maxRetries int
806+
disableRetry bool
807+
expectedCallCount int
808+
}{
809+
{
810+
name: "MaxRetries Active set to default",
811+
disableRetry: false,
812+
expectedCallCount: 4,
813+
},
814+
{
815+
name: "MaxRetries Active set to 1",
816+
maxRetries: 1,
817+
disableRetry: false,
818+
expectedCallCount: 2,
819+
},
820+
{
821+
name: "Max Retries Active set to 2",
822+
maxRetries: 2,
823+
disableRetry: false,
824+
expectedCallCount: 3,
825+
},
826+
{
827+
name: "Max Retries Active set to 3",
828+
maxRetries: 3,
829+
disableRetry: false,
830+
expectedCallCount: 4,
831+
},
832+
{
833+
name: "MaxRetries Inactive set to 0",
834+
maxRetries: 0,
835+
disableRetry: true,
836+
expectedCallCount: 1,
837+
},
838+
{
839+
name: "MaxRetries Inactive set to 3",
840+
maxRetries: 3,
841+
disableRetry: true,
842+
expectedCallCount: 1,
843+
},
844+
}
845+
for _, test := range tests {
846+
t.Run(test.name, func(t *testing.T) {
847+
var callCount int
848+
c, _ := New(Config{
849+
URLs: []*url.URL{{}},
850+
Transport: &mockTransp{
851+
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
852+
callCount++
853+
return &http.Response{
854+
StatusCode: http.StatusBadGateway,
855+
Status: "MOCK",
856+
}, nil
857+
},
858+
},
859+
MaxRetries: test.maxRetries,
860+
DisableRetry: test.disableRetry,
861+
})
862+
863+
c.Perform(&http.Request{URL: &url.URL{}, Header: make(http.Header)}) // errcheck ignore
864+
865+
if test.expectedCallCount != callCount {
866+
t.Errorf("Bad retry call count, got : %d, want : %d", callCount, test.expectedCallCount)
867+
}
868+
})
869+
}
870+
}

0 commit comments

Comments
 (0)