Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/azure-pipelines/common/retry.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export async function retry<T>(fn: (attempt: number) => Promise<T>): Promise<T> {
7
let lastError: Error | undefined;
8
9
for (let run = 1; run <= 10; run++) {
10
try {
11
return await fn(run);
12
} catch (err) {
13
if (!/fetch failed|terminated|aborted|timeout|TimeoutError|Timeout Error|RestError|Client network socket disconnected|socket hang up|ECONNRESET|CredentialUnavailableError|endpoints_resolution_error|Audience validation failed|end of central directory record signature not found/i.test(err.message)) {
14
throw err;
15
}
16
17
lastError = err;
18
19
// maximum delay is 10th retry: ~3 seconds
20
const millis = Math.floor((Math.random() * 200) + (50 * Math.pow(1.5, run)));
21
await new Promise(c => setTimeout(c, millis));
22
}
23
}
24
25
console.error(`Too many retries, aborting.`);
26
throw lastError;
27
}
28
29