Relay worker micro-optimizations: socket-state caching, /prewarm, DO placement - #946
Conversation
Three additive, independently-deployable improvements to the tunnel relay
Worker. All are backward compatible with deployed desktop and iOS clients:
no auth, close-code, protocol, framing, or buffer-limit changes.
- Cache each socket's attachment and its paired partner in instance memory.
`normalizedAttachment()` deserialized on every frame and `partnerOf()`
rescanned the connection tag on every frame; both now read from a WeakMap.
Hibernation drops instance memory, so both caches fall back to the durable
attachment (or the tag scan) and repopulate themselves after a wake, and a
single write-through helper is now the only writer of an attachment so the
cache cannot drift. The partner entry is only ever a hint, re-checked
against the same predicate the scan uses.
- Add `GET /prewarm/:machineKey`, returning `{ok, control}`. Reaching the
object is the point: it un-hibernates it before a client needs the tunnel.
Inert by construction — no storage, no alarm, no attachment migration, no
signal to the host. Same auth stance as `/connect`. `/health` is answered
by the Worker and never reaches a DO, so it could not serve this.
- Derive a `locationHint` from `request.cf` on `claim` and `/host` only, so a
new machine's object is created near the machine rather than near whichever
request happened to arrive first. `/connect`, `/prewarm`, and `/pipe` never
carry a hint — a travelling phone must not place a machine's object.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Applied /quality dual-review findings. All behavior-preserving:
- partnerOf: the cache-hit path and the tag scan now share isPartnerOf, so
the "re-checked against the same predicate" invariant is structural rather
than coincidental. Drops two guards verified dead — a self-entry cannot be
inserted, and a stale reverse entry self-heals on its own next read.
- handlePrewarm: expressed as .some(), still reading the raw attachment so it
cannot trigger a migration write.
- RelayLocationHint aliases Cloudflare's DurableObjectLocationHint instead of
re-enumerating it, so the union cannot drift.
- parseLongitude: an empty longitude is missing data, not the prime meridian.
Number("") is 0, which read as a real coordinate and picked "me" for Asia.
- Renamed the caches cachedAttachments/cachedPartners: the socket's own
attachment stays authoritative, and the names now say so.
- Tests: establishEpochV2 delegates to openTunnel, so the {t:"open"} shape is
now asserted for every tunnel opened. Adds the empty-longitude case and one
test pinning the socket-identity premise the caches rest on.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (5)
📝 WalkthroughWalkthroughAdds a prewarm endpoint, geography-based Durable Object placement hints, centralized WebSocket attachment caching, safer partner lifecycle handling, and unit/integration coverage for routing, placement, hibernation, pairing, and prewarm behavior. ChangesTunnel relay behavior
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary by CodeRabbit
New Features
Bug Fixes