github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/retry/options.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package retry 22 23 import ( 24 "math" 25 "math/rand" 26 "time" 27 28 "github.com/uber-go/tally" 29 ) 30 31 const ( 32 defaultInitialBackoff = time.Second 33 defaultBackoffFactor = 2.0 34 defaultMaxBackoff = time.Duration(math.MaxInt64) 35 defaultMaxRetries = 2 36 defaultForever = false 37 defaultJitter = true 38 ) 39 40 type options struct { 41 scope tally.Scope 42 initialBackoff time.Duration 43 backoffFactor float64 44 maxBackoff time.Duration 45 maxRetries int 46 forever bool 47 jitter bool 48 rngFn RngFn 49 } 50 51 // NewOptions creates new retry options. 52 func NewOptions() Options { 53 return &options{ 54 scope: tally.NoopScope, 55 initialBackoff: defaultInitialBackoff, 56 backoffFactor: defaultBackoffFactor, 57 maxBackoff: defaultMaxBackoff, 58 maxRetries: defaultMaxRetries, 59 forever: defaultForever, 60 jitter: defaultJitter, 61 rngFn: rand.Int63n, 62 } 63 } 64 65 func (o *options) SetMetricsScope(value tally.Scope) Options { 66 opts := *o 67 opts.scope = value 68 return &opts 69 } 70 71 func (o *options) MetricsScope() tally.Scope { 72 return o.scope 73 } 74 75 func (o *options) SetInitialBackoff(value time.Duration) Options { 76 opts := *o 77 opts.initialBackoff = value 78 return &opts 79 } 80 81 func (o *options) InitialBackoff() time.Duration { 82 return o.initialBackoff 83 } 84 85 func (o *options) SetBackoffFactor(value float64) Options { 86 opts := *o 87 opts.backoffFactor = value 88 return &opts 89 } 90 91 func (o *options) BackoffFactor() float64 { 92 return o.backoffFactor 93 } 94 95 func (o *options) SetMaxBackoff(value time.Duration) Options { 96 opts := *o 97 opts.maxBackoff = value 98 return &opts 99 } 100 101 func (o *options) MaxBackoff() time.Duration { 102 return o.maxBackoff 103 } 104 105 func (o *options) SetMaxRetries(value int) Options { 106 opts := *o 107 opts.maxRetries = value 108 return &opts 109 } 110 111 func (o *options) MaxRetries() int { 112 return o.maxRetries 113 } 114 115 func (o *options) SetForever(value bool) Options { 116 opts := *o 117 opts.forever = value 118 return &opts 119 } 120 121 func (o *options) Forever() bool { 122 return o.forever 123 } 124 125 func (o *options) SetJitter(value bool) Options { 126 opts := *o 127 opts.jitter = value 128 return &opts 129 } 130 131 func (o *options) Jitter() bool { 132 return o.jitter 133 } 134 135 func (o *options) SetRngFn(value RngFn) Options { 136 opts := *o 137 opts.rngFn = value 138 return &opts 139 } 140 141 func (o *options) RngFn() RngFn { 142 return o.rngFn 143 }