From 1da8d9d0d6ad4f042aed8204076f1d206b6018b3 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 --- 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 863c8bfcbb5..ac14de50b33 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 {