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