mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
fdfdcc058f
Signed-off-by: Fish-pro <zechun.chen@daocloud.io>
32 lines
749 B
Go
32 lines
749 B
Go
package retry
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
)
|
|
|
|
// RetryFunc allows retrying a function on error within a given timeout
|
|
func RetryFunc(ctx context.Context, retryInterval, timeout time.Duration, logger logr.Logger, msg string, run func(context.Context) error) func() error {
|
|
return func() error {
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
registerTicker := time.NewTicker(retryInterval)
|
|
defer registerTicker.Stop()
|
|
var err error
|
|
for {
|
|
select {
|
|
case <-registerTicker.C:
|
|
if err = run(ctx); err != nil {
|
|
logger.V(3).Info(msg, "reason", err.Error())
|
|
} else {
|
|
return nil
|
|
}
|
|
case <-ctx.Done():
|
|
return fmt.Errorf("retry times out: %w", err)
|
|
}
|
|
}
|
|
}
|
|
}
|