|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 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 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +package crdb |
| 16 | + |
| 17 | +import ( |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +// RetryFunc owns the state for a transaction retry operation. Usually, this is |
| 22 | +// just the retry count. RetryFunc is not assumed to be safe for concurrent use. |
| 23 | +type RetryFunc func(err error) (time.Duration, error) |
| 24 | + |
| 25 | +// RetryPolicy constructs a new instance of a RetryFunc for each transaction |
| 26 | +// it is used with. Instances of RetryPolicy can likely be immutable and |
| 27 | +// should be safe for concurrent calls to NewRetry. |
| 28 | +type RetryPolicy interface { |
| 29 | + NewRetry() RetryFunc |
| 30 | +} |
| 31 | + |
| 32 | +type LimitBackoffRetryPolicy struct { |
| 33 | + RetryLimit int |
| 34 | + Delay time.Duration |
| 35 | +} |
| 36 | + |
| 37 | +func (l *LimitBackoffRetryPolicy) NewRetry() RetryFunc { |
| 38 | + tryCount := 0 |
| 39 | + return func(err error) (time.Duration, error) { |
| 40 | + tryCount++ |
| 41 | + if tryCount > l.RetryLimit { |
| 42 | + return 0, newMaxRetriesExceededError(err, l.RetryLimit) |
| 43 | + } |
| 44 | + return l.Delay, nil |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// ExpBackoffRetryPolicy implements RetryPolicy using an exponential backoff with optional |
| 49 | +// saturation. |
| 50 | +type ExpBackoffRetryPolicy struct { |
| 51 | + RetryLimit int |
| 52 | + BaseDelay time.Duration |
| 53 | + MaxDelay time.Duration |
| 54 | +} |
| 55 | + |
| 56 | +// NewRetry implements RetryPolicy |
| 57 | +func (l *ExpBackoffRetryPolicy) NewRetry() RetryFunc { |
| 58 | + tryCount := 0 |
| 59 | + return func(err error) (time.Duration, error) { |
| 60 | + tryCount++ |
| 61 | + if tryCount > l.RetryLimit { |
| 62 | + return 0, newMaxRetriesExceededError(err, l.RetryLimit) |
| 63 | + } |
| 64 | + delay := l.BaseDelay << (tryCount - 1) |
| 65 | + if l.MaxDelay > 0 && delay > l.MaxDelay { |
| 66 | + return l.MaxDelay, nil |
| 67 | + } |
| 68 | + if delay < l.BaseDelay { |
| 69 | + // We've overflowed. |
| 70 | + if l.MaxDelay > 0 { |
| 71 | + return l.MaxDelay, nil |
| 72 | + } |
| 73 | + // There's no max delay. Giving up is probably better in |
| 74 | + // practice than using a 290-year MAX_INT delay. |
| 75 | + return 0, newMaxRetriesExceededError(err, tryCount) |
| 76 | + } |
| 77 | + return delay, nil |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Vargo converts a go-retry style Delay provider into a RetryPolicy |
| 82 | +func Vargo(fn func() VargoBackoff) RetryPolicy { |
| 83 | + return &vargoAdapter{ |
| 84 | + DelegateFactory: fn, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +type VargoBackoff interface { |
| 89 | + Next() (next time.Duration, stop bool) |
| 90 | +} |
| 91 | + |
| 92 | +// vargoAdapter adapts backoff policies in the style of sethvargo/go-retry |
| 93 | +type vargoAdapter struct { |
| 94 | + DelegateFactory func() VargoBackoff |
| 95 | +} |
| 96 | + |
| 97 | +func (b *vargoAdapter) NewRetry() RetryFunc { |
| 98 | + delegate := b.DelegateFactory() |
| 99 | + return func(err error) (time.Duration, error) { |
| 100 | + d, stop := delegate.Next() |
| 101 | + if stop { |
| 102 | + return 0, err |
| 103 | + } |
| 104 | + return d, nil |
| 105 | + } |
| 106 | +} |
0 commit comments