-
Notifications
You must be signed in to change notification settings - Fork 2
feat(graphql): block retry if connection not established #854
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
73aba32
feat(graphql): block retry if connection not established
a400afd
polish spec
19d8cef
polish graphql service
0795f81
refactor log
0bacdaf
add teardownService
52006b5
Update packages/javascript-api/src/lib/services/graphql/__fixtures__/…
stdevi 33627b4
Update packages/javascript-api/src/lib/services/graphql/__fixtures__/…
stdevi 53bfaa4
Update packages/javascript-api/src/lib/services/graphql/__fixtures__/…
stdevi b41fa11
fix
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
95 changes: 95 additions & 0 deletions
95
packages/javascript-api/src/lib/services/graphql/__tests__/graphql-blocked-websocket.spec.ts
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { WebSocket } from 'mock-socket'; | ||
|
|
||
| import { GraphQLSubscriptionsFixture } from '../__fixtures__/graphql-subscriptions-fixture'; | ||
| import * as backoff from '../../../util/randomized-exponential-backoff/randomized-exponential-backoff'; | ||
|
|
||
| jest.mock('isomorphic-ws', () => WebSocket); | ||
| jest.mock('../../../util/sleep-ms/sleep-ms', () => ({ | ||
| sleepMs: () => new Promise((resolve) => setTimeout(resolve, 4)), | ||
| })); | ||
|
|
||
| // Mirrors the constants in graphql.service.ts (not exported). | ||
| const MAX_FAILED_HANDSHAKES = 5; | ||
| const BLOCKED_RETRY_INTERVAL_MS = 5 * 60 * 1000; | ||
|
|
||
| describe('GraphQL blocked-WebSocket back-off', () => { | ||
| let fixture: GraphQLSubscriptionsFixture; | ||
|
|
||
| beforeEach(() => { | ||
| fixture = new GraphQLSubscriptionsFixture(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fixture.cleanup(); | ||
| }); | ||
|
|
||
| it('should back off to a slow interval after repeated handshakes fail before connecting', async () => { | ||
| const service = fixture.graphqlService as any; | ||
| const backoffSpy = jest.spyOn( | ||
| backoff, | ||
| 'calculateRandomizedExponentialBackoffTime', | ||
| ); | ||
| const infoSpy = jest.spyOn(service.logger, 'info'); | ||
|
|
||
| const sub = fixture.triggerSubscription(); | ||
|
|
||
| // Each cycle: connect, never ACK, close before the connection is established. | ||
| for (let i = 0; i < MAX_FAILED_HANDSHAKES; i++) { | ||
| await fixture.waitForConnection(); | ||
| await fixture.consumeInitMessage(); | ||
| await fixture.closeWithCode(1001); | ||
| fixture.openServer(); | ||
| } | ||
|
|
||
| expect(service.consecutiveFailedHandshakes).toBeGreaterThanOrEqual( | ||
| MAX_FAILED_HANDSHAKES, | ||
| ); | ||
| // Fast exponential backoff is used only for the first (MAX - 1) reconnects; | ||
| // once the cap is hit, the flat slow interval is used instead. | ||
| expect(backoffSpy).toHaveBeenCalledTimes(MAX_FAILED_HANDSHAKES - 1); | ||
| expect(infoSpy).toHaveBeenCalledWith( | ||
| `Reconnect socket in ${BLOCKED_RETRY_INTERVAL_MS}ms`, | ||
| ); | ||
|
|
||
| sub.unsubscribe(); | ||
| }); | ||
|
|
||
| it('should not count a close that happens after a successful connection', async () => { | ||
| const service = fixture.graphqlService as any; | ||
|
|
||
| const sub = fixture.triggerSubscription(); | ||
| await fixture.handleConnectionInit(); // connection_ack -> CONNECTED | ||
| await fixture.consumeSubscribeMessage(); | ||
|
|
||
| expect(service.consecutiveFailedHandshakes).toBe(0); | ||
|
|
||
| // A drop after the connection was established is a normal reconnect. | ||
| await fixture.closeWithCode(1001); | ||
| expect(service.consecutiveFailedHandshakes).toBe(0); | ||
|
|
||
| fixture.openServer(); | ||
| await fixture.handleConnectionInit(); | ||
| sub.unsubscribe(); | ||
| }); | ||
|
|
||
| it('should reset the counter when the connection is established', async () => { | ||
| const service = fixture.graphqlService as any; | ||
|
|
||
| const sub = fixture.triggerSubscription(); | ||
|
|
||
| for (let i = 0; i < 2; i++) { | ||
| await fixture.waitForConnection(); | ||
| await fixture.getNextMessage(); | ||
| await fixture.closeWithCode(1001); | ||
| fixture.openServer(); | ||
| } | ||
| expect(service.consecutiveFailedHandshakes).toBe(2); | ||
|
|
||
| // A successful connection_ack resets the counter back to fast reconnects. | ||
| await fixture.handleConnectionInit(); | ||
| await fixture.consumeSubscribeMessage(); | ||
| expect(service.consecutiveFailedHandshakes).toBe(0); | ||
|
|
||
| sub.unsubscribe(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.