|
| 1 | +/** |
| 2 | + * Utilities to make API calls against the Databricks API with retry, timeout, |
| 3 | + * and rate limiting support. |
| 4 | + */ |
| 5 | + |
| 6 | +import type {Option, Options} from './options'; |
| 7 | +import type {Retrier} from './retrier'; |
| 8 | + |
| 9 | +/** |
| 10 | + * A function representing a single API call attempt. Throw an error to |
| 11 | + * indicate failure; return normally to indicate success. |
| 12 | + */ |
| 13 | +export type Call = (signal: AbortSignal) => Promise<void>; |
| 14 | + |
| 15 | +/** |
| 16 | + * Makes an API call using the given options for retry, timeout, and rate |
| 17 | + * limiting behavior. |
| 18 | + */ |
| 19 | +export async function execute(call: Call, ...opts: Option[]): Promise<void> { |
| 20 | + const options: Options = {}; |
| 21 | + for (const opt of opts) { |
| 22 | + opt.apply(options); |
| 23 | + } |
| 24 | + |
| 25 | + await executeImpl(call, options, sleepMs); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Sleeps for the given duration in milliseconds. Rejects with an |
| 30 | + * AbortError if the signal is aborted before the sleep completes. |
| 31 | + */ |
| 32 | +function sleepMs(ms: number, signal: AbortSignal): Promise<void> { |
| 33 | + return new Promise<void>((resolve, reject) => { |
| 34 | + if (signal.aborted) { |
| 35 | + reject(signal.reason as Error); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const timer = setTimeout(() => { |
| 40 | + signal.removeEventListener('abort', onAbort); |
| 41 | + resolve(); |
| 42 | + }, ms); |
| 43 | + |
| 44 | + function onAbort(): void { |
| 45 | + clearTimeout(timer); |
| 46 | + reject(signal.reason as Error); |
| 47 | + } |
| 48 | + |
| 49 | + signal.addEventListener('abort', onAbort, {once: true}); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +// Convenience type for readability and testability. |
| 54 | +type Sleeper = (ms: number, signal: AbortSignal) => Promise<void>; |
| 55 | + |
| 56 | +/** |
| 57 | + * The actual implementation of execute, separated for testability. |
| 58 | + */ |
| 59 | +async function executeImpl( |
| 60 | + apiCall: Call, |
| 61 | + opts: Options, |
| 62 | + sleep: Sleeper |
| 63 | +): Promise<void> { |
| 64 | + // Set up abort controller with optional timeout. |
| 65 | + const controller = new AbortController(); |
| 66 | + const {signal} = controller; |
| 67 | + |
| 68 | + let timeoutId: ReturnType<typeof setTimeout> | undefined; |
| 69 | + if (opts.timeoutMs !== undefined && opts.timeoutMs > 0) { |
| 70 | + timeoutId = setTimeout(() => { |
| 71 | + controller.abort( |
| 72 | + new DOMException('The operation timed out.', 'TimeoutError') |
| 73 | + ); |
| 74 | + }, opts.timeoutMs); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + // Lazily instantiated retrier. |
| 79 | + let retrier: Retrier | undefined; |
| 80 | + |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition |
| 82 | + while (true) { |
| 83 | + if (opts.rateLimiter !== undefined) { |
| 84 | + await opts.rateLimiter.wait(signal); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + await apiCall(signal); |
| 89 | + return; // Success — nothing to retry. |
| 90 | + } catch (err: unknown) { |
| 91 | + if (retrier === undefined) { |
| 92 | + if (opts.retrier !== undefined) { |
| 93 | + retrier = opts.retrier(); |
| 94 | + } |
| 95 | + if (retrier === undefined) { |
| 96 | + throw err; // No retrier — no retry. |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const decision = retrier.isRetriable(err); |
| 101 | + if (!decision.retriable) { |
| 102 | + throw err; // Not retriable. |
| 103 | + } |
| 104 | + |
| 105 | + await sleep(decision.delayMs, signal); |
| 106 | + } |
| 107 | + } |
| 108 | + } finally { |
| 109 | + if (timeoutId !== undefined) { |
| 110 | + clearTimeout(timeoutId); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments