Skip to content

Commit 416834e

Browse files
authored
fix: tweak the delay factor when downloading artifacts (#244)
1 parent 541bf48 commit 416834e

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

dist/cli/index.js

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/domain/artifact.spec.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('Artifact', () => {
6767

6868
expect(axiosRetry).toHaveBeenCalledWith(axios, {
6969
retries: 3,
70-
70+
onRetry: expect.any(Function),
7171
retryCondition: expect.matchesPredicate(
7272
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
7373
(retryConditionFn: Function) => {
@@ -79,21 +79,18 @@ describe('Artifact', () => {
7979
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
8080
retryDelay: expect.matchesPredicate((retryDelayFn: Function) => {
8181
// Make sure the retry delays follow exponential backoff
82-
// and the final retry happens after at least 1 minute total
83-
// (in this case, at least 70 seconds).
84-
// Axios randomly adds an extra 0-20% of jitter to each delay.
85-
// Test upper bounds as well to ensure the workflow completes reasonably quickly
86-
// (in this case, no more than 84 seconds total).
82+
// Axios randomly adds an extra 0-20% of jitter to each delay:
83+
// https://github.com/softonic/axios-retry/blob/3f9557920b816ec4f692870d89939ae739d7f8ed/src/index.ts#L169
8784
const firstRetryDelay = retryDelayFn.call(this, 0);
8885
const secondRetryDelay = retryDelayFn.call(this, 1);
8986
const thirdRetryDelay = retryDelayFn.call(this, 2);
9087
return (
91-
10000 <= firstRetryDelay &&
92-
firstRetryDelay <= 12000 &&
93-
20000 <= secondRetryDelay &&
94-
secondRetryDelay <= 24000 &&
95-
40000 <= thirdRetryDelay &&
96-
thirdRetryDelay <= 48000
88+
2000 <= firstRetryDelay &&
89+
firstRetryDelay <= 2400 &&
90+
4000 <= secondRetryDelay &&
91+
secondRetryDelay <= 4800 &&
92+
8000 <= thirdRetryDelay &&
93+
thirdRetryDelay <= 9600
9794
);
9895
}),
9996
shouldResetTimeout: true,

src/domain/artifact.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class ArtifactDownloadError extends Error {
2323
* An artifact that can be downloaded and have its integrity hash computed.
2424
*/
2525
export class Artifact {
26+
public static readonly MAX_RETRIES = 3;
2627
private _diskPath: string | null = null;
2728
public constructor(public readonly url: string) {}
2829

@@ -66,10 +67,14 @@ export class Artifact {
6667
const writer = fs.createWriteStream(dest, { flags: 'w' });
6768

6869
// Retry the request in case the artifact is still being uploaded.
69-
// Exponential backoff with 3 retries and a delay factor of 10 seconds
70-
// gives you at least 70 seconds to upload a release archive.
7170
axiosRetry(axios, {
72-
retries: 3,
71+
onRetry(retryCount, error, _requestConfig) {
72+
console.error(`Failed to download artifact; ${error.message}`);
73+
console.error(
74+
`Retry atempt ${retryCount} / ${Artifact.MAX_RETRIES}...`
75+
);
76+
},
77+
retries: Artifact.MAX_RETRIES,
7378
retryDelay: exponentialDelay,
7479
shouldResetTimeout: true,
7580
retryCondition: defaultRetryPlus404,
@@ -130,8 +135,8 @@ function exponentialDelay(
130135
retryCount: number,
131136
error: AxiosError | undefined
132137
): number {
133-
// Default delay factor is 10 seconds, but can be overridden for testing.
134-
const delayFactor = Number(process.env.BACKOFF_DELAY_FACTOR) || 10_000;
138+
// Default delay factor is 2 seconds, but can be overridden for testing.
139+
const delayFactor = Number(process.env.BACKOFF_DELAY_FACTOR) || 2000;
135140
return axiosRetry.exponentialDelay(retryCount, error, delayFactor);
136141
}
137142

0 commit comments

Comments
 (0)