Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug & some optimize #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Cancel the retry sleep if the request context is cancelled or de…
…adline exceeded
  • Loading branch information
panlq01 committed Apr 6, 2022
commit d4720760862cc3c172a53372b7ce546c69607d0c
15 changes: 13 additions & 2 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ func (c *Client) Do(request *http.Request) (*http.Response, error) {
break
}

backoffTime := c.retrier.NextInterval(i)
time.Sleep(backoffTime)
// Cancel the retry sleep if the request context is cancelled or deadline exceeded
timer := time.NewTimer(c.retrier.NextInterval(i))
select {
case <-request.Context().Done():
timer.Stop()
break
case <-timer.C:
}
}

if !shouldRetry && err == nil {
Expand All @@ -185,6 +191,11 @@ func (c *Client) Do(request *http.Request) (*http.Response, error) {
}

func (c *Client) checkRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
// do not retry on context.Canceled or context.DeadlineExceeded
if ctx.Err() != nil {
return false, ctx.Err()
}

if err != nil {
return true, err
}
Expand Down
32 changes: 32 additions & 0 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpclient

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -15,6 +16,37 @@ import (
"github.com/stretchr/testify/require"
)

func TestHTTPRequestWithContextDoSuccess(t *testing.T) {
noOfRetries := 3
backoffInterval := 1 * time.Millisecond
maximumJitterInterval := 10 * time.Millisecond

client := NewClient(
WithRetryCount(noOfRetries),
WithRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(backoffInterval, maximumJitterInterval))),
)

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "response": "ok" }`))
time.Sleep(100 * time.Millisecond)
}

server := httptest.NewServer(http.HandlerFunc(dummyHandler))
defer server.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err)

// define some user case context
subCtx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

req = req.WithContext(subCtx)
_, err = client.Do(req)
require.Contains(t, err.Error(), "context deadline exceeded")
}

func TestHTTPClientDoSuccess(t *testing.T) {
client := NewClient(WithHTTPTimeout(10 * time.Millisecond))

Expand Down