Skip to content

Commit

Permalink
refine backoff
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Aug 28, 2023
1 parent 6f5baee commit 23318b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 11 additions & 9 deletions client/retry/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions client/retry/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/stretchr/testify/require"
)

Expand All @@ -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)
}

0 comments on commit 23318b6

Please sign in to comment.