|
| 1 | +# Class: RetryHandler |
| 2 | + |
| 3 | +Extends: `undici.DispatcherHandlers` |
| 4 | + |
| 5 | +A handler class that implements the retry logic for a request. |
| 6 | + |
| 7 | +## `new RetryHandler(dispatchOptions, retryHandlers, [retryOptions])` |
| 8 | + |
| 9 | +Arguments: |
| 10 | + |
| 11 | +- **options** `Dispatch.DispatchOptions & RetryOptions` (required) - It is an intersection of `Dispatcher.DispatchOptions` and `RetryOptions`. |
| 12 | +- **retryHandlers** `RetryHandlers` (required) - Object containing the `dispatch` to be used on every retry, and `handler` for handling the `dispatch` lifecycle. |
| 13 | + |
| 14 | +Returns: `retryHandler` |
| 15 | + |
| 16 | +### Parameter: `Dispatch.DispatchOptions & RetryOptions` |
| 17 | + |
| 18 | +Extends: [`Dispatch.DispatchOptions`](Dispatcher.md#parameter-dispatchoptions). |
| 19 | + |
| 20 | +#### `RetryOptions` |
| 21 | + |
| 22 | +- **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. |
| 23 | +- **maxRetries** `number` (optional) - Maximum number of retries. Default: `5` |
| 24 | +- **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds) |
| 25 | +- **minTimeout** `number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second) |
| 26 | +- **timeoutFactor** `number` (optional) - Factor to multiply the timeout by for each retry attempt. Default: `2` |
| 27 | +- **retryAfter** `boolean` (optional) - It enables automatic retry after the `Retry-After` header is received. Default: `true` |
| 28 | +- |
| 29 | +- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'PUT', 'HEAD', 'OPTIONS', 'DELETE']` |
| 30 | +- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]` |
| 31 | +- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', |
| 32 | + |
| 33 | +**`RetryContext`** |
| 34 | + |
| 35 | +- `state`: `RetryState` - Current retry state. It can be mutated. |
| 36 | +- `opts`: `Dispatch.DispatchOptions & RetryOptions` - Options passed to the retry handler. |
| 37 | + |
| 38 | +### Parameter `RetryHandlers` |
| 39 | + |
| 40 | +- **dispatch** `(options: Dispatch.DispatchOptions, handlers: Dispatch.DispatchHandlers) => Promise<Dispatch.DispatchResponse>` (required) - Dispatch function to be called after every retry. |
| 41 | +- **handler** Extends [`Dispatch.DispatchHandlers`](Dispatcher.md#dispatcherdispatchoptions-handler) (required) - Handler function to be called after the request is successful or the retries are exhausted. |
| 42 | + |
| 43 | +Examples: |
| 44 | + |
| 45 | +```js |
| 46 | +const client = new Client(`http://localhost:${server.address().port}`); |
| 47 | +const chunks = []; |
| 48 | +const handler = new RetryHandler( |
| 49 | + { |
| 50 | + ...dispatchOptions, |
| 51 | + retryOptions: { |
| 52 | + // custom retry function |
| 53 | + retry: function (err, state, callback) { |
| 54 | + counter++; |
| 55 | + |
| 56 | + if (err.code && err.code === "UND_ERR_DESTROYED") { |
| 57 | + callback(err); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (err.statusCode === 206) { |
| 62 | + callback(err); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + setTimeout(() => callback(null), 1000); |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + dispatch: (...args) => { |
| 72 | + return client.dispatch(...args); |
| 73 | + }, |
| 74 | + handler: { |
| 75 | + onConnect() {}, |
| 76 | + onBodySent() {}, |
| 77 | + onHeaders(status, _rawHeaders, resume, _statusMessage) { |
| 78 | + // do something with headers |
| 79 | + }, |
| 80 | + onData(chunk) { |
| 81 | + chunks.push(chunk); |
| 82 | + return true; |
| 83 | + }, |
| 84 | + onComplete() {}, |
| 85 | + onError() { |
| 86 | + // handle error properly |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +); |
| 91 | +``` |
| 92 | + |
| 93 | +#### Example - Basic RetryHandler with defaults |
| 94 | + |
| 95 | +```js |
| 96 | +const client = new Client(`http://localhost:${server.address().port}`); |
| 97 | +const handler = new RetryHandler(dispatchOptions, { |
| 98 | + dispatch: client.dispatch.bind(client), |
| 99 | + handler: { |
| 100 | + onConnect() {}, |
| 101 | + onBodySent() {}, |
| 102 | + onHeaders(status, _rawHeaders, resume, _statusMessage) {}, |
| 103 | + onData(chunk) {}, |
| 104 | + onComplete() {}, |
| 105 | + onError(err) {}, |
| 106 | + }, |
| 107 | +}); |
| 108 | +``` |
0 commit comments