| // |
| 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 implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package wait |
| 16 |
| 17 | import ( |
| 18 | "math/rand/v2" |
| 19 | "time" |
| 20 |
| 21 | "k8s.io/utils/clock" |
| 22 |
| 23 | "github.com/fatedier/frp/pkg/util/util" |
| 24 | ) |
| 25 |
| 26 | type BackoffFunc func(previousDuration time.Duration, previousConditionError bool) time.Duration |
| 27 |
| 28 | func (f BackoffFunc) Backoff(previousDuration time.Duration, previousConditionError bool) time.Duration { |
| 29 | return f(previousDuration, previousConditionError) |
| 30 | } |
| 31 |
| 32 | type BackoffManager interface { |
| 33 | Backoff(previousDuration time.Duration, previousConditionError bool) time.Duration |
| 34 | } |
| 35 |
| 36 | type FastBackoffOptions struct { |
| 37 | Duration time.Duration |
| 38 | Factor float64 |
| 39 | Jitter float64 |
| 40 | MaxDuration time.Duration |
| 41 | InitDurationIfFail time.Duration |
| 42 |
| 43 | // If FastRetryCount > 0, then within the FastRetryWindow time window, |
| 44 | // the retry will be performed with a delay of FastRetryDelay for the first FastRetryCount calls. |
| 45 | FastRetryCount int |
| 46 | FastRetryDelay time.Duration |
| 47 | FastRetryJitter float64 |
| 48 | FastRetryWindow time.Duration |
| 49 | } |
| 50 |
| 51 | type fastBackoffImpl struct { |
| 52 | options FastBackoffOptions |
| 53 | clock clock.PassiveClock |
| 54 |
| 55 | lastCalledTime time.Time |
| 56 | consecutiveErrCount int |
| 57 |
| 58 | fastRetryCutoffTime time.Time |
| 59 | countsInFastRetryWindow int |
| 60 | } |
| 61 |
| 62 | func NewFastBackoffManager(options FastBackoffOptions) BackoffManager { |
| 63 | return newFastBackoffManagerWithClock(options, clock.RealClock{}) |
| 64 | } |
| 65 |
| 66 | func newFastBackoffManagerWithClock(options FastBackoffOptions, clk clock.PassiveClock) BackoffManager { |
| 67 | if clk == nil { |
| 68 | clk = clock.RealClock{} |
| 69 | } |
| 70 | return &fastBackoffImpl{ |
| 71 | options: options, |
| 72 | clock: clk, |
| 73 | countsInFastRetryWindow: 1, |
| 74 | } |
| 75 | } |
| 76 |
| 77 | func (f *fastBackoffImpl) Backoff(previousDuration time.Duration, previousConditionError bool) time.Duration { |
| 78 | if f.lastCalledTime.IsZero() { |
| 79 | f.lastCalledTime = f.clock.Now() |
| 80 | return f.options.Duration |
| 81 | } |
| 82 | now := f.clock.Now() |
| 83 | f.lastCalledTime = now |
| 84 |
| 85 | if previousConditionError { |
| 86 | f.consecutiveErrCount++ |
| 87 | } else { |
| 88 | f.consecutiveErrCount = 0 |
| 89 | } |
| 90 |
| 91 | if f.options.FastRetryCount > 0 && previousConditionError { |
| 92 | f.countsInFastRetryWindow++ |
| 93 | if f.countsInFastRetryWindow <= f.options.FastRetryCount { |
| 94 | return Jitter(f.options.FastRetryDelay, f.options.FastRetryJitter) |
| 95 | } |
| 96 | if now.After(f.fastRetryCutoffTime) { |
| 97 | // reset |
| 98 | f.fastRetryCutoffTime = now.Add(f.options.FastRetryWindow) |
| 99 | f.countsInFastRetryWindow = 0 |
| 100 | } |
| 101 | } |
| 102 |
| 103 | if previousConditionError { |
| 104 | var duration time.Duration |
| 105 | if f.consecutiveErrCount == 1 { |
| 106 | duration = util.EmptyOr(f.options.InitDurationIfFail, previousDuration) |
| 107 | } else { |
| 108 | duration = previousDuration |
| 109 | } |
| 110 |
| 111 | duration = util.EmptyOr(duration, time.Second) |
| 112 | if f.options.Factor != 0 { |
| 113 | duration = time.Duration(float64(duration) * f.options.Factor) |
| 114 | } |
| 115 | if f.options.Jitter > 0 { |
| 116 | duration = Jitter(duration, f.options.Jitter) |
| 117 | } |
| 118 | if f.options.MaxDuration > 0 && duration > f.options.MaxDuration { |
| 119 | duration = f.options.MaxDuration |
| 120 | } |
| 121 | return duration |
| 122 | } |
| 123 | return f.options.Duration |
| 124 | } |
| 125 |
| 126 | func BackoffUntil(f func() (bool, error), backoff BackoffManager, sliding bool, stopCh <-chan struct{}) { |
| 127 | var delay time.Duration |
| 128 | previousError := false |
| 129 |
| 130 | ticker := time.NewTicker(backoff.Backoff(delay, previousError)) |
| 131 | defer ticker.Stop() |
| 132 |
| 133 | for { |
| 134 | select { |
| 135 | case <-stopCh: |
| 136 | return |
| 137 | default: |
| 138 | } |
| 139 |
| 140 | if !sliding { |
| 141 | delay = backoff.Backoff(delay, previousError) |
| 142 | } |
| 143 |
| 144 | if done, err := f(); done { |
| 145 | return |
| 146 | } else if err != nil { |
| 147 | previousError = true |
| 148 | } else { |
| 149 | previousError = false |
| 150 | } |
| 151 |
| 152 | if sliding { |
| 153 | delay = backoff.Backoff(delay, previousError) |
| 154 | } |
| 155 |
| 156 | ticker.Reset(delay) |
| 157 | select { |
| 158 | case <-stopCh: |
| 159 | return |
| 160 | case <-ticker.C: |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 |
| 165 | // Jitter returns a time.Duration between duration and duration + maxFactor * |
| 166 | // duration. |
| 167 | // |
| 168 | // This allows clients to avoid converging on periodic behavior. If maxFactor |
| 169 | // is 0.0, a suggested default value will be chosen. |
| 170 | func Jitter(duration time.Duration, maxFactor float64) time.Duration { |
| 171 | if maxFactor <= 0.0 { |
| 172 | maxFactor = 1.0 |
| 173 | } |
| 174 | wait := duration + time.Duration(rand.Float64()*maxFactor*float64(duration)) |
| 175 | return wait |
| 176 | } |
| 177 |
| 178 | func Until(f func(), period time.Duration, stopCh <-chan struct{}) { |
| 179 | ff := func() (bool, error) { |
| 180 | f() |
| 181 | return false, nil |
| 182 | } |
| 183 | BackoffUntil(ff, BackoffFunc(func(time.Duration, bool) time.Duration { |
| 184 | return period |
| 185 | }), true, stopCh) |
| 186 | } |