wait.go 12 KB

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