-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
172fa2a
commit 1ebff88
Showing
9 changed files
with
4,329 additions
and
14,597 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
type RetryOptions = { | ||
maxRetries: number; | ||
timeout: number; | ||
}; | ||
|
||
const timeout = (t: number) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, t); | ||
}); | ||
|
||
export async function retry<T extends (signal: AbortSignal) => any>( | ||
fn: T, | ||
options: RetryOptions | ||
): Promise<ReturnType<T>> { | ||
let numRetries = 0; | ||
const errors = []; | ||
|
||
const abortController = new AbortController(); | ||
|
||
while (numRetries < options.maxRetries) { | ||
try { | ||
return Promise.race([ | ||
fn(abortController.signal), | ||
timeout(options.timeout).then(() => { | ||
throw new Error("timeout"); | ||
}), | ||
]); | ||
} catch (err) { | ||
errors.push(err); | ||
} | ||
|
||
await timeout(2000 * 2 ** numRetries); | ||
numRetries++; | ||
} | ||
|
||
const error = new Error("max retries"); | ||
error.cause = errors; | ||
|
||
throw error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters