github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/retry/retry_with_opt.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package retry
    15  
    16  import (
    17  	"context"
    18  	"math"
    19  	"math/rand"
    20  	"time"
    21  
    22  	"github.com/pingcap/errors"
    23  	cerror "github.com/pingcap/ticdc/pkg/errors"
    24  )
    25  
    26  // Operation is the action need to retry
    27  type Operation func() error
    28  
    29  // Do execute the specified function at most maxTries times until it succeeds or got canceled
    30  func Do(ctx context.Context, operation Operation, opts ...Option) error {
    31  	retryOption := setOptions(opts...)
    32  	return run(ctx, operation, retryOption)
    33  }
    34  
    35  func setOptions(opts ...Option) *retryOptions {
    36  	retryOption := newRetryOptions()
    37  	for _, opt := range opts {
    38  		opt(retryOption)
    39  	}
    40  	return retryOption
    41  }
    42  
    43  func run(ctx context.Context, op Operation, retryOption *retryOptions) error {
    44  	select {
    45  	case <-ctx.Done():
    46  		return errors.Trace(ctx.Err())
    47  	default:
    48  	}
    49  
    50  	var t *time.Timer
    51  	try := 0
    52  	backOff := time.Duration(0)
    53  	for {
    54  		err := op()
    55  		if err == nil {
    56  			return nil
    57  		}
    58  
    59  		if !retryOption.isRetryable(err) {
    60  			return err
    61  		}
    62  
    63  		try++
    64  		if int64(try) >= retryOption.maxTries {
    65  			return cerror.ErrReachMaxTry.Wrap(err).GenWithStackByArgs(retryOption.maxTries)
    66  		}
    67  
    68  		backOff = getBackoffInMs(retryOption.backoffBaseInMs, retryOption.backoffCapInMs, float64(try))
    69  		if t == nil {
    70  			t = time.NewTimer(backOff)
    71  			defer t.Stop()
    72  		} else {
    73  			t.Reset(backOff)
    74  		}
    75  
    76  		select {
    77  		case <-ctx.Done():
    78  			return errors.Trace(ctx.Err())
    79  		case <-t.C:
    80  		}
    81  	}
    82  }
    83  
    84  // getBackoffInMs returns the duration to wait before next try
    85  // See https://www.awsarchitectureblog.com/2015/03/backoff.html
    86  func getBackoffInMs(backoffBaseInMs, backoffCapInMs, try float64) time.Duration {
    87  	temp := int64(math.Min(backoffCapInMs, backoffBaseInMs*math.Exp2(try)) / 2)
    88  	if temp <= 0 {
    89  		temp = 1
    90  	}
    91  	sleep := (temp + rand.Int63n(temp)) * 3
    92  	if sleep <= 0 {
    93  		sleep = math.MaxInt64
    94  	}
    95  	backOff := math.Min(backoffCapInMs, float64(rand.Int63n(sleep))+backoffBaseInMs)
    96  	return time.Duration(backOff) * time.Millisecond
    97  }