@@ -11,10 +11,10 @@ import (
1111 "github.com/smartcontractkit/chainlink-common/pkg/logger"
1212)
1313
14- // CtxKeyTracingID is the context key for tracing ID
1514type ctxKey string
1615
17- const CtxKeyTracingID ctxKey = "tracingID"
16+ // CtxKeyRetryID is the context key for tracing ID
17+ const CtxKeyRetryID ctxKey = "retryID"
1818
1919// Exponential backoff (default) is used to handle retries with increasing wait times in case of errors
2020var BackoffStrategyDefault = backoff.Backoff {
@@ -23,18 +23,18 @@ var BackoffStrategyDefault = backoff.Backoff{
2323 Factor : 2 ,
2424}
2525
26- // WithRetryStrategy applies a retry strategy to a given function.
27- func WithRetryStrategy [R any ](ctx context.Context , lggr logger.Logger , strategy backoff.Backoff , fn func (ctx context.Context ) (R , error )) (R , error ) {
26+ // WithStrategy applies a retry strategy to a given function.
27+ func WithStrategy [R any ](ctx context.Context , lggr logger.Logger , strategy backoff.Backoff , fn func (ctx context.Context ) (R , error )) (R , error ) {
2828 // Generate a new tracing ID if not present, used to track retries
29- tracingID , ok := ctx .Value (CtxKeyTracingID ).(string )
29+ retryID , ok := ctx .Value (CtxKeyRetryID ).(string )
3030 if ! ok {
31- tracingID = uuid .New ().String ()
31+ retryID = uuid .New ().String ()
3232 // Add the generated tracing ID to the context (as it was not already present)
33- ctx = context .WithValue (ctx , CtxKeyTracingID , tracingID )
33+ ctx = context .WithValue (ctx , CtxKeyRetryID , retryID )
3434 }
3535
3636 // Track the number of retries
37- numRetries := 0
37+ numRetries := int ( strategy . Attempt ())
3838 for {
3939 result , err := fn (ctx )
4040 if err == nil {
@@ -43,19 +43,19 @@ func WithRetryStrategy[R any](ctx context.Context, lggr logger.Logger, strategy
4343
4444 wait := strategy .Duration ()
4545 message := fmt .Sprintf ("Failed to execute function, retrying in %s ..." , wait )
46- lggr .Warnw (message , "wait" , wait , "numRetries" , numRetries , "tracingID " , tracingID , "err" , err )
46+ lggr .Warnw (message , "wait" , wait , "numRetries" , numRetries , "retryID " , retryID , "err" , err )
4747
4848 select {
4949 case <- ctx .Done ():
50- return result , fmt .Errorf ("context done while executing function {tracingID =%s, numRetries=%d}: %w" , tracingID , numRetries , ctx .Err ())
50+ return result , fmt .Errorf ("context done while executing function {retryID =%s, numRetries=%d}: %w" , retryID , numRetries , ctx .Err ())
5151 case <- time .After (wait ):
5252 numRetries ++
5353 // Continue with the next retry
5454 }
5555 }
5656}
5757
58- // WithRetry applies a default retry strategy to a given function.
59- func WithRetry [R any ](ctx context.Context , lggr logger.Logger , fn func (ctx context.Context ) (R , error )) (R , error ) {
60- return WithRetryStrategy (ctx , lggr , BackoffStrategyDefault , fn )
58+ // With applies a default retry strategy to a given function.
59+ func With [R any ](ctx context.Context , lggr logger.Logger , fn func (ctx context.Context ) (R , error )) (R , error ) {
60+ return WithStrategy (ctx , lggr , BackoffStrategyDefault , fn )
6161}
0 commit comments