github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/retry/options.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  	"math"
    18  	"time"
    19  )
    20  
    21  const (
    22  	// defaultBackoffBaseInMs is the initial duration, in Millisecond
    23  	defaultBackoffBaseInMs = 10.0
    24  	// defaultBackoffCapInMs is the max amount of duration, in Millisecond
    25  	defaultBackoffCapInMs   = 100.0
    26  	defaultMaxTries         = math.MaxUint64
    27  	defaultMaxRetryDuration = time.Duration(0)
    28  )
    29  
    30  // Option ...
    31  type Option func(*retryOptions)
    32  
    33  // IsRetryable checks the error is safe or worth to retry, eg. "context.Canceled" better not retry
    34  type IsRetryable func(error) bool
    35  
    36  type retryOptions struct {
    37  	totalRetryDuration time.Duration
    38  	maxTries           uint64
    39  	backoffBaseInMs    float64
    40  	backoffCapInMs     float64
    41  	isRetryable        IsRetryable
    42  }
    43  
    44  func newRetryOptions() *retryOptions {
    45  	return &retryOptions{
    46  		totalRetryDuration: defaultMaxRetryDuration,
    47  		maxTries:           defaultMaxTries,
    48  		backoffBaseInMs:    defaultBackoffBaseInMs,
    49  		backoffCapInMs:     defaultBackoffCapInMs,
    50  		isRetryable:        func(err error) bool { return true },
    51  	}
    52  }
    53  
    54  // WithBackoffBaseDelay configures the initial delay, if delayInMs <= 0 "defaultBackoffBaseInMs" will be used
    55  func WithBackoffBaseDelay(delayInMs int64) Option {
    56  	return func(o *retryOptions) {
    57  		if delayInMs > 0 {
    58  			o.backoffBaseInMs = float64(delayInMs)
    59  		}
    60  	}
    61  }
    62  
    63  // WithBackoffMaxDelay configures the maximum delay, if delayInMs <= 0 "defaultBackoffCapInMs" will be used
    64  func WithBackoffMaxDelay(delayInMs int64) Option {
    65  	return func(o *retryOptions) {
    66  		if delayInMs > 0 {
    67  			o.backoffCapInMs = float64(delayInMs)
    68  		}
    69  	}
    70  }
    71  
    72  // WithMaxTries configures maximum tries, if tries is 0, 1 will be used
    73  func WithMaxTries(tries uint64) Option {
    74  	return func(o *retryOptions) {
    75  		if tries == 0 {
    76  			tries = 1
    77  		}
    78  		o.maxTries = tries
    79  	}
    80  }
    81  
    82  // WithTotalRetryDuratoin configures the total retry duration.
    83  func WithTotalRetryDuratoin(retryDuration time.Duration) Option {
    84  	return func(o *retryOptions) {
    85  		o.totalRetryDuration = retryDuration
    86  	}
    87  }
    88  
    89  // WithIsRetryableErr configures the error should retry or not, if not set, retry by default
    90  func WithIsRetryableErr(f IsRetryable) Option {
    91  	return func(o *retryOptions) {
    92  		if f != nil {
    93  			o.isRetryable = f
    94  		}
    95  	}
    96  }