Repository navigation
fix(relay): buffer messages during WebSocket admission #1045
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
4 commits
Select commit
Hold shift + click to select a range
b252b63
fix(relay): buffer messages during WebSocket admission
Ferryx349 ccb1c40
fix(relay): resume closing sockets after WebSocket admission
Ferryx349 9d171af
Merge branch 'main' into feat/peering-feeds
Ferryx349 7e8359f
chore(test): revert unrelated deploy timeout change
Ferryx349 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| "nostream": patch | ||
| --- | ||
|
|
||
| fix(relay): handle messages sent immediately after a WebSocket opens | ||
|
|
||
| Attaches a temporary message listener before the awaited connection | ||
| rate-limit check, pauses the socket during admission, and buffers early | ||
| frames with bounded limits (64 messages / 1 MiB). Once admission succeeds the | ||
| adapter is created, buffered frames are replayed in arrival order, and the | ||
| socket resumes. Rate-limited connections are still terminated before any | ||
| message is processed, and buffer overflow closes with code 1009 instead of | ||
| silently dropping frames. Adds regression tests for early-message replay, | ||
| rate-limited rejection, and overflow handling. |
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
114 changes: 114 additions & 0 deletions
114
test/unit/adapters/web-socket-server-adapter-real.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,114 @@ | ||
| import http from 'node:http' | ||
| import { AddressInfo } from 'node:net' | ||
|
|
||
| import chai from 'chai' | ||
| import Sinon from 'sinon' | ||
| import WebSocket, { WebSocketServer } from 'ws' | ||
|
|
||
| import { WebSocketServerAdapter } from '../../../src/adapters/web-socket-server-adapter' | ||
| import * as rateLimiterMiddleware from '../../../src/handlers/request-handlers/rate-limiter-middleware' | ||
|
|
||
| const { expect } = chai | ||
|
|
||
| const waitFor = async (predicate: () => boolean, timeoutMs = 3_000): Promise<void> => { | ||
| const startedAt = Date.now() | ||
| while (!predicate()) { | ||
| if (Date.now() - startedAt > timeoutMs) { | ||
| throw new Error('timed out waiting for condition') | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, 10)) | ||
| } | ||
| } | ||
|
|
||
| describe('WebSocketServerAdapter real socket admission', () => { | ||
| let sandbox: Sinon.SinonSandbox | ||
| let server: http.Server | ||
| let webSocketServer: WebSocketServer | ||
| let adapter: WebSocketServerAdapter | ||
| let isRateLimitedStub: Sinon.SinonStub | ||
| let receivedFrames: Buffer[] | undefined | ||
| const clients: WebSocket[] = [] | ||
|
|
||
| const settings = () => | ||
| ({ | ||
| network: { remoteIpHeader: '' }, | ||
| limits: { connection: { rateLimits: [] } }, | ||
| }) as any | ||
|
|
||
| beforeEach(async () => { | ||
| sandbox = Sinon.createSandbox() | ||
| receivedFrames = undefined | ||
| isRateLimitedStub = sandbox.stub(rateLimiterMiddleware, 'isRateLimited').resolves(false) | ||
|
|
||
| server = http.createServer() | ||
| webSocketServer = new WebSocketServer({ server }) | ||
|
|
||
| adapter = new WebSocketServerAdapter( | ||
| server, | ||
| webSocketServer, | ||
| ([client]: [WebSocket, unknown, unknown]) => { | ||
| const received: Buffer[] = [] | ||
| receivedFrames = received | ||
| client.on('message', (data: Buffer) => received.push(data)) | ||
|
|
||
| return { | ||
| emit: () => undefined, | ||
| drainAndClose: () => client.close(), | ||
| getClientId: () => 'real-client', | ||
| getClientAddress: () => '127.0.0.1', | ||
| } as any | ||
| }, | ||
| settings, | ||
| ) | ||
|
|
||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| for (const client of clients.splice(0)) { | ||
| if (client.readyState === WebSocket.OPEN) { | ||
| client.terminate() | ||
| } | ||
| } | ||
|
|
||
| await new Promise<void>((resolve) => adapter.close(() => resolve())) | ||
| sandbox.restore() | ||
| }) | ||
|
|
||
| it('delivers frames sent immediately on open across a delayed admission check', async () => { | ||
| let releaseAdmission: ((limited: boolean) => void) | undefined | ||
| isRateLimitedStub.returns( | ||
| new Promise<boolean>((resolve) => { | ||
| releaseAdmission = resolve | ||
| }), | ||
| ) | ||
|
|
||
| const address = server.address() as AddressInfo | ||
| const client = new WebSocket(`ws://127.0.0.1:${address.port}`) | ||
| clients.push(client) | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| client.once('open', () => resolve()) | ||
| client.once('error', reject) | ||
| }) | ||
|
|
||
| const frames = [ | ||
| Buffer.from('["REQ","immediate-one",{"kinds":[1]}]'), | ||
| Buffer.from('["REQ","immediate-two",{"kinds":[1]}]'), | ||
| ] | ||
|
|
||
| for (const frame of frames) { | ||
| client.send(frame) | ||
| } | ||
|
|
||
| // Give the frames time to reach the relay while admission is still pending. | ||
| await new Promise((resolve) => setTimeout(resolve, 50)) | ||
| releaseAdmission?.(false) | ||
|
|
||
| await waitFor(() => (receivedFrames?.length ?? 0) >= frames.length) | ||
|
|
||
| expect((receivedFrames ?? []).map((frame) => frame.toString())).to.deep.equal( | ||
| frames.map((frame) => frame.toString()), | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.