From 3970df8d66e5db6104ca2f89878fc20dacb473d1 Mon Sep 17 00:00:00 2001 From: husharp Date: Mon, 28 Aug 2023 18:21:04 +0800 Subject: [PATCH] refine backoff Signed-off-by: husharp --- client/retry/backoff.go | 12 +++++++----- client/retry/backoff_test.go | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/client/retry/backoff.go b/client/retry/backoff.go index fb3837f58f55..895b7bc4d96c 100644 --- a/client/retry/backoff.go +++ b/client/retry/backoff.go @@ -24,8 +24,9 @@ import ( // BackOffer is a backoff policy for retrying operations. type BackOffer struct { - maxBackoff time.Duration - nextBackoff time.Duration + maxBackoff time.Duration + nextBackoff time.Duration + initialBackoff time.Duration } // WithBackoff is a helper function to add backoff. @@ -56,8 +57,9 @@ func WithBackoff( // InitialBackOffer make the initial state for retrying. func InitialBackOffer(initialBackoff, maxBackoff time.Duration) BackOffer { return BackOffer{ - maxBackoff: maxBackoff, - nextBackoff: initialBackoff, + maxBackoff: maxBackoff, + initialBackoff: initialBackoff, + nextBackoff: initialBackoff, } } @@ -78,7 +80,7 @@ func (rs *BackOffer) ExponentialBackoff() time.Duration { // ResetBackoff reset the backoff to initial state. func (rs *BackOffer) ResetBackoff() { - rs.nextBackoff = 0 + rs.nextBackoff = rs.initialBackoff } // Only used for test. diff --git a/client/retry/backoff_test.go b/client/retry/backoff_test.go index 70436cb53460..15085a5e1fa6 100644 --- a/client/retry/backoff_test.go +++ b/client/retry/backoff_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/pingcap/errors" "github.com/stretchr/testify/require" ) @@ -40,8 +41,7 @@ func TestExponentialBackoff(t *testing.T) { // Reset backoff backoff.ResetBackoff() err := WithBackoff(context.Background(), func() error { - return nil + return errors.New("test") }, &backoff) - re.Nil(err) - + re.NotNil(err) }