|
| 1 | +// maximum number of retries on 503 responses |
| 2 | +const MAX_RETRIES = 5; |
| 3 | + |
| 4 | +// base delay in ms for exponential backoff |
| 5 | +const BASE_DELAY_MS = 500; |
| 6 | + |
| 7 | +// longest delay we will allow even if more is specified by Retry-After header is 1 minute. |
| 8 | +const MAX_DELAY_MS = 60000; |
| 9 | + |
| 10 | +/** |
| 11 | + * wraps a fetch function to retry on 503 responses only, passing all other responses through and |
| 12 | + * respecting the abort controller signal. |
| 13 | + * |
| 14 | + * if server provides a Retry-After header, that is respected (within reason), otherwise we use exponential |
| 15 | + * backoff based on the number of attempts. |
| 16 | + * |
| 17 | + * retry is attempted up to MAX_RETRIES times with exponential backoff between 0 and 2^n * BASE_DELAY_MS. |
| 18 | + * |
| 19 | + */ |
| 20 | +export function with503Retry(fetch: typeof globalThis.fetch) { |
| 21 | + return async function fetchWithRetry503(input: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 22 | + // want to respect abort signals if provided |
| 23 | + const abortSignal = init?.signal; |
| 24 | + |
| 25 | + for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) { |
| 26 | + // fetch will throw if already aborted |
| 27 | + const response = await fetch(input, init); |
| 28 | + |
| 29 | + if (response.status !== 503) { |
| 30 | + // If not a 503, return the response immediately |
| 31 | + return response; |
| 32 | + } |
| 33 | + |
| 34 | + if (attempt >= MAX_RETRIES) { |
| 35 | + // return the last response |
| 36 | + return response; |
| 37 | + } |
| 38 | + |
| 39 | + // if there is a retry after, we'll respect that as the server is explicitly indicating when to retry. |
| 40 | + const retryAfterMs = getRetryAfterMs(response.headers); |
| 41 | + |
| 42 | + if (retryAfterMs !== undefined) { |
| 43 | + // if the value is 0, we retry immediately as it is explicitly indicated by server. Jitter is not |
| 44 | + // applied in this case, as the server is instructing the wait time due to some knowledge it has. |
| 45 | + if (retryAfterMs > 0) { |
| 46 | + // note that we clamp the maximum delay to avoid excessively long waits, even if server indicates longer |
| 47 | + // that could result in a muisconfiguration or error on server side causing request to delay for years |
| 48 | + // (or millennia?) |
| 49 | + await waitForTimeoutOrCancel(Math.min(retryAfterMs, MAX_DELAY_MS), abortSignal); |
| 50 | + } |
| 51 | + continue; // Proceed to next attempt |
| 52 | + } |
| 53 | + |
| 54 | + // no Retry-After header, so we use exponential backoff. |
| 55 | + |
| 56 | + // Calculate delay |
| 57 | + const delay = Math.random() * Math.pow(2, attempt) * BASE_DELAY_MS; |
| 58 | + |
| 59 | + // Wait for the delay before retrying, but abort if signal is triggered |
| 60 | + await waitForTimeoutOrCancel(delay, abortSignal); |
| 61 | + |
| 62 | + // Proceed to next attempt |
| 63 | + } |
| 64 | + |
| 65 | + // This point should never be reached |
| 66 | + throw new Error('Unexpected error in fetchWithRetry503'); |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Helper function to wait for a timeout or abort with Aborted if signal is triggered |
| 72 | + */ |
| 73 | +async function waitForTimeoutOrCancel(delay: number, signal: AbortSignal | null | undefined): Promise<void> { |
| 74 | + if (!signal) { |
| 75 | + return new Promise(resolve => setTimeout(resolve, delay)); |
| 76 | + } |
| 77 | + return await new Promise((resolve, reject) => { |
| 78 | + const onAbort = () => { |
| 79 | + signal.removeEventListener('abort', onAbort); |
| 80 | + clearTimeout(timeoutId); // timeoutId is defined, hoisting. |
| 81 | + // Reject the promise if aborted using standard DOMException for AbortError |
| 82 | + reject(new DOMException('Aborted', 'AbortError')); |
| 83 | + }; |
| 84 | + signal.addEventListener('abort', onAbort); |
| 85 | + const timeoutId = setTimeout(() => { |
| 86 | + signal.removeEventListener('abort', onAbort); |
| 87 | + resolve(); |
| 88 | + }, delay); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * either returns number of milliseconds to wait as instructed by the server, or undefined |
| 94 | + * if no valid Retry-After header is present. |
| 95 | + * |
| 96 | + * note that 0 is a valid retry-after value, and explicitly indicates no wait and that the |
| 97 | + * client should retry immediately. |
| 98 | + * |
| 99 | + * Handles both delta-seconds and HTTP-date formats, returning the number of milliseconds to |
| 100 | + * wait from now if HTTP-date is provided. If it is in the past, returns 0. |
| 101 | + * |
| 102 | + * does not clamp the upper bound value, that must be handled by the caller. |
| 103 | + * |
| 104 | + */ |
| 105 | +export function getRetryAfterMs(headers?: Headers): number | undefined { |
| 106 | + const retryAfter = headers?.get('Retry-After'); |
| 107 | + if (!retryAfter) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const intValue = parseInt(retryAfter, 10); |
| 112 | + if (!isNaN(intValue)) { |
| 113 | + if (intValue < 0) { |
| 114 | + // invalid, treat as no header present |
| 115 | + return; |
| 116 | + } else if (intValue === 0) { |
| 117 | + // explicit immediate retry |
| 118 | + return 0; |
| 119 | + } |
| 120 | + return Math.ceil(intValue) * 1000; // return whole integers as milliseconds only. |
| 121 | + } |
| 122 | + |
| 123 | + // reminder: https://jsdate.wtf/ |
| 124 | + const date = new Date(retryAfter); |
| 125 | + if (!isNaN(date.getTime())) { |
| 126 | + const value = Math.ceil(date.getTime() - Date.now()); |
| 127 | + if (value < 0) { |
| 128 | + // date is in the past, so we return 0 |
| 129 | + return 0; |
| 130 | + } |
| 131 | + return value; |
| 132 | + } |
| 133 | + |
| 134 | + // otherwise the date was invalid so we treat as no header present |
| 135 | + return; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * returns number of full seconds to wait as instructed by the server, or undefined |
| 140 | + * if no valid Retry-After header is present. |
| 141 | + * |
| 142 | + * @see getRetryAfterMs |
| 143 | + */ |
| 144 | +export function getRetryAfterSeconds(headers?: Headers): number | undefined { |
| 145 | + const ms = getRetryAfterMs(headers); |
| 146 | + if (ms === undefined) { |
| 147 | + return; |
| 148 | + } |
| 149 | + return Math.ceil(ms / 1000); |
| 150 | +} |
0 commit comments