11cb0ef41Sopenharmony_ci# Class: RetryHandler 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciExtends: `undici.DispatcherHandlers` 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciA handler class that implements the retry logic for a request. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci## `new RetryHandler(dispatchOptions, retryHandlers, [retryOptions])` 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciArguments: 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci- **options** `Dispatch.DispatchOptions & RetryOptions` (required) - It is an intersection of `Dispatcher.DispatchOptions` and `RetryOptions`. 121cb0ef41Sopenharmony_ci- **retryHandlers** `RetryHandlers` (required) - Object containing the `dispatch` to be used on every retry, and `handler` for handling the `dispatch` lifecycle. 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciReturns: `retryHandler` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci### Parameter: `Dispatch.DispatchOptions & RetryOptions` 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciExtends: [`Dispatch.DispatchOptions`](Dispatcher.md#parameter-dispatchoptions). 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci#### `RetryOptions` 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci- **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => void` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed. 231cb0ef41Sopenharmony_ci- **maxRetries** `number` (optional) - Maximum number of retries. Default: `5` 241cb0ef41Sopenharmony_ci- **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds) 251cb0ef41Sopenharmony_ci- **minTimeout** `number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second) 261cb0ef41Sopenharmony_ci- **timeoutFactor** `number` (optional) - Factor to multiply the timeout by for each retry attempt. Default: `2` 271cb0ef41Sopenharmony_ci- **retryAfter** `boolean` (optional) - It enables automatic retry after the `Retry-After` header is received. Default: `true` 281cb0ef41Sopenharmony_ci- 291cb0ef41Sopenharmony_ci- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'PUT', 'HEAD', 'OPTIONS', 'DELETE']` 301cb0ef41Sopenharmony_ci- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]` 311cb0ef41Sopenharmony_ci- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci**`RetryContext`** 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci- `state`: `RetryState` - Current retry state. It can be mutated. 361cb0ef41Sopenharmony_ci- `opts`: `Dispatch.DispatchOptions & RetryOptions` - Options passed to the retry handler. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci### Parameter `RetryHandlers` 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci- **dispatch** `(options: Dispatch.DispatchOptions, handlers: Dispatch.DispatchHandlers) => Promise<Dispatch.DispatchResponse>` (required) - Dispatch function to be called after every retry. 411cb0ef41Sopenharmony_ci- **handler** Extends [`Dispatch.DispatchHandlers`](Dispatcher.md#dispatcherdispatchoptions-handler) (required) - Handler function to be called after the request is successful or the retries are exhausted. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciExamples: 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci```js 461cb0ef41Sopenharmony_ciconst client = new Client(`http://localhost:${server.address().port}`); 471cb0ef41Sopenharmony_ciconst chunks = []; 481cb0ef41Sopenharmony_ciconst handler = new RetryHandler( 491cb0ef41Sopenharmony_ci { 501cb0ef41Sopenharmony_ci ...dispatchOptions, 511cb0ef41Sopenharmony_ci retryOptions: { 521cb0ef41Sopenharmony_ci // custom retry function 531cb0ef41Sopenharmony_ci retry: function (err, state, callback) { 541cb0ef41Sopenharmony_ci counter++; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci if (err.code && err.code === "UND_ERR_DESTROYED") { 571cb0ef41Sopenharmony_ci callback(err); 581cb0ef41Sopenharmony_ci return; 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci if (err.statusCode === 206) { 621cb0ef41Sopenharmony_ci callback(err); 631cb0ef41Sopenharmony_ci return; 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci setTimeout(() => callback(null), 1000); 671cb0ef41Sopenharmony_ci }, 681cb0ef41Sopenharmony_ci }, 691cb0ef41Sopenharmony_ci }, 701cb0ef41Sopenharmony_ci { 711cb0ef41Sopenharmony_ci dispatch: (...args) => { 721cb0ef41Sopenharmony_ci return client.dispatch(...args); 731cb0ef41Sopenharmony_ci }, 741cb0ef41Sopenharmony_ci handler: { 751cb0ef41Sopenharmony_ci onConnect() {}, 761cb0ef41Sopenharmony_ci onBodySent() {}, 771cb0ef41Sopenharmony_ci onHeaders(status, _rawHeaders, resume, _statusMessage) { 781cb0ef41Sopenharmony_ci // do something with headers 791cb0ef41Sopenharmony_ci }, 801cb0ef41Sopenharmony_ci onData(chunk) { 811cb0ef41Sopenharmony_ci chunks.push(chunk); 821cb0ef41Sopenharmony_ci return true; 831cb0ef41Sopenharmony_ci }, 841cb0ef41Sopenharmony_ci onComplete() {}, 851cb0ef41Sopenharmony_ci onError() { 861cb0ef41Sopenharmony_ci // handle error properly 871cb0ef41Sopenharmony_ci }, 881cb0ef41Sopenharmony_ci }, 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci); 911cb0ef41Sopenharmony_ci``` 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci#### Example - Basic RetryHandler with defaults 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci```js 961cb0ef41Sopenharmony_ciconst client = new Client(`http://localhost:${server.address().port}`); 971cb0ef41Sopenharmony_ciconst handler = new RetryHandler(dispatchOptions, { 981cb0ef41Sopenharmony_ci dispatch: client.dispatch.bind(client), 991cb0ef41Sopenharmony_ci handler: { 1001cb0ef41Sopenharmony_ci onConnect() {}, 1011cb0ef41Sopenharmony_ci onBodySent() {}, 1021cb0ef41Sopenharmony_ci onHeaders(status, _rawHeaders, resume, _statusMessage) {}, 1031cb0ef41Sopenharmony_ci onData(chunk) {}, 1041cb0ef41Sopenharmony_ci onComplete() {}, 1051cb0ef41Sopenharmony_ci onError(err) {}, 1061cb0ef41Sopenharmony_ci }, 1071cb0ef41Sopenharmony_ci}); 1081cb0ef41Sopenharmony_ci``` 109