From 5c6e92769f596a4434ca806aee1c35bb96c2919b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 17 Dec 2024 13:25:41 +0100 Subject: [PATCH] fix: Make `contextAttemptKey` its own type. This is a best practice because it avoids any potential clashes with other values of the context. --- retry.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/retry.go b/retry.go index 6571c94..12a9779 100644 --- a/retry.go +++ b/retry.go @@ -118,10 +118,10 @@ func Abort(err error) Error { return permanentError{err} } -var contextAttemptKey struct{} +type ctxKey struct{} func withAttempt(ctx context.Context, attempt int) context.Context { - return context.WithValue(ctx, contextAttemptKey, attempt) + return context.WithValue(ctx, ctxKey{}, attempt) } // Attempt returns the number of previous attempts. In other words, it returns @@ -129,7 +129,7 @@ func withAttempt(ctx context.Context, attempt int) context.Context { // // Only call this function from within a retried function. func Attempt(ctx context.Context) int { - i := ctx.Value(contextAttemptKey) + i := ctx.Value(ctxKey{}) if i == nil { return 0 }