leaderelection: replace deprecated wait.PollImmediateUntil with wait.PollUntilContextTimeout

This commit refactors the `renew` function in the leader election package to replace
the deprecated `wait.PollImmediateUntil` with `wait.PollUntilContextTimeout`.
This change simplifies the context handling by removing the need for an additional
timeout context and its cancellation.

The previous implementation created a timeout context for each retry period,
which added unnecessary complexity. The new implementation directly passes the parent
context to the retry function, ensuring that the timeout is handled within `PollUntilContextTimeout`.

This change also ensures that the context passed to `tryAcquireOrRenew` and
`tryCoordinatedRenew` is consistent, improving the readability and maintainability of the code.

Kubernetes-commit: 3d2f498aa31cb2e90bcfc372b7930aed0604fbef
This commit is contained in:
aimuz 2024-08-30 19:25:45 +08:00 committed by Kubernetes Publisher
parent 6c241d51b1
commit 4379fade33

View File

@ -277,16 +277,13 @@ func (le *LeaderElector) renew(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
wait.Until(func() {
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline)
defer timeoutCancel()
err := wait.PollImmediateUntil(le.config.RetryPeriod, func() (bool, error) {
err := wait.PollUntilContextTimeout(ctx, le.config.RetryPeriod, le.config.RenewDeadline, true, func(ctx context.Context) (done bool, err error) {
if !le.config.Coordinated {
return le.tryAcquireOrRenew(timeoutCtx), nil
return le.tryAcquireOrRenew(ctx), nil
} else {
return le.tryCoordinatedRenew(timeoutCtx), nil
return le.tryCoordinatedRenew(ctx), nil
}
}, timeoutCtx.Done())
})
le.maybeReportTransition()
desc := le.config.Lock.Describe()
if err == nil {