diff --git a/client/base_client.go b/client/base_client.go index 9f0f2ca6bb5..556dde16032 100644 --- a/client/base_client.go +++ b/client/base_client.go @@ -140,7 +140,7 @@ func (c *baseClient) memberLoop() { case <-c.checkLeaderCh: case <-ticker.C: case <-ctx.Done(): - log.Info("[pd.reconnectLoop] exit reconnectLoop") + log.Info("[pd.memberLoop] exit memberLoop") return } failpoint.Inject("skipUpdateMember", func() { diff --git a/client/retry/backoff.go b/client/retry/backoff.go index fb3837f58f5..a8c1fe2ed9f 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,18 +57,19 @@ 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, } } -// NextBackoff implements the `Backoffer`, for now use the `ExponentialBackoff`. +// NextBackoff for now use the `exponentialBackoff`. func (rs *BackOffer) NextBackoff() time.Duration { - return rs.ExponentialBackoff() + return rs.exponentialBackoff() } -// ExponentialBackoff Get the exponential backoff duration. -func (rs *BackOffer) ExponentialBackoff() time.Duration { +// exponentialBackoff Get the exponential backoff duration. +func (rs *BackOffer) exponentialBackoff() time.Duration { backoff := rs.nextBackoff rs.nextBackoff *= 2 if rs.nextBackoff > rs.maxBackoff { @@ -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 70436cb5346..9b0bba9744a 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.Error(err) }