Skip to content

Commit

Permalink
fix request context cancellation is ignored when retryBackoff (opense…
Browse files Browse the repository at this point in the history
…arch-project#539)

Signed-off-by: zhyu <angellwings@gmail.com>
  • Loading branch information
zhyu authored and Jakob3xD committed Jul 1, 2024
1 parent 2464386 commit 92a2412
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adjust security Role struct for FLS from string to []string ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))
- Fixes wrong response parsing for indices mapping and recovery ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))
- Fixes wrong response parsing for security get requests ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))
- Fixes opensearchtransport ignores request context cancellation when `retryBackoff` is configured ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))

### Security

Expand Down
13 changes: 12 additions & 1 deletion opensearchtransport/opensearchtransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,18 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {

// Delay the retry if a backoff function is configured
if c.retryBackoff != nil {
time.Sleep(c.retryBackoff(i + 1))
var cancelled bool
timer := time.NewTimer(c.retryBackoff(i + 1))
select {
case <-req.Context().Done():
timer.Stop()
err = req.Context().Err()
cancelled = true
case <-timer.C:
}
if cancelled {
break
}
}
}
// Read, close and replace the http response body to close the connection
Expand Down
34 changes: 34 additions & 0 deletions opensearchtransport/opensearchtransport_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ package opensearchtransport
import (
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -848,6 +850,38 @@ func TestTransportPerformRetries(t *testing.T) {
t.Errorf("Unexpected duration, want=>%s, got=%s", expectedDuration, end)
}
})

t.Run("Delay the retry with retry on timeout and context deadline", func(t *testing.T) {
var i int
u, _ := url.Parse("http://foo.bar")
tp, _ := New(Config{
EnableRetryOnTimeout: true,
MaxRetries: 100,
RetryBackoff: func(i int) time.Duration { return time.Hour },
URLs: []*url.URL{u},
Transport: &mockTransp{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
i++
<-req.Context().Done()
return nil, req.Context().Err()
},
},
})

req, _ := http.NewRequest(http.MethodGet, "/abc", nil)
ctx, cancel := context.WithTimeout(req.Context(), 50*time.Millisecond)
defer cancel()
req = req.WithContext(ctx)

//nolint:bodyclose // Mock response does not have a body to close
_, err := tp.Perform(req)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got %s", err)
}
if i != 1 {
t.Fatalf("unexpected number of requests: expected 1, got %d", i)
}
})
}

func TestURLs(t *testing.T) {
Expand Down

0 comments on commit 92a2412

Please sign in to comment.