github.com/cilium/cilium@v1.16.2/pkg/resiliency/helpers.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package resiliency
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"k8s.io/apimachinery/pkg/util/wait"
    11  )
    12  
    13  // RetryFunc tracks resiliency retry calls.
    14  type RetryFunc func(ctx context.Context, retries int) (bool, error)
    15  
    16  // Retry retries the provided call using exponential retries given an initial duration for up to max retries count.
    17  func Retry(ctx context.Context, duration time.Duration, maxRetries int, fn RetryFunc) error {
    18  	bo := wait.Backoff{
    19  		Duration: duration,
    20  		Factor:   1,
    21  		Jitter:   0.1,
    22  		Steps:    maxRetries,
    23  	}
    24  
    25  	var retries int
    26  	f := func(ctx context.Context) (bool, error) {
    27  		retries++
    28  		return fn(ctx, retries)
    29  	}
    30  
    31  	return wait.ExponentialBackoffWithContext(ctx, bo, f)
    32  }