Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exponential backoff for s3 503 error #46

Merged
merged 6 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions action/__tests__/plugins/staticHosting/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as core from '@actions/core';
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
import fetch, { Response } from 'cross-fetch';

import { uploadFileWithSignedUrl } from '../../../src/plugins/staticHosting/helpers';

jest.mock('cross-fetch', () => jest.fn());
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;

jest.mock('@actions/core');
const mockCore = core as jest.Mocked<typeof core>;

describe('uploadFileWithSignedUrl', function () {
afterEach(() => {
jest.restoreAllMocks();
});

describe('when there is no error', function () {
it('uploads the file with one fetch call', async () => {
mockFetch.mockResolvedValue({ ok: true } as Response);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should move this setup to a "beforeEach" block to be consistent across different tests within this file.

await uploadFileWithSignedUrl('https://localhost', {}, 'object-key', 'package.json');
expect(mockFetch).toHaveBeenCalledTimes(1);
});
});

describe('when there is 503 error', function () {
const response503 = { ok: false, status: 503, statusText: 'Slow down' } as Response;

beforeEach(() => {
mockFetch
.mockResolvedValueOnce(response503)
.mockResolvedValueOnce(response503)
.mockResolvedValue({ ok: true, status: 204, statusText: 'No content' } as Response);
});

it('retries', async () => {
await uploadFileWithSignedUrl('https://localhost', {}, 'object-key', 'package.json');
expect(mockFetch).toHaveBeenCalledTimes(3);

const warnings = mockCore.warning.mock.calls;
expect(warnings.length).toEqual(2);
warnings.forEach((warning) => {
expect(warning[0]).toContain('503 error');
});
});
});
});
27 changes: 22 additions & 5 deletions action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28530,11 +28530,28 @@ const uploadFileWithSignedUrl = (signedUrl, fields, objectKey, localFilePath, dr
form.append('key', objectKey);
form.append('Content-Type', mime_types.lookup(localFilePath) || 'application/octet-stream');
form.append('file', external_fs_default().readFileSync(localFilePath));
const response = yield node_ponyfill_default()(signedUrl, {
method: 'POST',
body: form
});
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${response.status} - ${response.statusText}`);
let retry = 0;
const maxRetry = 6;
while (retry <= maxRetry) {
const { ok, status, statusText } = yield node_ponyfill_default()(signedUrl, {
method: 'POST',
body: form
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);
if (ok) {
break;
}
else if (status === 503) {
const waitingMillis = Math.pow(2, retry) * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
yield new Promise((r) => setTimeout(r, waitingMillis));
}
else {
throw new Error(statusText);
}
++retry;
}
});
// Reference:
// https://github.com/elysiumphase/s3-lambo/blob/master/lib/index.js#L255
Expand Down
2 changes: 1 addition & 1 deletion action/dist/index.js.map

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions action/src/plugins/staticHosting/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,27 @@ export const uploadFileWithSignedUrl = async (
form.append('Content-Type', mime.lookup(localFilePath) || 'application/octet-stream');
form.append('file', fs.readFileSync(localFilePath));

const response = await fetch(signedUrl, {
method: 'POST',
body: form as any
});
let retry = 0;
const maxRetry = 6;
while (retry <= maxRetry) {
const { ok, status, statusText } = await fetch(signedUrl, {
method: 'POST',
body: form as any
});
const retryStatus = retry > 0 ? ` (retry ${retry})` : '';
core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${status} - ${statusText}${retryStatus}`);

core.info(`-- Upload ${localFilePath} -> ${objectKey}: ${response.status} - ${response.statusText}`);
if (ok) {
break;
} else if (status === 503) {
const waitingMillis = 2 ** retry * 100;
core.warning(`-- Hit 503 error, waiting for ${waitingMillis}ms before retry (${retry})...`);
await new Promise((r) => setTimeout(r, waitingMillis));
} else {
throw new Error(statusText);
}
++retry;
}
};

// Reference:
Expand Down