wait: ExponentialBackoffWithContext should take context-aware fn

The condition methods will eventually all take a context. Since we
have been provided one, alter the accepted condition type and
change the four references in tree.

Collers of ExponentialBackoffWithContext should use a condition
aware function (ConditionWithContextFunc). If the context can be
ignored the helper ConditionFunc.WithContext can be used to convert
an existing function to the new type.
This commit is contained in:
Clayton Coleman 2023-01-16 14:57:57 -05:00
parent 7b01daba71
commit 34bfdc3635
No known key found for this signature in database
GPG Key ID: CF7DB7FC943D3E0E
4 changed files with 7 additions and 7 deletions

View File

@ -729,7 +729,7 @@ func poller(interval, timeout time.Duration) WaitWithContextFunc {
// ExponentialBackoffWithContext works with a request context and a Backoff. It ensures that the retry wait never
// exceeds the deadline specified by the request context.
func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, condition ConditionFunc) error {
func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, condition ConditionWithContextFunc) error {
for backoff.Steps > 0 {
select {
case <-ctx.Done():
@ -737,7 +737,7 @@ func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, conditi
default:
}
if ok, err := runConditionWithCrashProtection(condition); err != nil || ok {
if ok, err := runConditionWithCrashProtectionWithContext(ctx, condition); err != nil || ok {
return err
}

View File

@ -875,7 +875,7 @@ func TestExponentialBackoffWithContext(t *testing.T) {
}
attempts := 0
err := ExponentialBackoffWithContext(test.ctxGetter(), backoff, func() (bool, error) {
err := ExponentialBackoffWithContext(test.ctxGetter(), backoff, func(_ context.Context) (bool, error) {
attempts++
return test.callback(attempts)
})

View File

@ -121,7 +121,7 @@ func WithExponentialBackoff(ctx context.Context, retryBackoff wait.Backoff, webh
// having a webhook error allows us to track the last actual webhook error for requests that
// are later cancelled or time out.
var webhookErr error
err := wait.ExponentialBackoffWithContext(ctx, retryBackoff, func() (bool, error) {
err := wait.ExponentialBackoffWithContext(ctx, retryBackoff, func(_ context.Context) (bool, error) {
webhookErr = webhookFn()
if shouldRetry(webhookErr) {
return false, nil

View File

@ -673,7 +673,7 @@ func waitForDetachAndGrabMetrics(ctx context.Context, oldMetrics *storageControl
oldDetachCount = 0
}
verifyMetricFunc := func() (bool, error) {
verifyMetricFunc := func(ctx context.Context) (bool, error) {
updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx)
if err != nil {
@ -821,7 +821,7 @@ func waitForPVControllerSync(ctx context.Context, metricsGrabber *e2emetrics.Gra
Factor: 1.2,
Steps: 21,
}
verifyMetricFunc := func() (bool, error) {
verifyMetricFunc := func(ctx context.Context) (bool, error) {
updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx)
if err != nil {
framework.Logf("Error fetching controller-manager metrics")
@ -866,7 +866,7 @@ func waitForADControllerStatesMetrics(ctx context.Context, metricsGrabber *e2eme
Factor: 1.2,
Steps: 21,
}
verifyMetricFunc := func() (bool, error) {
verifyMetricFunc := func(ctx context.Context) (bool, error) {
updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx)
if err != nil {
e2eskipper.Skipf("Could not get controller-manager metrics - skipping")