blob: a25e92465413f4a1b009267c480f047ab7b40bec [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package wait
18
19import (
20 "context"
21 "errors"
22 "math/rand"
23 "sync"
24 "time"
25
26 "k8s.io/apimachinery/pkg/util/runtime"
27)
28
29// For any test of the style:
30// ...
31// <- time.After(timeout):
32// t.Errorf("Timed out")
33// The value for timeout should effectively be "forever." Obviously we don't want our tests to truly lock up forever, but 30s
34// is long enough that it is effectively forever for the things that can slow down a run on a heavily contended machine
35// (GC, seeks, etc), but not so long as to make a developer ctrl-c a test run if they do happen to break that test.
36var ForeverTestTimeout = time.Second * 30
37
38// NeverStop may be passed to Until to make it never stop.
39var NeverStop <-chan struct{} = make(chan struct{})
40
41// Group allows to start a group of goroutines and wait for their completion.
42type Group struct {
43 wg sync.WaitGroup
44}
45
46func (g *Group) Wait() {
47 g.wg.Wait()
48}
49
50// StartWithChannel starts f in a new goroutine in the group.
51// stopCh is passed to f as an argument. f should stop when stopCh is available.
52func (g *Group) StartWithChannel(stopCh <-chan struct{}, f func(stopCh <-chan struct{})) {
53 g.Start(func() {
54 f(stopCh)
55 })
56}
57
58// StartWithContext starts f in a new goroutine in the group.
59// ctx is passed to f as an argument. f should stop when ctx.Done() is available.
60func (g *Group) StartWithContext(ctx context.Context, f func(context.Context)) {
61 g.Start(func() {
62 f(ctx)
63 })
64}
65
66// Start starts f in a new goroutine in the group.
67func (g *Group) Start(f func()) {
68 g.wg.Add(1)
69 go func() {
70 defer g.wg.Done()
71 f()
72 }()
73}
74
75// Forever calls f every period for ever.
76//
77// Forever is syntactic sugar on top of Until.
78func Forever(f func(), period time.Duration) {
79 Until(f, period, NeverStop)
80}
81
82// Until loops until stop channel is closed, running f every period.
83//
84// Until is syntactic sugar on top of JitterUntil with zero jitter factor and
85// with sliding = true (which means the timer for period starts after the f
86// completes).
87func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
88 JitterUntil(f, period, 0.0, true, stopCh)
89}
90
91// NonSlidingUntil loops until stop channel is closed, running f every
92// period.
93//
94// NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter
95// factor, with sliding = false (meaning the timer for period starts at the same
96// time as the function starts).
97func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
98 JitterUntil(f, period, 0.0, false, stopCh)
99}
100
101// JitterUntil loops until stop channel is closed, running f every period.
102//
103// If jitterFactor is positive, the period is jittered before every run of f.
104// If jitterFactor is not positive, the period is unchanged and not jittered.
105//
106// If sliding is true, the period is computed after f runs. If it is false then
107// period includes the runtime for f.
108//
109// Close stopCh to stop. f may not be invoked if stop channel is already
110// closed. Pass NeverStop to if you don't want it stop.
111func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{}) {
112 var t *time.Timer
113 var sawTimeout bool
114
115 for {
116 select {
117 case <-stopCh:
118 return
119 default:
120 }
121
122 jitteredPeriod := period
123 if jitterFactor > 0.0 {
124 jitteredPeriod = Jitter(period, jitterFactor)
125 }
126
127 if !sliding {
128 t = resetOrReuseTimer(t, jitteredPeriod, sawTimeout)
129 }
130
131 func() {
132 defer runtime.HandleCrash()
133 f()
134 }()
135
136 if sliding {
137 t = resetOrReuseTimer(t, jitteredPeriod, sawTimeout)
138 }
139
140 // NOTE: b/c there is no priority selection in golang
141 // it is possible for this to race, meaning we could
142 // trigger t.C and stopCh, and t.C select falls through.
143 // In order to mitigate we re-check stopCh at the beginning
144 // of every loop to prevent extra executions of f().
145 select {
146 case <-stopCh:
147 return
148 case <-t.C:
149 sawTimeout = true
150 }
151 }
152}
153
154// Jitter returns a time.Duration between duration and duration + maxFactor *
155// duration.
156//
157// This allows clients to avoid converging on periodic behavior. If maxFactor
158// is 0.0, a suggested default value will be chosen.
159func Jitter(duration time.Duration, maxFactor float64) time.Duration {
160 if maxFactor <= 0.0 {
161 maxFactor = 1.0
162 }
163 wait := duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
164 return wait
165}
166
167// ErrWaitTimeout is returned when the condition exited without success.
168var ErrWaitTimeout = errors.New("timed out waiting for the condition")
169
170// ConditionFunc returns true if the condition is satisfied, or an error
171// if the loop should be aborted.
172type ConditionFunc func() (done bool, err error)
173
174// Backoff holds parameters applied to a Backoff function.
175type Backoff struct {
176 Duration time.Duration // the base duration
177 Factor float64 // Duration is multiplied by factor each iteration
178 Jitter float64 // The amount of jitter applied each iteration
179 Steps int // Exit with error after this many steps
180}
181
182// ExponentialBackoff repeats a condition check with exponential backoff.
183//
184// It checks the condition up to Steps times, increasing the wait by multiplying
185// the previous duration by Factor.
186//
187// If Jitter is greater than zero, a random amount of each duration is added
188// (between duration and duration*(1+jitter)).
189//
190// If the condition never returns true, ErrWaitTimeout is returned. All other
191// errors terminate immediately.
192func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
193 duration := backoff.Duration
194 for i := 0; i < backoff.Steps; i++ {
195 if i != 0 {
196 adjusted := duration
197 if backoff.Jitter > 0.0 {
198 adjusted = Jitter(duration, backoff.Jitter)
199 }
200 time.Sleep(adjusted)
201 duration = time.Duration(float64(duration) * backoff.Factor)
202 }
203 if ok, err := condition(); err != nil || ok {
204 return err
205 }
206 }
207 return ErrWaitTimeout
208}
209
210// Poll tries a condition func until it returns true, an error, or the timeout
211// is reached.
212//
213// Poll always waits the interval before the run of 'condition'.
214// 'condition' will always be invoked at least once.
215//
216// Some intervals may be missed if the condition takes too long or the time
217// window is too short.
218//
219// If you want to Poll something forever, see PollInfinite.
220func Poll(interval, timeout time.Duration, condition ConditionFunc) error {
221 return pollInternal(poller(interval, timeout), condition)
222}
223
224func pollInternal(wait WaitFunc, condition ConditionFunc) error {
225 done := make(chan struct{})
226 defer close(done)
227 return WaitFor(wait, condition, done)
228}
229
230// PollImmediate tries a condition func until it returns true, an error, or the timeout
231// is reached.
232//
233// Poll always checks 'condition' before waiting for the interval. 'condition'
234// will always be invoked at least once.
235//
236// Some intervals may be missed if the condition takes too long or the time
237// window is too short.
238//
239// If you want to Poll something forever, see PollInfinite.
240func PollImmediate(interval, timeout time.Duration, condition ConditionFunc) error {
241 return pollImmediateInternal(poller(interval, timeout), condition)
242}
243
244func pollImmediateInternal(wait WaitFunc, condition ConditionFunc) error {
245 done, err := condition()
246 if err != nil {
247 return err
248 }
249 if done {
250 return nil
251 }
252 return pollInternal(wait, condition)
253}
254
255// PollInfinite tries a condition func until it returns true or an error
256//
257// PollInfinite always waits the interval before the run of 'condition'.
258//
259// Some intervals may be missed if the condition takes too long or the time
260// window is too short.
261func PollInfinite(interval time.Duration, condition ConditionFunc) error {
262 done := make(chan struct{})
263 defer close(done)
264 return PollUntil(interval, condition, done)
265}
266
267// PollImmediateInfinite tries a condition func until it returns true or an error
268//
269// PollImmediateInfinite runs the 'condition' before waiting for the interval.
270//
271// Some intervals may be missed if the condition takes too long or the time
272// window is too short.
273func PollImmediateInfinite(interval time.Duration, condition ConditionFunc) error {
274 done, err := condition()
275 if err != nil {
276 return err
277 }
278 if done {
279 return nil
280 }
281 return PollInfinite(interval, condition)
282}
283
284// PollUntil tries a condition func until it returns true, an error or stopCh is
285// closed.
286//
287// PollUntil always waits interval before the first run of 'condition'.
288// 'condition' will always be invoked at least once.
289func PollUntil(interval time.Duration, condition ConditionFunc, stopCh <-chan struct{}) error {
290 return WaitFor(poller(interval, 0), condition, stopCh)
291}
292
293// PollImmediateUntil tries a condition func until it returns true, an error or stopCh is closed.
294//
295// PollImmediateUntil runs the 'condition' before waiting for the interval.
296// 'condition' will always be invoked at least once.
297func PollImmediateUntil(interval time.Duration, condition ConditionFunc, stopCh <-chan struct{}) error {
298 done, err := condition()
299 if err != nil {
300 return err
301 }
302 if done {
303 return nil
304 }
305 select {
306 case <-stopCh:
307 return ErrWaitTimeout
308 default:
309 return PollUntil(interval, condition, stopCh)
310 }
311}
312
313// WaitFunc creates a channel that receives an item every time a test
314// should be executed and is closed when the last test should be invoked.
315type WaitFunc func(done <-chan struct{}) <-chan struct{}
316
317// WaitFor continually checks 'fn' as driven by 'wait'.
318//
319// WaitFor gets a channel from 'wait()'', and then invokes 'fn' once for every value
320// placed on the channel and once more when the channel is closed.
321//
322// If 'fn' returns an error the loop ends and that error is returned, and if
323// 'fn' returns true the loop ends and nil is returned.
324//
325// ErrWaitTimeout will be returned if the channel is closed without fn ever
326// returning true.
327func WaitFor(wait WaitFunc, fn ConditionFunc, done <-chan struct{}) error {
328 c := wait(done)
329 for {
330 _, open := <-c
331 ok, err := fn()
332 if err != nil {
333 return err
334 }
335 if ok {
336 return nil
337 }
338 if !open {
339 break
340 }
341 }
342 return ErrWaitTimeout
343}
344
345// poller returns a WaitFunc that will send to the channel every interval until
346// timeout has elapsed and then closes the channel.
347//
348// Over very short intervals you may receive no ticks before the channel is
349// closed. A timeout of 0 is interpreted as an infinity.
350//
351// Output ticks are not buffered. If the channel is not ready to receive an
352// item, the tick is skipped.
353func poller(interval, timeout time.Duration) WaitFunc {
354 return WaitFunc(func(done <-chan struct{}) <-chan struct{} {
355 ch := make(chan struct{})
356
357 go func() {
358 defer close(ch)
359
360 tick := time.NewTicker(interval)
361 defer tick.Stop()
362
363 var after <-chan time.Time
364 if timeout != 0 {
365 // time.After is more convenient, but it
366 // potentially leaves timers around much longer
367 // than necessary if we exit early.
368 timer := time.NewTimer(timeout)
369 after = timer.C
370 defer timer.Stop()
371 }
372
373 for {
374 select {
375 case <-tick.C:
376 // If the consumer isn't ready for this signal drop it and
377 // check the other channels.
378 select {
379 case ch <- struct{}{}:
380 default:
381 }
382 case <-after:
383 return
384 case <-done:
385 return
386 }
387 }
388 }()
389
390 return ch
391 })
392}
393
394// resetOrReuseTimer avoids allocating a new timer if one is already in use.
395// Not safe for multiple threads.
396func resetOrReuseTimer(t *time.Timer, d time.Duration, sawTimeout bool) *time.Timer {
397 if t == nil {
398 return time.NewTimer(d)
399 }
400 if !t.Stop() && !sawTimeout {
401 <-t.C
402 }
403 t.Reset(d)
404 return t
405}