@@ -27,8 +27,6 @@ export {
27
27
type Uploadable ,
28
28
} from './uploads' ;
29
29
30
- const MAX_RETRIES = 2 ;
31
-
32
30
export type Fetch = ( url : RequestInfo , init ?: RequestInit ) => Promise < Response > ;
33
31
34
32
type PromiseOrValue < T > = T | Promise < T > ;
@@ -162,7 +160,7 @@ export abstract class APIClient {
162
160
163
161
constructor ( {
164
162
baseURL,
165
- maxRetries,
163
+ maxRetries = 2 ,
166
164
timeout = 600000 , // 10 minutes
167
165
httpAgent,
168
166
fetch : overridenFetch ,
@@ -174,7 +172,7 @@ export abstract class APIClient {
174
172
fetch : Fetch | undefined ;
175
173
} ) {
176
174
this . baseURL = baseURL ;
177
- this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ?? MAX_RETRIES ) ;
175
+ this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
178
176
this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
179
177
this . httpAgent = httpAgent ;
180
178
@@ -513,8 +511,6 @@ export abstract class APIClient {
513
511
retriesRemaining : number ,
514
512
responseHeaders ?: Headers | undefined ,
515
513
) : Promise < APIResponseProps > {
516
- retriesRemaining -= 1 ;
517
-
518
514
// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
519
515
let timeoutMillis : number | undefined ;
520
516
const retryAfterHeader = responseHeaders ?. [ 'retry-after' ] ;
@@ -540,22 +536,22 @@ export abstract class APIClient {
540
536
}
541
537
await sleep ( timeoutMillis ) ;
542
538
543
- return this . makeRequest ( options , retriesRemaining ) ;
539
+ return this . makeRequest ( options , retriesRemaining - 1 ) ;
544
540
}
545
541
546
542
private calculateDefaultRetryTimeoutMillis ( retriesRemaining : number , maxRetries : number ) : number {
547
543
const initialRetryDelay = 0.5 ;
548
- const maxRetryDelay = 2 ;
544
+ const maxRetryDelay = 8.0 ;
549
545
550
546
const numRetries = maxRetries - retriesRemaining ;
551
547
552
548
// Apply exponential backoff, but not more than the max.
553
- const sleepSeconds = Math . min ( initialRetryDelay * Math . pow ( numRetries - 1 , 2 ) , maxRetryDelay ) ;
549
+ const sleepSeconds = Math . min ( initialRetryDelay * Math . pow ( 2 , numRetries ) , maxRetryDelay ) ;
554
550
555
- // Apply some jitter, plus-or-minus half a second .
556
- const jitter = Math . random ( ) - 0.5 ;
551
+ // Apply some jitter, take up to at most 25 percent of the retry time .
552
+ const jitter = 1 - Math . random ( ) * 0.25 ;
557
553
558
- return ( sleepSeconds + jitter ) * 1000 ;
554
+ return sleepSeconds * jitter * 1000 ;
559
555
}
560
556
561
557
private getUserAgent ( ) : string {
0 commit comments