zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/common/retry.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  func RetryWithContext(ctx context.Context, operation func(attempt int, retryIn time.Duration) error, maxRetries int,
     9  	delay time.Duration,
    10  ) error {
    11  	err := operation(1, delay)
    12  
    13  	for attempt := 1; err != nil && attempt < maxRetries; attempt++ {
    14  		select {
    15  		case <-time.After(delay):
    16  		case <-ctx.Done():
    17  			return err
    18  		}
    19  
    20  		err = operation(attempt+1, delay)
    21  	}
    22  
    23  	return err
    24  }