This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
feat: add retry to callApi #384
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| import { FetchRetryOptions } from './types'; | ||
|
|
||
| // HTTP status codes | ||
| export const HTTP_STATUS_OK = 200; | ||
| export const HTTP_STATUS_NOT_MODIFIED = 304; | ||
|
|
||
| // Namespace for Cache API | ||
| export const CACHE_AVAILABLE = 'caches' in self; | ||
| export const CACHE_KEY = '@SUPERSET-UI/CONNECTION'; | ||
|
|
||
| export const DEFAULT_FETCH_RETRY_OPTIONS: FetchRetryOptions = { | ||
| retries: 3, | ||
| retryDelay: 1000, | ||
| retryOn: [503], | ||
| }; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import * as constants from '../../src/constants'; | |
| import { LOGIN_GLOB } from '../fixtures/constants'; | ||
| import throwIfCalled from '../utils/throwIfCalled'; | ||
| import { CallApi } from '../../src/types'; | ||
| import { DEFAULT_FETCH_RETRY_OPTIONS } from '../../src/constants'; | ||
|
|
||
| describe('callApi()', () => { | ||
| beforeAll(() => { | ||
|
|
@@ -20,6 +21,8 @@ describe('callApi()', () => { | |
| const mockPatchUrl = '/mock/patch/url'; | ||
| const mockCacheUrl = '/mock/cache/url'; | ||
| const mockNotFound = '/mock/notfound'; | ||
| const mockErrorUrl = '/mock/error/url'; | ||
| const mock503 = '/mock/503'; | ||
|
|
||
| const mockGetPayload = { get: 'payload' }; | ||
| const mockPostPayload = { post: 'payload' }; | ||
|
|
@@ -30,13 +33,16 @@ describe('callApi()', () => { | |
| body: 'BODY', | ||
| headers: { Etag: 'etag' }, | ||
| }; | ||
| const mockErrorPayload = { status: 500, statusText: 'Internal error' }; | ||
|
|
||
| fetchMock.get(mockGetUrl, mockGetPayload); | ||
| fetchMock.post(mockPostUrl, mockPostPayload); | ||
| fetchMock.put(mockPutUrl, mockPutPayload); | ||
| fetchMock.patch(mockPatchUrl, mockPatchPayload); | ||
| fetchMock.get(mockCacheUrl, mockCachePayload); | ||
| fetchMock.get(mockNotFound, { status: 404 }); | ||
| fetchMock.get(mock503, { status: 503 }); | ||
| fetchMock.get(mockErrorUrl, () => Promise.reject(mockErrorPayload)); | ||
|
|
||
| afterEach(fetchMock.reset); | ||
|
|
||
|
|
@@ -437,19 +443,47 @@ describe('callApi()', () => { | |
| }); | ||
| }); | ||
|
|
||
| it('rejects if the request throws', () => { | ||
| const mockErrorUrl = '/mock/error/url'; | ||
| const mockErrorPayload = { status: 500, statusText: 'Internal error' }; | ||
| fetchMock.get(mockErrorUrl, () => Promise.reject(mockErrorPayload)); | ||
| it('rejects after retrying thrice if the request throws', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could update the tests to match the retry config variables specified if we expose them
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is pretty awesome tho that the throwing is delayed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
| expect.assertions(3); | ||
|
|
||
| return callApi({ | ||
| fetchRetryOptions: DEFAULT_FETCH_RETRY_OPTIONS, | ||
| url: mockErrorUrl, | ||
| method: 'GET', | ||
| }) | ||
| .then(throwIfCalled) | ||
| .catch(error => { | ||
| expect(fetchMock.calls(mockErrorUrl)).toHaveLength(4); | ||
| expect(error.status).toBe(mockErrorPayload.status); | ||
| expect(error.statusText).toBe(mockErrorPayload.statusText); | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects without retries if the config is set to 0 retries', () => { | ||
| expect.assertions(3); | ||
|
|
||
| return callApi({ url: mockErrorUrl, method: 'GET' }) | ||
| return callApi({ | ||
| fetchRetryOptions: { retries: 0 }, | ||
| url: mockErrorUrl, | ||
| method: 'GET', | ||
| }) | ||
| .then(throwIfCalled) | ||
| .catch(error => { | ||
| expect(fetchMock.calls(mockErrorUrl)).toHaveLength(1); | ||
| expect(error.status).toBe(mockErrorPayload.status); | ||
| expect(error.statusText).toBe(mockErrorPayload.statusText); | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects after retrying thrice if the request returns a 503', async () => { | ||
| const url = mock503; | ||
| const response = await callApi({ | ||
| fetchRetryOptions: DEFAULT_FETCH_RETRY_OPTIONS, | ||
| url, | ||
| method: 'GET', | ||
| }); | ||
| const calls = fetchMock.calls(url); | ||
| expect(calls).toHaveLength(4); | ||
| expect(response.status).toEqual(503); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the retry options to
callApiarguments, too, in case people want to override the defaults?I'd also argue it's probably safer to make this an opt-in rather than opt-out. Can add an API to
superset-ui/connectionto change the defaults.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the retry options to the SupersetClient config. However, I think it's fair to make this opt out. I can add a comment in incubator-superset that this was added, but I think the common path should include QOL features like automatic retried on network issues