Repository navigation
Send already-encoded tunnel response bodies without permessage-deflate - #8223
Merged
Merged
Conversation
The bb Connect tunnel negotiates permessage-deflate with the gate, and TunnelSession sent every frame with the ws default compress: true. The bb server already encodes its compressible responses with brotli or gzip, so the tunnel client deflated those body chunks again for no size gain. For 8 MB of brotli body this cost 230-280 ms of client CPU and made the wire 3.6 KB larger. Send body-chunk frames with compress: false when the origin response has a Content-Encoding. Bodies without one, and all other frames, keep the default compression.
Body frames are deflated only when the origin response has no content encoding (or identity) and a content type that hono's compress() would compress, so images and other already-compressed media are not deflated for no gain. Frames other than response bodies keep compression. Co-Authored-By: Claude Opus 5.5 <[email protected]>
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
get-bb#8223) ## Human comments ## What was wrong The bb Connect tunnel socket offers permessage-deflate (the `ws` default in `plugins/connect/src/tunnel.ts`), and `TunnelSession.sendRaw` called `tunnel.send(data)` with no options, so `ws` deflated every frame. Each origin response chunk goes through `chunkBody` and `send` as a `body-chunk` frame. The bb server already encodes its compressible responses (brotli or gzip API JSON, hono `compress()`, precompressed assets), so the tunnel client compressed those bodies a second time. I measured the call path on the real `TunnelSession` against a local deflate-capable `ws` server before the change: an 8 MB brotli body cost 229-280 ms of client CPU (median 258 ms), and the wire was 3.6 KB larger than with no deflate. ## What changed - `packages/tunnel-client/src/session.ts`: `executeHttp` decides once per response whether its `body-chunk` frames are deflated. They are deflated only when the response has no `Content-Encoding` (or `identity`) and a `Content-Type` that matches hono's `COMPRESSIBLE_CONTENT_TYPE_REGEX` (the rule the bb server's `compress()` uses). Encoded bodies and incompressible types (images, archives, octet streams) go out with `compress: false`. All other frames keep the `ws` default (`compress: true`). `@bb/tunnel-client` now depends on `hono` for that regex. - `ws` keeps frame order when compressed and uncompressed sends mix, because its sender queues a send while a deflate is in progress. The frame format does not change. - No wire, CLI or protocol changes. No `HOST_DAEMON_PROTOCOL_VERSION` or tunnel `PROTOCOL_VERSION` bump. Deviation from the issue proposal: the issue suggests compressing only `ws-data` frames. This PR keeps compression for bodies with no `Content-Encoding`, because the measurement shows a large gain there: an 8 MB identity JSON body crosses the wire as about 750 KB with deflate. Origins behind a shared port (for example a dev server) can send such bodies. The choice by `Content-Encoding` can only change CPU, not the response the visitor gets. Not in this PR: the Durable Object also deflates its sends to the tunnel (uploads). Stopping that needs `perMessageDeflate: false` on the tunnel, which also stops compression of realtime pushes over the host uplink. That is a trade-off for a maintainer to decide. ## How you verified New test `packages/tunnel-client/test/tunnel-compression.test.ts`: a real `TunnelSession` on a real `ws` client connected to a `WebSocketServer` with `perMessageDeflate: true`, which counts the bytes on its TCP socket. It relays a 1 MB body with `content-encoding: br`, a 1 MB body with no encoding, and a 1 MB origin WebSocket message. - Before the change: `sends an already content-encoded response body uncompressed` fails with `AssertionError: expected 1727 to be greater than 1048576` (the encoded body was deflated). The other 3 tests pass. - After the change: 4 of 4 pass. Benchmark (script outside the commit): the gate and the origin run in a child process, so the measured CPU is only the tunnel client process. 8 MB body, 64 KB origin chunks. | body | metric | before | after | |---|---|---|---| | 8 MB, `content-encoding: br` (7 runs) | client CPU | 229-280 ms (median 258) | 8-49 ms (median 37) | | | wall time to `body-end` | 176-227 ms (median 205) | 9-32 ms (median 20) | | | wire bytes | 8,395,583 | 8,391,961 | | 8 MB JSON, no encoding (5 runs) | client CPU | 61-84 ms (median 71) | 63-101 ms (median 65) | | | wire bytes | 742,866-753,955 | 742,975-753,955 | Commands: - `pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=@bb/tunnel-client --filter=bb-plugin-connect`: 13 of 13 tasks pass (tunnel-client 31 tests, connect plugin 158 tests). - `pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/host-daemon`: pass. - `apps/server/test/security/connect-request-guard.test.ts` (uses `TunnelSession`): pass. Through a real tunnel on bb-connect-staging (the Cloudflare gate negotiates `permessage-deflate`), real `TunnelSession` dialing the gate, curl as the visitor, client CPU per request (medians of 3 interleaved rounds × 5; final row set from the content-type follow-up): | body | CPU before | CPU after | wire before → after | |---|---|---|---| | 8 MB random, `content-encoding: br` | 305–344 ms | 40–144 ms | 8.39 MB → 8.39 MB | | 687 KB brotli JSON | 29–31 ms | 4–13 ms | 655 KB → 687 KB (synthetic; real bb brotli timelines gain 0.0% from deflate) | | 8 MB JSON, no encoding | 64–95 ms | 56–93 ms | 630 KB → 630 KB (still deflated) | | 8 MB `image/png`, no encoding | 189–332 ms | 99–142 ms | 8.39 MB → 8.39 MB | The test also covers an `image/png` body with no encoding (sent uncompressed). Fixes get-bb#8222 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Human comments
What was wrong
The bb Connect tunnel socket offers permessage-deflate (the
wsdefault inplugins/connect/src/tunnel.ts), andTunnelSession.sendRawcalledtunnel.send(data)with no options, sowsdeflated every frame. Each origin response chunk goes throughchunkBodyandsendas abody-chunkframe. The bb server already encodes its compressible responses (brotli or gzip API JSON, honocompress(), precompressed assets), so the tunnel client compressed those bodies a second time. I measured the call path on the realTunnelSessionagainst a local deflate-capablewsserver before the change: an 8 MB brotli body cost 229-280 ms of client CPU (median 258 ms), and the wire was 3.6 KB larger than with no deflate.What changed
packages/tunnel-client/src/session.ts:executeHttpdecides once per response whether itsbody-chunkframes are deflated. They are deflated only when the response has noContent-Encoding(oridentity) and aContent-Typethat matches hono'sCOMPRESSIBLE_CONTENT_TYPE_REGEX(the rule the bb server'scompress()uses). Encoded bodies and incompressible types (images, archives, octet streams) go out withcompress: false. All other frames keep thewsdefault (compress: true).@bb/tunnel-clientnow depends onhonofor that regex.wskeeps frame order when compressed and uncompressed sends mix, because its sender queues a send while a deflate is in progress. The frame format does not change.HOST_DAEMON_PROTOCOL_VERSIONor tunnelPROTOCOL_VERSIONbump.Deviation from the issue proposal: the issue suggests compressing only
ws-dataframes. This PR keeps compression for bodies with noContent-Encoding, because the measurement shows a large gain there: an 8 MB identity JSON body crosses the wire as about 750 KB with deflate. Origins behind a shared port (for example a dev server) can send such bodies. The choice byContent-Encodingcan only change CPU, not the response the visitor gets.Not in this PR: the Durable Object also deflates its sends to the tunnel (uploads). Stopping that needs
perMessageDeflate: falseon the tunnel, which also stops compression of realtime pushes over the host uplink. That is a trade-off for a maintainer to decide.How you verified
New test
packages/tunnel-client/test/tunnel-compression.test.ts: a realTunnelSessionon a realwsclient connected to aWebSocketServerwithperMessageDeflate: true, which counts the bytes on its TCP socket. It relays a 1 MB body withcontent-encoding: br, a 1 MB body with no encoding, and a 1 MB origin WebSocket message.sends an already content-encoded response body uncompressedfails withAssertionError: expected 1727 to be greater than 1048576(the encoded body was deflated). The other 3 tests pass.Benchmark (script outside the commit): the gate and the origin run in a child process, so the measured CPU is only the tunnel client process. 8 MB body, 64 KB origin chunks.
content-encoding: br(7 runs)body-endCommands:
pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=@bb/tunnel-client --filter=bb-plugin-connect: 13 of 13 tasks pass (tunnel-client 31 tests, connect plugin 158 tests).pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/host-daemon: pass.apps/server/test/security/connect-request-guard.test.ts(usesTunnelSession): pass.Through a real tunnel on bb-connect-staging (the Cloudflare gate negotiates
permessage-deflate), realTunnelSessiondialing the gate, curl as the visitor, client CPU per request (medians of 3 interleaved rounds × 5; final row set from the content-type follow-up):content-encoding: brimage/png, no encodingThe test also covers an
image/pngbody with no encoding (sent uncompressed).Fixes #8222