Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 3 additions & 5 deletions packages/metro-cache/src/stores/HttpStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,12 @@ export default class HttpStore<T> {
return backOff(fn, {
jitter: 'full',
maxDelay: 30000,
numOfAttempts: this.#getEndpoint.maxAttempts || Number.POSITIVE_INFINITY,
numOfAttempts: endpoint.maxAttempts || Number.POSITIVE_INFINITY,
retry: (e: Error) => {
if (e instanceof HttpError) {
return this.#getEndpoint.retryStatuses.has(e.code);
return endpoint.retryStatuses.has(e.code);
}
return (
e instanceof NetworkError && this.#getEndpoint.retryNetworkErrors
);
return e instanceof NetworkError && endpoint.retryNetworkErrors;
},
});
}
Expand Down
33 changes: 33 additions & 0 deletions packages/metro-cache/src/stores/__tests__/HttpStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,39 @@ describe('HttpStore', () => {
});
});

test('set() retries use the set endpoint config, not the get endpoint', async () => {
jest.useRealTimers();
// Distinct read/write retry config: reads do not retry, writes retry 503s.
const store = new HttpStore({
getOptions: {endpoint: 'http://example.com', maxAttempts: 1},
setOptions: {
endpoint: 'http://example.com',
maxAttempts: 2,
retryStatuses: [503],
},
});
const {request} = require('http');

request.mockImplementation((opts, callback) => {
if (request.mock.calls.length === 1) {
callback(responseHttpError(503));
} else {
callback(responseHttpOk(''));
}
return new PassThrough();
});

let error;
try {
await store.set(Buffer.from('key-set'), {foo: 42});
} catch (e) {
error = e;
}

expect(error).toBeUndefined();
expect(request).toHaveBeenCalledTimes(2);
});

test('sets using the network via PUT method', done => {
const store = new HttpStore({endpoint: 'http://www.example.com/endpoint'});
const promise = store.set(Buffer.from('key-set'), {foo: 42});
Expand Down