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