From 503a22c8ab291e5c8215a65e3a223277e7069480 Mon Sep 17 00:00:00 2001 From: JmPotato Date: Tue, 9 Jan 2024 18:00:24 +0800 Subject: [PATCH] http/client, retry: copy a new Backoffer for each request (#7684) ref tikv/pd#7300 Copy a new `Backoffer` for each request to prevent race. Signed-off-by: JmPotato Signed-off-by: pingandb --- client/http/client.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/http/client.go b/client/http/client.go index 863c8bfcbb56..ac14de50b332 100644 --- a/client/http/client.go +++ b/client/http/client.go @@ -183,11 +183,13 @@ func (ci *clientInner) requestWithRetry( if reqInfo.bo == nil { return execFunc() } + // Copy a new backoffer for each request. + bo := *reqInfo.bo // Backoffer also needs to check the status code to determine whether to retry. - reqInfo.bo.SetRetryableChecker(func(err error) bool { + bo.SetRetryableChecker(func(err error) bool { return err != nil && !noNeedRetry(statusCode) }) - return reqInfo.bo.Exec(ctx, execFunc) + return bo.Exec(ctx, execFunc) } func noNeedRetry(statusCode int) bool {