Skip to content

perf(relay): defer KV state write, add health endpoint, harden POST handler#25

Open
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:perf/relay-worker-improvements
Open

perf(relay): defer KV state write, add health endpoint, harden POST handler#25
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:perf/relay-worker-improvements

Conversation

@iceteaSA
Copy link
Copy Markdown

Summary

Four improvements to the relay worker script:

1. Deferred KV state write (~4ms P50 improvement)

prepareUpstream previously awaited the KV writeState call before returning the body. Now the write is fire-and-forget (.catch(() => {})) and deferred via ctx.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 endpoint

Returns 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 502 instead 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_SCRIPT template string — no client-side behavior changes.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant