@@ -45,6 +45,14 @@ interface ExponentialBackoffResponseHandlerOptions {
45
45
maxRetries : number ;
46
46
}
47
47
48
+ function isRetryableError ( error : any ) : boolean {
49
+ // Check for network errors that are typically transient
50
+ if ( error . code ) {
51
+ return [ 'ECONNRESET' , 'ETIMEDOUT' , 'ECONNABORTED' ] . includes ( error . code ) ;
52
+ }
53
+ return false ;
54
+ }
55
+
48
56
function getExponentialBackoffResponseHandler (
49
57
axios : AxiosInstance ,
50
58
opts : ExponentialBackoffResponseHandlerOptions
@@ -76,6 +84,37 @@ function getExponentialBackoffResponseHandler(
76
84
} ;
77
85
}
78
86
87
+ function getExponentialBackoffErrorHandler (
88
+ axios : AxiosInstance ,
89
+ opts : ExponentialBackoffResponseHandlerOptions
90
+ ) {
91
+ const maxIntervalMillis = opts . maxIntervalMillis ;
92
+ const maxRetries = opts . maxRetries ;
93
+
94
+ return function ( error : any ) {
95
+ const config : BackoffAxiosRequestConfig = error . config ;
96
+
97
+ if ( ! isRetryableError ( error ) || ! config ) {
98
+ return Promise . reject ( error ) ;
99
+ }
100
+
101
+ const retryCount = ( config . retryCount || 0 ) + 1 ;
102
+ if ( retryCount <= maxRetries ) {
103
+ config . retryCount = retryCount ;
104
+ const baseDelay = Math . min (
105
+ maxIntervalMillis ,
106
+ DEFAULT_INITIAL_RETRY_INTERVAL_MILLIS * Math . pow ( 2 , retryCount )
107
+ ) ;
108
+ const delay = Math . floor ( baseDelay * Math . random ( ) ) ; // Full jitter backoff
109
+
110
+ return new Promise ( ( resolve : ( value : Promise < AxiosResponse > ) => void ) => {
111
+ setTimeout ( ( ) => resolve ( axios ( config ) ) , delay ) ;
112
+ } ) ;
113
+ }
114
+ return Promise . reject ( error ) ;
115
+ } ;
116
+ }
117
+
79
118
class RequestClient {
80
119
defaultTimeout : number ;
81
120
axios : AxiosInstance ;
@@ -96,9 +135,9 @@ class RequestClient {
96
135
* @param opts.maxTotalSockets - https.Agent maxTotalSockets option
97
136
* @param opts.maxFreeSockets - https.Agent maxFreeSockets option
98
137
* @param opts.scheduling - https.Agent scheduling option
99
- * @param opts.autoRetry - Enable auto-retry requests with exponential backoff on 429 responses. Defaults to false.
100
- * @param opts.maxRetryDelay - Max retry delay in milliseconds for 429 Too Many Request response retries. Defaults to 3000.
101
- * @param opts.maxRetries - Max number of request retries for 429 Too Many Request responses. Defaults to 3.
138
+ * @param opts.autoRetry - Enable auto-retry requests with exponential backoff on 429 responses and network errors . Defaults to false.
139
+ * @param opts.maxRetryDelay - Max retry delay in milliseconds for 429 Too Many Request response retries and network errors . Defaults to 3000.
140
+ * @param opts.maxRetries - Max number of request retries for 429 Too Many Request responses and network errors . Defaults to 3.
102
141
* @param opts.validationClient - Validation client for PKCV
103
142
*/
104
143
constructor ( opts ?: RequestClient . RequestClientOptions ) {
@@ -146,6 +185,10 @@ class RequestClient {
146
185
getExponentialBackoffResponseHandler ( this . axios , {
147
186
maxIntervalMillis : this . maxRetryDelay ,
148
187
maxRetries : this . maxRetries ,
188
+ } ) ,
189
+ getExponentialBackoffErrorHandler ( this . axios , {
190
+ maxIntervalMillis : this . maxRetryDelay ,
191
+ maxRetries : this . maxRetries ,
149
192
} )
150
193
) ;
151
194
}
@@ -421,16 +464,17 @@ namespace RequestClient {
421
464
ca ?: string | Buffer ;
422
465
/**
423
466
* Enable auto-retry with exponential backoff when receiving 429 Errors from
424
- * the API. Disabled by default.
467
+ * the API or network errors (e.g. ECONNRESET) . Disabled by default.
425
468
*/
426
469
autoRetry ?: boolean ;
427
470
/**
428
- * Maximum retry delay in milliseconds for 429 Error response retries.
429
- * Defaults to 3000.
471
+ * Maximum retry delay in milliseconds for 429 Error response retries
472
+ * and network errors. Defaults to 3000.
430
473
*/
431
474
maxRetryDelay ?: number ;
432
475
/**
433
- * Maximum number of request retries for 429 Error responses. Defaults to 3.
476
+ * Maximum number of request retries for 429 Error responses and network
477
+ * errors. Defaults to 3.
434
478
*/
435
479
maxRetries ?: number ;
436
480
/**
0 commit comments