-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(sse): bound workspace SSE connection lifetime #7058
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
5 commits
Select commit
Hold shift + click to select a range
fb1fead
fix(sse): bound workspace SSE connection lifetime
waleedlatif1 0c22829
fix(mothership): resync chat caches after an SSE reconnect gap
waleedlatif1 70541f4
fix(mothership): keep reconnect resync off locally streaming chats
waleedlatif1 f76765f
fix(mothership): only skip resync for a stream still running
waleedlatif1 cbe8898
fix(sse): raise the ceiling and narrow reconnect resync to the lists
waleedlatif1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
|
|
||
| import { authMockFns, permissionsMock, permissionsMockFns } from '@sim/testing' | ||
| import { NextRequest } from 'next/server' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| createWorkspaceSSE, | ||
| HEARTBEAT_INTERVAL_MS, | ||
| MAX_CONNECTION_JITTER_MS, | ||
| MAX_CONNECTION_MS, | ||
| MAX_UNDRAINED_CHUNKS, | ||
| } from '@/lib/events/sse-endpoint' | ||
|
|
||
| vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock) | ||
|
|
||
| const PAST_CEILING_MS = MAX_CONNECTION_MS + MAX_CONNECTION_JITTER_MS + HEARTBEAT_INTERVAL_MS | ||
|
|
||
| /** Enough undrained heartbeats to trip the unread check, and no more. */ | ||
| const PAST_UNREAD_MS = (MAX_UNDRAINED_CHUNKS + 2) * HEARTBEAT_INTERVAL_MS | ||
|
|
||
| async function openConnection(signal: AbortSignal = new AbortController().signal) { | ||
| const unsubscribe = vi.fn() | ||
| const handler = createWorkspaceSSE({ | ||
| label: 'test', | ||
| subscriptions: [{ subscribe: () => unsubscribe }], | ||
| }) | ||
| const request = new NextRequest(new URL('https://sim.test/api/test/events?workspaceId=ws-1'), { | ||
| signal, | ||
| }) | ||
| const response = await handler(request) | ||
|
|
||
| return { body: response.body as ReadableStream<Uint8Array>, unsubscribe } | ||
| } | ||
|
|
||
| /** Resolves once the stream closes. */ | ||
| async function drain(body: ReadableStream<Uint8Array>): Promise<void> { | ||
| const reader = body.getReader() | ||
| while (true) { | ||
| const { done } = await reader.read() | ||
| if (done) return | ||
| } | ||
| } | ||
|
|
||
| describe('createWorkspaceSSE', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| vi.useFakeTimers() | ||
| authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) | ||
| permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('admin') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('releases subscriptions and closes the stream when the connection reaches its ceiling', async () => { | ||
| const { body, unsubscribe } = await openConnection() | ||
| const drained = drain(body) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(PAST_CEILING_MS) | ||
|
|
||
| await drained | ||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('releases subscriptions when the consumer stops draining the stream', async () => { | ||
| const { unsubscribe } = await openConnection() | ||
|
|
||
| await vi.advanceTimersByTimeAsync(PAST_UNREAD_MS) | ||
|
|
||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('keeps a drained connection alive past the unread threshold', async () => { | ||
| const { body, unsubscribe } = await openConnection() | ||
| void drain(body) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(PAST_UNREAD_MS) | ||
|
|
||
| expect(unsubscribe).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('releases subscriptions when the request aborts', async () => { | ||
| const controller = new AbortController() | ||
| const { unsubscribe } = await openConnection(controller.signal) | ||
|
|
||
| controller.abort() | ||
|
|
||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('releases subscriptions when the consumer cancels the stream', async () => { | ||
| const { body, unsubscribe } = await openConnection() | ||
|
|
||
| await body.cancel() | ||
|
|
||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('releases subscriptions once when abort and the ceiling both elapse', async () => { | ||
| const controller = new AbortController() | ||
| const { unsubscribe } = await openConnection(controller.signal) | ||
|
|
||
| controller.abort() | ||
| await vi.advanceTimersByTimeAsync(PAST_CEILING_MS) | ||
|
|
||
| expect(unsubscribe).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
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.