Skip to content

Commit 8b2649c

Browse files
committed
fix unexpected sleep after the last retry
Signed-off-by: zhyu <[email protected]>
1 parent 7eecc89 commit 8b2649c

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1818
### Fixed
1919

2020
- Fixes opensearchtransport ignores request context cancellation when `retryBackoff` is configured ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))
21+
- Fixes opensearchtransport sleeps unexpectedly after the last retry ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))
2122

2223
### Security
2324

opensearchtransport/opensearchtransport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
379379
}
380380

381381
// Delay the retry if a backoff function is configured
382-
if c.retryBackoff != nil {
382+
if c.retryBackoff != nil && i < c.maxRetries {
383383
var cancelled bool
384384
timer := time.NewTimer(c.retryBackoff(i + 1))
385385
select {

opensearchtransport/opensearchtransport_internal_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,54 @@ func TestTransportPerformRetries(t *testing.T) {
841841
t.Fatalf("unexpected number of requests: expected 1, got %d", i)
842842
}
843843
})
844+
845+
t.Run("Don't backoff after the last retry", func(t *testing.T) {
846+
var (
847+
i int
848+
j int
849+
numReqs = 5
850+
numRetries = numReqs - 1
851+
)
852+
853+
u, _ := url.Parse("http://foo.bar")
854+
tp, _ := New(Config{
855+
MaxRetries: numRetries,
856+
URLs: []*url.URL{u, u, u},
857+
Transport: &mockTransp{
858+
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
859+
i++
860+
fmt.Printf("Request #%d", i)
861+
fmt.Print(": ERR\n")
862+
return nil, &mockNetError{error: fmt.Errorf("Mock network error (%d)", i)}
863+
},
864+
},
865+
866+
// A simple incremental backoff function
867+
//
868+
RetryBackoff: func(i int) time.Duration {
869+
j++
870+
d := time.Millisecond
871+
fmt.Printf("Attempt: %d | Sleeping for %s...\n", i, d)
872+
return d
873+
},
874+
})
875+
876+
req, _ := http.NewRequest(http.MethodGet, "/abc", nil)
877+
878+
//nolint:bodyclose // Mock response does not have a body to close
879+
_, err := tp.Perform(req)
880+
if err == nil {
881+
t.Fatalf("Expected error, got: %v", err)
882+
}
883+
884+
if i != numReqs {
885+
t.Errorf("Unexpected number of requests, want=%d, got=%d", numReqs, i)
886+
}
887+
888+
if j != numRetries {
889+
t.Errorf("Unexpected number of backoffs, want=>%d, got=%d", numRetries, j)
890+
}
891+
})
844892
}
845893

846894
func TestURLs(t *testing.T) {

0 commit comments

Comments
 (0)