Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#47959
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
lance6716 authored and ti-chi-bot committed Oct 25, 2023
1 parent 3080406 commit ea44dfb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions br/pkg/pdutil/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func pdRequest(
return nil, errors.Trace(err)
}
reqURL := fmt.Sprintf("%s/%s", u, prefix)
<<<<<<< HEAD
req, err := http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -152,6 +153,19 @@ func pdRequest(
}
count := 0
for {
=======
var (
req *http.Request
resp *http.Response
)
count := 0
for {
req, err = http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil {
return 0, nil, errors.Trace(err)
}
resp, err = cli.Do(req) //nolint:bodyclose
>>>>>>> b9b336843bc (lightning: every HTTP retry should use its own request (#47959))
count++
if count > pdRequestRetryTime || resp.StatusCode < 500 {
break
Expand Down
11 changes: 10 additions & 1 deletion br/pkg/pdutil/pd_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package pdutil

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -183,8 +184,16 @@ func TestPDRequestRetry(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
cli := http.DefaultClient
cli.Transport = http.DefaultTransport.(*http.Transport).Clone()
// although the real code doesn't disable keep alive, we need to disable it
// in test to avoid the connection being reused and #47930 can't appear. The
// real code will only meet #47930 when go's internal http client just dropped
// all idle connections.
cli.Transport.(*http.Transport).DisableKeepAlives = true

taddr := ts.URL
_, reqErr := pdRequest(ctx, taddr, "", cli, http.MethodGet, nil)
body := bytes.NewBuffer([]byte("test"))
_, reqErr := pdRequest(ctx, taddr, "", cli, http.MethodPost, body)
require.NoError(t, reqErr)
ts.Close()
count = 0
Expand Down

0 comments on commit ea44dfb

Please sign in to comment.