github.com/m3db/m3@v1.5.0/src/x/retry/types.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 provides utilities for retrying functions. 22 package retry 23 24 import ( 25 "time" 26 27 "github.com/m3db/m3/src/x/errors" 28 29 "github.com/uber-go/tally" 30 ) 31 32 // RetryableError returns a retryable error. 33 func RetryableError(err error) error { 34 return errors.NewRetryableError(err) 35 } 36 37 // NonRetryableError returns a non-retryable error. 38 func NonRetryableError(err error) error { 39 return errors.NewNonRetryableError(err) 40 } 41 42 // RngFn returns a non-negative pseudo-random number in [0,n). 43 type RngFn func(n int64) int64 44 45 // Fn is a function that can be retried. 46 type Fn func() error 47 48 // ContinueFn is a function that returns whether to continue attempting an operation. 49 type ContinueFn func(attempt int) bool 50 51 // Retrier is a executor that can retry attempts on executing methods. 52 type Retrier interface { 53 // Options returns the options used to construct the retrier, useful 54 // for changing instrumentation options, etc while preserving other options. 55 Options() Options 56 57 // Attempt will attempt to perform a function with retries. 58 Attempt(fn Fn) error 59 60 // AttemptWhile will attempt to perform a function with retries. 61 AttemptWhile(continueFn ContinueFn, fn Fn) error 62 } 63 64 // Options is a set of retry options. 65 type Options interface { 66 // SetMetricsScope sets the metrics scope. 67 SetMetricsScope(value tally.Scope) Options 68 69 // MetricsScope returns the metrics scope. 70 MetricsScope() tally.Scope 71 72 // SetInitialBackoff sets the initial delay duration. 73 SetInitialBackoff(value time.Duration) Options 74 75 // InitialBackoff gets the initial delay duration. 76 InitialBackoff() time.Duration 77 78 // SetBackoffFactor sets the backoff factor multiplier when moving to next attempt. 79 SetBackoffFactor(value float64) Options 80 81 // BackoffFactor gets the backoff factor multiplier when moving to next attempt. 82 BackoffFactor() float64 83 84 // SetMaxBackoff sets the maximum backoff delay. 85 SetMaxBackoff(value time.Duration) Options 86 87 // MaxBackoff returns the maximum backoff delay. 88 MaxBackoff() time.Duration 89 90 // SetMaxRetries sets the maximum retry attempts. 91 SetMaxRetries(value int) Options 92 93 // Max gets the maximum retry attempts. 94 MaxRetries() int 95 96 // SetForever sets whether to retry forever until either the attempt succeeds, 97 // or the retry condition becomes false. 98 SetForever(value bool) Options 99 100 // Forever returns whether to retry forever until either the attempt succeeds, 101 // or the retry condition becomes false. 102 Forever() bool 103 104 // SetJitter sets whether to jitter between the current backoff and the next 105 // backoff when moving to next attempt. 106 SetJitter(value bool) Options 107 108 // Jitter gets whether to jitter between the current backoff and the next 109 // backoff when moving to next attempt. 110 Jitter() bool 111 112 // SetRngFn sets the RngFn. 113 SetRngFn(value RngFn) Options 114 115 // RngFn returns the RngFn. 116 RngFn() RngFn 117 }