1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package wait 18 19 import ( 20 "context" 21 "time" 22 23 "k8s.io/apimachinery/pkg/util/runtime" 24 ) 25 26 // loopConditionUntilContext executes the provided condition at intervals defined by 27 // the provided timer until the provided context is cancelled, the condition returns 28 // true, or the condition returns an error. If sliding is true, the period is computed 29 // after condition runs. If it is false then period includes the runtime for condition. 30 // If immediate is false the first delay happens before any call to condition, if 31 // immediate is true the condition will be invoked before waiting and guarantees that 32 // the condition is invoked at least once, regardless of whether the context has been 33 // cancelled. The returned error is the error returned by the last condition or the 34 // context error if the context was terminated. 35 // 36 // This is the common loop construct for all polling in the wait package. 37 func loopConditionUntilContext(ctx context.Context, t Timer, immediate, sliding bool, condition ConditionWithContextFunc) error { 38 defer t.Stop() 39 40 var timeCh <-chan time.Time 41 doneCh := ctx.Done() 42 43 if !sliding { 44 timeCh = t.C() 45 } 46 47 // if immediate is true the condition is 48 // guaranteed to be executed at least once, 49 // if we haven't requested immediate execution, delay once 50 if immediate { 51 if ok, err := func() (bool, error) { 52 defer runtime.HandleCrash() 53 return condition(ctx) 54 }(); err != nil || ok { 55 return err 56 } 57 } 58 59 if sliding { 60 timeCh = t.C() 61 } 62 63 for { 64 65 // Wait for either the context to be cancelled or the next invocation be called 66 select { 67 case <-doneCh: 68 return ctx.Err() 69 case <-timeCh: 70 } 71 72 // IMPORTANT: Because there is no channel priority selection in golang 73 // it is possible for very short timers to "win" the race in the previous select 74 // repeatedly even when the context has been canceled. We therefore must 75 // explicitly check for context cancellation on every loop and exit if true to 76 // guarantee that we don't invoke condition more than once after context has 77 // been cancelled. 78 if err := ctx.Err(); err != nil { 79 return err 80 } 81 82 if !sliding { 83 t.Next() 84 } 85 if ok, err := func() (bool, error) { 86 defer runtime.HandleCrash() 87 return condition(ctx) 88 }(); err != nil || ok { 89 return err 90 } 91 if sliding { 92 t.Next() 93 } 94 } 95 } 96