@@ -18,16 +18,17 @@ import (
1818type RateLimitParams struct {
1919 RateLimiter * rate.Limiter // can be nil to create a new rate limiter
2020
21- // RetryAttempt represents the current retry attempt, starting at 1. This will increment for each retry
22- RetryAttempt int
23- MinDuration time.Duration
24- MaxDuration time.Duration
21+ // Attempt represents the current attempt, starting at 1. This will increment for each (re)try
22+ Attempt int
23+
24+ MinDuration time.Duration
25+ MaxDuration time.Duration
2526}
2627
2728func RateLimit (ctx context.Context , params RateLimitParams ) (* rate.Limiter , error ) {
2829 ctx , span := telemetry .StartSpan (ctx , "rate-limiter-wait" ,
2930 trace .WithAttributes (
30- attribute .Int ("retry_attempt " , params .RetryAttempt ),
31+ attribute .Int ("attempt " , params .Attempt ),
3132 attribute .Int64 ("min_duration_ms" , params .MinDuration .Milliseconds ()),
3233 attribute .Int64 ("max_duration_ms" , params .MaxDuration .Milliseconds ()),
3334 ))
@@ -52,7 +53,7 @@ func RateLimit(ctx context.Context, params RateLimitParams) (*rate.Limiter, erro
5253
5354// generateRateLimiter initializes a new rate limiter or sets a new limit on it.
5455func generateRateLimiter (ctx context.Context , params RateLimitParams ) (* rate.Limiter , error ) {
55- rateLimitDuration , err := generateRateLimitDuration (params .RetryAttempt , params .MinDuration , params .MaxDuration )
56+ rateLimitDuration , err := generateRateLimitDuration (params .Attempt , params .MinDuration , params .MaxDuration )
5657 if err != nil {
5758 return nil , fmt .Errorf ("generating rate limit duration: %w" , err )
5859 }
@@ -103,7 +104,7 @@ func generateRateLimitDuration(multiplier int, minDuration, maxDuration time.Dur
103104
104105type RetryParams struct {
105106 ShouldRetry func (err error ) bool
106- MaxRetries int
107+ MaxAttempts int
107108 MinDuration time.Duration
108109 MaxDuration time.Duration
109110}
@@ -116,8 +117,8 @@ func ExecRetryable[R any](ctx context.Context, closure func(ctx context.Context)
116117 )
117118
118119 // Validate params
119- if params .MaxRetries <= 0 {
120- params .MaxRetries = 1 // Default to at least one try
120+ if params .MaxAttempts <= 0 {
121+ params .MaxAttempts = 1 // Default to at least one try
121122 }
122123 if params .MinDuration <= 0 {
123124 params .MinDuration = 100 * time .Millisecond // Default min backoff
@@ -126,20 +127,20 @@ func ExecRetryable[R any](ctx context.Context, closure func(ctx context.Context)
126127 params .MaxDuration = params .MinDuration * 10 // Default max to 10x min
127128 }
128129
129- retryFunc := func (ctx context.Context , retryAttempt int ) (R , error ) {
130+ tryFunc := func (ctx context.Context , attemptNum int ) (R , error ) {
130131 tryCtx , span := telemetry .StartSpan (ctx , "try" ,
131132 trace .WithAttributes (
132- attribute .Int ("retry_attempt " , retryAttempt ),
133- attribute .Int ("max_tries " , params .MaxRetries ),
133+ attribute .Int ("attempt " , attemptNum ),
134+ attribute .Int ("max_attempts " , params .MaxAttempts ),
134135 ),
135136 )
136137 defer span .End ()
137138 return closure (tryCtx )
138139 }
139140
140- for i := 0 ; i < params .MaxRetries ; i ++ {
141- retryAttempt := i + 1
142- retVal , err = retryFunc (ctx , retryAttempt )
141+ for i := range params .MaxAttempts {
142+ attempt := i + 1
143+ retVal , err = tryFunc (ctx , attempt )
143144
144145 // no error means success - break out
145146 if err == nil {
@@ -152,20 +153,20 @@ func ExecRetryable[R any](ctx context.Context, closure func(ctx context.Context)
152153 }
153154
154155 // record event if we'll be attempting retries
155- err = fmt .Errorf ("try %d of %d: %w" , retryAttempt , params .MaxRetries , err )
156+ err = fmt .Errorf ("try %d of %d: %w" , attempt , params .MaxAttempts , err )
156157 telemetry .AddEvent (ctx , err .Error ())
157158
158- if retryAttempt != params .MaxRetries {
159+ if attempt != params .MaxAttempts {
159160 // If error and we haven't hit max tries,
160161 // generate rate limiter to delay retries.
161162 // This will jitter a wait time before the next iteration.
162163 //
163164 // We abort on rate limit errors (e.g., ctx cancel) instead of continuing
164165 rlParams := RateLimitParams {
165- RateLimiter : rateLimiter ,
166- RetryAttempt : retryAttempt ,
167- MinDuration : params .MinDuration ,
168- MaxDuration : params .MaxDuration ,
166+ RateLimiter : rateLimiter ,
167+ Attempt : attempt ,
168+ MinDuration : params .MinDuration ,
169+ MaxDuration : params .MaxDuration ,
169170 }
170171 rateLimiter , err = RateLimit (ctx , rlParams )
171172 if err != nil {
@@ -176,7 +177,7 @@ func ExecRetryable[R any](ctx context.Context, closure func(ctx context.Context)
176177 }
177178
178179 if err != nil {
179- return retVal , fmt .Errorf ("hit max tries %d: %w" , params .MaxRetries , err )
180+ return retVal , fmt .Errorf ("hit max tries %d: %w" , params .MaxAttempts , err )
180181 }
181182 return retVal , nil
182183}
0 commit comments