Skip to content

Commit 4c2bde0

Browse files
committed
refactored retry conetxt ID
1 parent b26aef1 commit 4c2bde0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pkg/utils/retry/retry.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import (
1313

1414
type ctxKey string
1515

16-
// CtxKeyRetryID is the context key for tracing ID
17-
const CtxKeyRetryID ctxKey = "retryID"
16+
// ctxKeyID is the context key for tracing ID
17+
const ctxKeyID ctxKey = "retryID"
18+
19+
func CtxWithID(ctx context.Context, retryID string) context.Context {
20+
return context.WithValue(ctx, ctxKeyID, retryID)
21+
}
1822

1923
// Exponential backoff (default) is used to handle retries with increasing wait times in case of errors
2024
var BackoffStrategyDefault = backoff.Backoff{
@@ -26,11 +30,11 @@ var BackoffStrategyDefault = backoff.Backoff{
2630
// WithStrategy applies a retry strategy to a given function.
2731
func WithStrategy[R any](ctx context.Context, lggr logger.Logger, strategy backoff.Backoff, fn func(ctx context.Context) (R, error)) (R, error) {
2832
// Generate a new tracing ID if not present, used to track retries
29-
retryID, ok := ctx.Value(CtxKeyRetryID).(string)
30-
if !ok {
33+
retryID := ctx.Value(ctxKeyID)
34+
if retryID == nil {
3135
retryID = uuid.New().String()
3236
// Add the generated tracing ID to the context (as it was not already present)
33-
ctx = context.WithValue(ctx, CtxKeyRetryID, retryID)
37+
ctx = context.WithValue(ctx, ctxKeyID, retryID)
3438
}
3539

3640
// Track the number of retries

0 commit comments

Comments
 (0)