Skip to content

Commit e70433c

Browse files
t3dotggclaude
andcommitted
perf(server): negotiate permessage-deflate on the websocket
Patch @effect/platform-node and @effect/platform-bun so the WebSocket server accepts permessage-deflate from clients that offer it. Browsers offer the extension automatically, so web/desktop connections get compressed frames with no client changes; clients that do not offer it keep uncompressed frames on their connection, so the change is backwards compatible per-connection. Context takeover stays enabled (ws default / dedicated compressors on Bun) so the per-connection compression window is shared across frames — that is where the win comes from on our streaming traffic of small JSON frames with repeated keys and UUIDs. The Node path also sets a 512-byte threshold so tiny frames skip compression. Measured on real activity frames replayed from a production database through the wire (raw TCP byte counts): 4.0x reduction on pruned thread.activity-appended traffic, 3.7x on unpruned frames. Adds a server test asserting the 101 response accepts the extension when offered and that connections without the offer still work. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 476d69c commit e70433c

5 files changed

Lines changed: 145 additions & 31 deletions

File tree

‎apps/server/src/server.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,6 +3081,34 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
30813081
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
30823082
);
30833083

3084+
it.effect("negotiates permessage-deflate with clients that offer it", () =>
3085+
Effect.gen(function* () {
3086+
yield* buildAppUnderTest();
3087+
3088+
const { cookie, url } = parseSessionCookieFromWsUrl(yield* getWsServerUrl("/ws"));
3089+
const openSocket = (perMessageDeflate: boolean) =>
3090+
Effect.acquireRelease(
3091+
Effect.callback<NodeSocket.NodeWS.WebSocket, Error>((resume) => {
3092+
const socket = new NodeSocket.NodeWS.WebSocket(url, {
3093+
perMessageDeflate,
3094+
...(cookie ? { headers: { cookie } } : {}),
3095+
});
3096+
socket.on("open", () => resume(Effect.succeed(socket)));
3097+
socket.on("error", (error) => resume(Effect.fail(error)));
3098+
}),
3099+
(socket) => Effect.sync(() => socket.close()),
3100+
);
3101+
3102+
const compressed = yield* openSocket(true);
3103+
// The ws client records the negotiated extension only when the server's
3104+
// 101 response accepted the offer.
3105+
assert.include(compressed.extensions, "permessage-deflate");
3106+
3107+
const plain = yield* openSocket(false);
3108+
assert.notInclude(plain.extensions, "permessage-deflate");
3109+
}).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)),
3110+
);
3111+
30843112
it.effect("issues short-lived websocket tickets for authenticated bearer sessions", () =>
30853113
Effect.gen(function* () {
30863114
yield* buildAppUnderTest();

‎patches/@[email protected]‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/dist/BunHttpServer.js b/dist/BunHttpServer.js
2+
index 5fb0cbacc183b58e31a442badf4aef06bebcd592..c74a79be2657ab5e42f21351aa4c206dec13ec06 100644
3+
--- a/dist/BunHttpServer.js
4+
+++ b/dist/BunHttpServer.js
5+
@@ -45,6 +45,14 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
6+
...options,
7+
fetch: handlerStack[0],
8+
websocket: {
9+
+ // Negotiate permessage-deflate with clients that offer it; clients that
10+
+ // don't still get uncompressed frames on their connection. Dedicated
11+
+ // compressors keep per-connection sliding windows (context takeover) so
12+
+ // the compression dictionary is shared across frames.
13+
+ perMessageDeflate: {
14+
+ compress: "dedicated",
15+
+ decompress: "dedicated"
16+
+ },
17+
open(ws) {
18+
Deferred.doneUnsafe(ws.data.deferred, Exit.succeed(ws));
19+
},
20+
diff --git a/src/BunHttpServer.ts b/src/BunHttpServer.ts
21+
index 9f2ac3dfc018e2d35c6f44785f1ce05e2e06fc7c..c1e5869fecc5d635ff89cd303feaf1efd6d06ef4 100644
22+
--- a/src/BunHttpServer.ts
23+
+++ b/src/BunHttpServer.ts
24+
@@ -90,6 +90,14 @@ export const make = Effect.fnUntraced(
25+
...options as ServeOptions<R>,
26+
fetch: handlerStack[0],
27+
websocket: {
28+
+ // Negotiate permessage-deflate with clients that offer it; clients that
29+
+ // don't still get uncompressed frames on their connection. Dedicated
30+
+ // compressors keep per-connection sliding windows (context takeover) so
31+
+ // the compression dictionary is shared across frames.
32+
+ perMessageDeflate: {
33+
+ compress: "dedicated",
34+
+ decompress: "dedicated"
35+
+ },
36+
open(ws) {
37+
Deferred.doneUnsafe(ws.data.deferred, Exit.succeed(ws))
38+
},

‎patches/@[email protected]‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
diff --git a/dist/NodeHttpServer.js b/dist/NodeHttpServer.js
2+
index ab7c9354853032fefa192ebfa8ab2026bac6ddb7..6ecf13db641a9319c3ce6d0fb5dcf678cf75ba3b 100644
3+
--- a/dist/NodeHttpServer.js
4+
+++ b/dist/NodeHttpServer.js
5+
@@ -83,7 +83,14 @@ export const make = /*#__PURE__*/Effect.fnUntraced(function* (evaluate, options)
6+
});
7+
const address = server.address();
8+
const wss = yield* Effect.acquireRelease(Effect.sync(() => new NodeWS.WebSocketServer({
9+
- noServer: true
10+
+ noServer: true,
11+
+ // Negotiate permessage-deflate with clients that offer it; clients that
12+
+ // don't still get uncompressed frames on their connection. Context
13+
+ // takeover stays enabled (ws default) so the compression window is shared
14+
+ // across frames; threshold skips compression for tiny frames.
15+
+ perMessageDeflate: {
16+
+ threshold: 512
17+
+ }
18+
})), wss => Effect.callback(resume => {
19+
wss.close(() => resume(Effect.void));
20+
})).pipe(Scope.provide(scope), Effect.cached);
21+
diff --git a/src/NodeHttpServer.ts b/src/NodeHttpServer.ts
22+
index e18cd931f17c26d6ca985a2d1ece9e9d760851d3..758fd795c731200a9f58959e4be93817616b9ea7 100644
23+
--- a/src/NodeHttpServer.ts
24+
+++ b/src/NodeHttpServer.ts
25+
@@ -116,7 +116,18 @@ export const make = Effect.fnUntraced(function*(
26+
const address = server.address()!
27+
28+
const wss = yield* Effect.acquireRelease(
29+
- Effect.sync(() => new NodeWS.WebSocketServer({ noServer: true })),
30+
+ Effect.sync(() =>
31+
+ new NodeWS.WebSocketServer({
32+
+ noServer: true,
33+
+ // Negotiate permessage-deflate with clients that offer it; clients that
34+
+ // don't still get uncompressed frames on their connection. Context
35+
+ // takeover stays enabled (ws default) so the compression window is
36+
+ // shared across frames; threshold skips compression for tiny frames.
37+
+ perMessageDeflate: {
38+
+ threshold: 512
39+
+ }
40+
+ })
41+
+ ),
42+
(wss) =>
43+
Effect.callback<void>((resume) => {
44+
wss.close(() => resume(Effect.void))

0 commit comments

Comments
 (0)