From 0e39195983db7f6cd60bee0c44ae3a86479c32ba Mon Sep 17 00:00:00 2001 From: lance6716 Date: Wed, 25 Oct 2023 16:14:03 +0800 Subject: [PATCH] This is an automated cherry-pick of #47959 Signed-off-by: ti-chi-bot --- br/pkg/pdutil/pd.go | 14 ++++++++++++++ br/pkg/pdutil/pd_serial_test.go | 11 ++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/br/pkg/pdutil/pd.go b/br/pkg/pdutil/pd.go index d7921d4c7bf7e..0a6c4612e2a88 100644 --- a/br/pkg/pdutil/pd.go +++ b/br/pkg/pdutil/pd.go @@ -141,6 +141,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) @@ -151,6 +152,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 diff --git a/br/pkg/pdutil/pd_serial_test.go b/br/pkg/pdutil/pd_serial_test.go index c76e49cd70c61..89d7b0fda5726 100644 --- a/br/pkg/pdutil/pd_serial_test.go +++ b/br/pkg/pdutil/pd_serial_test.go @@ -3,6 +3,7 @@ package pdutil import ( + "bytes" "context" "encoding/hex" "encoding/json" @@ -186,8 +187,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