github.com/m3db/m3@v1.5.0/src/x/retry/config.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 "time" 25 26 "github.com/uber-go/tally" 27 ) 28 29 // Configuration configures options for retry attempts. 30 type Configuration struct { 31 // Initial retry backoff. 32 InitialBackoff time.Duration `yaml:"initialBackoff" validate:"min=0"` 33 34 // Backoff factor for exponential backoff. 35 BackoffFactor float64 `yaml:"backoffFactor" validate:"min=0"` 36 37 // Maximum backoff time. 38 MaxBackoff time.Duration `yaml:"maxBackoff" validate:"min=0"` 39 40 // Maximum number of retry attempts. 41 MaxRetries int `yaml:"maxRetries"` 42 43 // Whether to retry forever until either the attempt succeeds, 44 // or the retry condition becomes false. 45 Forever *bool `yaml:"forever"` 46 47 // Whether jittering is applied during retries. 48 Jitter *bool `yaml:"jitter"` 49 } 50 51 // NewOptions creates a new retry options based on the configuration. 52 func (c Configuration) NewOptions(scope tally.Scope) Options { 53 opts := NewOptions().SetMetricsScope(scope) 54 if c.InitialBackoff != 0 { 55 opts = opts.SetInitialBackoff(c.InitialBackoff) 56 } 57 if c.BackoffFactor != 0.0 { 58 opts = opts.SetBackoffFactor(c.BackoffFactor) 59 } 60 if c.MaxBackoff != 0 { 61 opts = opts.SetMaxBackoff(c.MaxBackoff) 62 } 63 if c.MaxRetries != 0 { 64 opts = opts.SetMaxRetries(c.MaxRetries) 65 } 66 if c.Forever != nil { 67 opts = opts.SetForever(*c.Forever) 68 } 69 if c.Jitter != nil { 70 opts = opts.SetJitter(*c.Jitter) 71 } 72 73 return opts 74 } 75 76 // NewRetrier creates a new retrier based on the configuration. 77 func (c Configuration) NewRetrier(scope tally.Scope) Retrier { 78 return NewRetrier(c.NewOptions(scope)) 79 }