loop.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2023 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. "time"
  17. "k8s.io/apimachinery/pkg/util/runtime"
  18. )
  19. // loopConditionUntilContext executes the provided condition at intervals defined by
  20. // the provided timer until the provided context is cancelled, the condition returns
  21. // true, or the condition returns an error. If sliding is true, the period is computed
  22. // after condition runs. If it is false then period includes the runtime for condition.
  23. // If immediate is false the first delay happens before any call to condition, if
  24. // immediate is true the condition will be invoked before waiting and guarantees that
  25. // the condition is invoked at least once, regardless of whether the context has been
  26. // cancelled. The returned error is the error returned by the last condition or the
  27. // context error if the context was terminated.
  28. //
  29. // This is the common loop construct for all polling in the wait package.
  30. func loopConditionUntilContext(ctx context.Context, t Timer, immediate, sliding bool, condition ConditionWithContextFunc) error {
  31. defer t.Stop()
  32. var timeCh <-chan time.Time
  33. doneCh := ctx.Done()
  34. // if immediate is true the condition is
  35. // guaranteed to be executed at least once,
  36. // if we haven't requested immediate execution, delay once
  37. if immediate {
  38. if ok, err := func() (bool, error) {
  39. defer runtime.HandleCrash()
  40. return condition(ctx)
  41. }(); err != nil || ok {
  42. return err
  43. }
  44. } else {
  45. timeCh = t.C()
  46. select {
  47. case <-doneCh:
  48. return ctx.Err()
  49. case <-timeCh:
  50. }
  51. }
  52. for {
  53. // checking ctx.Err() is slightly faster than checking a select
  54. if err := ctx.Err(); err != nil {
  55. return err
  56. }
  57. if !sliding {
  58. t.Next()
  59. }
  60. if ok, err := func() (bool, error) {
  61. defer runtime.HandleCrash()
  62. return condition(ctx)
  63. }(); err != nil || ok {
  64. return err
  65. }
  66. if sliding {
  67. t.Next()
  68. }
  69. if timeCh == nil {
  70. timeCh = t.C()
  71. }
  72. // NOTE: b/c there is no priority selection in golang
  73. // it is possible for this to race, meaning we could
  74. // trigger t.C and doneCh, and t.C select falls through.
  75. // In order to mitigate we re-check doneCh at the beginning
  76. // of every loop to guarantee at-most one extra execution
  77. // of condition.
  78. select {
  79. case <-doneCh:
  80. return ctx.Err()
  81. case <-timeCh:
  82. }
  83. }
  84. }