mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #129346 from pohly/log-client-go-apimachinery-wait
apimachinery wait: support contextual logging
This commit is contained in:
commit
2a425157ba
@ -157,6 +157,8 @@ func (b Backoff) DelayWithReset(c clock.Clock, resetInterval time.Duration) Dela
|
|||||||
// Until is syntactic sugar on top of JitterUntil with zero jitter factor and
|
// Until is syntactic sugar on top of JitterUntil with zero jitter factor and
|
||||||
// with sliding = true (which means the timer for period starts after the f
|
// with sliding = true (which means the timer for period starts after the f
|
||||||
// completes).
|
// completes).
|
||||||
|
//
|
||||||
|
// Contextual logging: UntilWithContext should be used instead of Until in code which supports contextual logging.
|
||||||
func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
||||||
JitterUntil(f, period, 0.0, true, stopCh)
|
JitterUntil(f, period, 0.0, true, stopCh)
|
||||||
}
|
}
|
||||||
@ -176,6 +178,8 @@ func UntilWithContext(ctx context.Context, f func(context.Context), period time.
|
|||||||
// NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter
|
// NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter
|
||||||
// factor, with sliding = false (meaning the timer for period starts at the same
|
// factor, with sliding = false (meaning the timer for period starts at the same
|
||||||
// time as the function starts).
|
// time as the function starts).
|
||||||
|
//
|
||||||
|
// Contextual logging: NonSlidingUntilWithContext should be used instead of NonSlidingUntil in code which supports contextual logging.
|
||||||
func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
|
func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
|
||||||
JitterUntil(f, period, 0.0, false, stopCh)
|
JitterUntil(f, period, 0.0, false, stopCh)
|
||||||
}
|
}
|
||||||
@ -200,19 +204,44 @@ func NonSlidingUntilWithContext(ctx context.Context, f func(context.Context), pe
|
|||||||
//
|
//
|
||||||
// Close stopCh to stop. f may not be invoked if stop channel is already
|
// Close stopCh to stop. f may not be invoked if stop channel is already
|
||||||
// closed. Pass NeverStop to if you don't want it stop.
|
// closed. Pass NeverStop to if you don't want it stop.
|
||||||
|
//
|
||||||
|
// Contextual logging: JitterUntilWithContext should be used instead of JitterUntil in code which supports contextual logging.
|
||||||
func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{}) {
|
func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{}) {
|
||||||
BackoffUntil(f, NewJitteredBackoffManager(period, jitterFactor, &clock.RealClock{}), sliding, stopCh)
|
BackoffUntil(f, NewJitteredBackoffManager(period, jitterFactor, &clock.RealClock{}), sliding, stopCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JitterUntilWithContext loops until context is done, running f every period.
|
||||||
|
//
|
||||||
|
// If jitterFactor is positive, the period is jittered before every run of f.
|
||||||
|
// If jitterFactor is not positive, the period is unchanged and not jittered.
|
||||||
|
//
|
||||||
|
// If sliding is true, the period is computed after f runs. If it is false then
|
||||||
|
// period includes the runtime for f.
|
||||||
|
//
|
||||||
|
// Cancel context to stop. f may not be invoked if context is already done.
|
||||||
|
func JitterUntilWithContext(ctx context.Context, f func(context.Context), period time.Duration, jitterFactor float64, sliding bool) {
|
||||||
|
BackoffUntilWithContext(ctx, f, NewJitteredBackoffManager(period, jitterFactor, &clock.RealClock{}), sliding)
|
||||||
|
}
|
||||||
|
|
||||||
// BackoffUntil loops until stop channel is closed, run f every duration given by BackoffManager.
|
// BackoffUntil loops until stop channel is closed, run f every duration given by BackoffManager.
|
||||||
//
|
//
|
||||||
// If sliding is true, the period is computed after f runs. If it is false then
|
// If sliding is true, the period is computed after f runs. If it is false then
|
||||||
// period includes the runtime for f.
|
// period includes the runtime for f.
|
||||||
|
//
|
||||||
|
// Contextual logging: BackoffUntilWithContext should be used instead of BackoffUntil in code which supports contextual logging.
|
||||||
func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan struct{}) {
|
func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan struct{}) {
|
||||||
|
BackoffUntilWithContext(ContextForChannel(stopCh), func(context.Context) { f() }, backoff, sliding)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BackoffUntilWithContext loops until context is done, run f every duration given by BackoffManager.
|
||||||
|
//
|
||||||
|
// If sliding is true, the period is computed after f runs. If it is false then
|
||||||
|
// period includes the runtime for f.
|
||||||
|
func BackoffUntilWithContext(ctx context.Context, f func(ctx context.Context), backoff BackoffManager, sliding bool) {
|
||||||
var t clock.Timer
|
var t clock.Timer
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-stopCh:
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
@ -222,8 +251,8 @@ func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan
|
|||||||
}
|
}
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrashWithContext(ctx)
|
||||||
f()
|
f(ctx)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if sliding {
|
if sliding {
|
||||||
@ -236,7 +265,7 @@ func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan
|
|||||||
// In order to mitigate we re-check stopCh at the beginning
|
// In order to mitigate we re-check stopCh at the beginning
|
||||||
// of every loop to prevent extra executions of f().
|
// of every loop to prevent extra executions of f().
|
||||||
select {
|
select {
|
||||||
case <-stopCh:
|
case <-ctx.Done():
|
||||||
if !t.Stop() {
|
if !t.Stop() {
|
||||||
<-t.C()
|
<-t.C()
|
||||||
}
|
}
|
||||||
@ -246,19 +275,6 @@ func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// JitterUntilWithContext loops until context is done, running f every period.
|
|
||||||
//
|
|
||||||
// If jitterFactor is positive, the period is jittered before every run of f.
|
|
||||||
// If jitterFactor is not positive, the period is unchanged and not jittered.
|
|
||||||
//
|
|
||||||
// If sliding is true, the period is computed after f runs. If it is false then
|
|
||||||
// period includes the runtime for f.
|
|
||||||
//
|
|
||||||
// Cancel context to stop. f may not be invoked if context is already expired.
|
|
||||||
func JitterUntilWithContext(ctx context.Context, f func(context.Context), period time.Duration, jitterFactor float64, sliding bool) {
|
|
||||||
JitterUntil(func() { f(ctx) }, period, jitterFactor, sliding, ctx.Done())
|
|
||||||
}
|
|
||||||
|
|
||||||
// backoffManager provides simple backoff behavior in a threadsafe manner to a caller.
|
// backoffManager provides simple backoff behavior in a threadsafe manner to a caller.
|
||||||
type backoffManager struct {
|
type backoffManager struct {
|
||||||
backoff Backoff
|
backoff Backoff
|
||||||
|
@ -49,7 +49,7 @@ func loopConditionUntilContext(ctx context.Context, t Timer, immediate, sliding
|
|||||||
// if we haven't requested immediate execution, delay once
|
// if we haven't requested immediate execution, delay once
|
||||||
if immediate {
|
if immediate {
|
||||||
if ok, err := func() (bool, error) {
|
if ok, err := func() (bool, error) {
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrashWithContext(ctx)
|
||||||
return condition(ctx)
|
return condition(ctx)
|
||||||
}(); err != nil || ok {
|
}(); err != nil || ok {
|
||||||
return err
|
return err
|
||||||
@ -83,7 +83,7 @@ func loopConditionUntilContext(ctx context.Context, t Timer, immediate, sliding
|
|||||||
t.Next()
|
t.Next()
|
||||||
}
|
}
|
||||||
if ok, err := func() (bool, error) {
|
if ok, err := func() (bool, error) {
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrashWithContext(ctx)
|
||||||
return condition(ctx)
|
return condition(ctx)
|
||||||
}(); err != nil || ok {
|
}(); err != nil || ok {
|
||||||
return err
|
return err
|
||||||
|
@ -141,6 +141,7 @@ func (c channelContext) Value(key any) any { return nil }
|
|||||||
//
|
//
|
||||||
// Deprecated: Will be removed when the legacy polling methods are removed.
|
// Deprecated: Will be removed when the legacy polling methods are removed.
|
||||||
func runConditionWithCrashProtection(condition ConditionFunc) (bool, error) {
|
func runConditionWithCrashProtection(condition ConditionFunc) (bool, error) {
|
||||||
|
//nolint:logcheck // Already deprecated.
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrash()
|
||||||
return condition()
|
return condition()
|
||||||
}
|
}
|
||||||
@ -150,7 +151,7 @@ func runConditionWithCrashProtection(condition ConditionFunc) (bool, error) {
|
|||||||
//
|
//
|
||||||
// Deprecated: Will be removed when the legacy polling methods are removed.
|
// Deprecated: Will be removed when the legacy polling methods are removed.
|
||||||
func runConditionWithCrashProtectionWithContext(ctx context.Context, condition ConditionWithContextFunc) (bool, error) {
|
func runConditionWithCrashProtectionWithContext(ctx context.Context, condition ConditionWithContextFunc) (bool, error) {
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrashWithContext(ctx)
|
||||||
return condition(ctx)
|
return condition(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user