perf(relay): defer KV state write, add health endpoint, harden POST handler#25
Open
iceteaSA wants to merge 1 commit into
Open
perf(relay): defer KV state write, add health endpoint, harden POST handler#25iceteaSA wants to merge 1 commit into
iceteaSA wants to merge 1 commit into
Conversation
…andler - Defer KV state write via ctx.waitUntil instead of awaiting inline. Response streaming starts before state is persisted, reducing P50 latency by the KV write time (~4ms). - Increase WS heartbeat from 5s to 15s to reduce keepalive overhead. - GET / returns JSON health check with available transports. - Wrap POST handler in try/catch returning structured 502 JSON instead of raw Cloudflare error pages on unexpected failures.
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.
Summary
Four improvements to the relay worker script:
1. Deferred KV state write (~4ms P50 improvement)
prepareUpstreampreviouslyawaited the KVwriteStatecall before returning the body. Now the write is fire-and-forget (.catch(() => {})) and deferred viactx.waitUntil. Response streaming starts immediately — the state is persisted in the background.2. Heartbeat interval 5s → 15s
Reduces WebSocket keepalive overhead by 3x. The CF Workers runtime keeps connections alive independently; the heartbeat only needs to prevent idle timeouts on intermediate proxies, which typically have 30-60s thresholds.
3. GET
/health endpointReturns JSON with relay status and available transports instead of plain "ok":
{"status":"ok","transports":["http","websocket"]}4. POST handler try/catch → 502
Wraps the HTTP POST handler in a try/catch that returns structured JSON
502instead of raw Cloudflare error pages on unexpected failures. Clients can parse the error and fall back to direct API calls.Testing
All 214 tests pass. Changes are in the embedded
WORKER_SCRIPTtemplate string — no client-side behavior changes.