From 6eb5a7af48d04adc5625cbc8355e2556db4b992f Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 7 Jul 2023 00:34:25 +0800 Subject: Refactor: move retry into single package. --- common/retry.go | 47 ----------------------------------------------- common/retry/retry.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ recording/task.go | 6 +++--- 3 files changed, 50 insertions(+), 50 deletions(-) delete mode 100644 common/retry.go create mode 100644 common/retry/retry.go diff --git a/common/retry.go b/common/retry.go deleted file mode 100644 index ff7b869..0000000 --- a/common/retry.go +++ /dev/null @@ -1,47 +0,0 @@ -package common - -import ( - "context" - "github.com/keuin/slbr/logging" - "time" -) - -// AutoRetry retries the supplier automatically, with given time limit and interval. -// If maximum retry time limit is reached and the supplier still fails, -// the last error will be returned. -// If logger is not nil, retry information will be printed to it. -func AutoRetry[T any]( - ctx context.Context, - supplier func() (T, error), - maxRetryTimes int, - retryInterval time.Duration, - logger *logging.Logger) (T, error) { - var err error - var ret T - for i := 0; i < maxRetryTimes+1; i++ { - ret, err = supplier() - if err != nil { - if logger != nil { - logger.Info("Try %v/%v (sleep %v): %v", - i, maxRetryTimes, retryInterval, err) - } - timer := time.NewTimer(retryInterval) - select { - case <-timer.C: - // time to have the next try - continue - case <-ctx.Done(): - // context is cancelled - var zero T - return zero, ctx.Err() - } - } - // success - return ret, nil - } - if logger != nil { - logger.Error("Max retry times reached, but it still fails. Last error: %v", err) - } - var zero T - return zero, err -} diff --git a/common/retry/retry.go b/common/retry/retry.go new file mode 100644 index 0000000..43e7d69 --- /dev/null +++ b/common/retry/retry.go @@ -0,0 +1,47 @@ +package retry + +import ( + "context" + "github.com/keuin/slbr/logging" + "time" +) + +// AutoRetry retries the supplier automatically, with given time limit and interval. +// If maximum retry time limit is reached and the supplier still fails, +// the last error will be returned. +// If logger is not nil, retry information will be printed to it. +func AutoRetry[T any]( + ctx context.Context, + supplier func() (T, error), + maxRetryTimes int, + retryInterval time.Duration, + logger *logging.Logger) (T, error) { + var err error + var ret T + for i := 0; i < maxRetryTimes+1; i++ { + ret, err = supplier() + if err != nil { + if logger != nil { + logger.Info("Try %v/%v (sleep %v): %v", + i, maxRetryTimes, retryInterval, err) + } + timer := time.NewTimer(retryInterval) + select { + case <-timer.C: + // time to have the next try + continue + case <-ctx.Done(): + // context is cancelled + var zero T + return zero, ctx.Err() + } + } + // success + return ret, nil + } + if logger != nil { + logger.Error("Max retry times reached, but it still fails. Last error: %v", err) + } + var zero T + return zero, err +} diff --git a/recording/task.go b/recording/task.go index 78be348..5c1f5ee 100644 --- a/recording/task.go +++ b/recording/task.go @@ -8,7 +8,7 @@ Concrete task works are done in the `runner.go` file. import ( "context" "fmt" - "github.com/keuin/slbr/common" + "github.com/keuin/slbr/common/retry" "github.com/keuin/slbr/logging" "time" ) @@ -90,7 +90,7 @@ func AutoRetryWithTask[T any]( t *RunningTask, supplier func() (T, error), ) (T, error) { - return common.AutoRetry[T]( + return retry.AutoRetry[T]( t.ctx, supplier, t.Transport.MaxRetryTimes, @@ -105,7 +105,7 @@ func AutoRetryWithConfig[T any]( t *TaskConfig, supplier func() (T, error), ) (T, error) { - return common.AutoRetry[T]( + return retry.AutoRetry[T]( ctx, supplier, t.Transport.MaxRetryTimes, -- cgit v1.2.3