@@ -134,6 +134,28 @@ function validateRetryConfig<T>(config: RetryConfig<T>): void {
134
134
}
135
135
}
136
136
137
+ /**
138
+ * Calculates delay between retries, in milliseconds.
139
+ * @internal
140
+ */
141
+ function calculateDelay (
142
+ attemptCount : number ,
143
+ retryDelayInMs : number ,
144
+ maxRetryDelayInMs : number ,
145
+ mode : RetryMode
146
+ ) : number {
147
+ if ( mode === RetryMode . Exponential ) {
148
+ const boundedRandDelta =
149
+ retryDelayInMs * 0.8 +
150
+ Math . floor ( Math . random ( ) * ( retryDelayInMs * 1.2 - retryDelayInMs * 0.8 ) ) ;
151
+
152
+ const incrementDelta = boundedRandDelta * ( Math . pow ( 2 , attemptCount ) - 1 ) ;
153
+ return Math . min ( incrementDelta , maxRetryDelayInMs ) ;
154
+ }
155
+
156
+ return retryDelayInMs ;
157
+ }
158
+
137
159
/**
138
160
* Every operation is attempted at least once. Additional attempts are made if the previous attempt failed
139
161
* with a retryable error. The number of additional attempts is governed by the `maxRetries` property provided
@@ -219,21 +241,14 @@ export async function retry<T>(config: RetryConfig<T>): Promise<T> {
219
241
i ,
220
242
err
221
243
) ;
222
- let targetDelayInMs = config . retryOptions . retryDelayInMs ;
223
- if ( config . retryOptions . mode === RetryMode . Exponential ) {
224
- let incrementDelta = Math . pow ( 2 , i ) - 1 ;
225
- const boundedRandDelta =
226
- config . retryOptions . retryDelayInMs * 0.8 +
227
- Math . floor (
228
- Math . random ( ) *
229
- ( config . retryOptions . retryDelayInMs * 1.2 - config . retryOptions . retryDelayInMs * 0.8 )
230
- ) ;
231
- incrementDelta *= boundedRandDelta ;
232
-
233
- targetDelayInMs = Math . min ( incrementDelta , config . retryOptions . maxRetryDelayInMs ) ;
234
- }
235
244
236
245
if ( lastError && lastError . retryable && totalNumberOfAttempts > i ) {
246
+ const targetDelayInMs = calculateDelay (
247
+ i ,
248
+ config . retryOptions . retryDelayInMs ,
249
+ config . retryOptions . maxRetryDelayInMs ,
250
+ config . retryOptions . mode
251
+ ) ;
237
252
logger . verbose (
238
253
"[%s] Sleeping for %d milliseconds for '%s'." ,
239
254
config . connectionId ,
0 commit comments