Repository navigation
feat(manager): drained reload (reload(runner, { drain })) - #92
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 37 minutes. View limit details
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
commit: |
… counting - close the previous runner at the drain timeout instead of a graceful shutdown that overran it; default 60s, `Infinity` for no limit - read and count the active runner in the same tick in `fetch()`/`upgrade()` - keep lazy invalidation per runner, so a draining runner's module reload can't clear the new runner's - re-wrap tracked bodies with srvx `FastResponse` - Bun/Deno bridge test goes through `RunnerManager.wsSrvxPlugin()`
- keep srvx `FastResponse` headers (`content-type`/`content-length`) by reading it like srvx's Node adapter, instead of via `.body` - stop draining a runner that is attached again; `reload(activeRunner)` keeps it; wrap a runner's `close()` once - `NaN` drain timeout uses the default, negative values are 0 - log a drained runner's synchronously throwing `shutdown()`/`close()`
…allbacks crossws 0.4.16 (h3js/crossws#219) reports proxied connections with `onUpstreamConnect`/`onUpstreamClose`, exactly once each, so the bridge no longer wraps the proxy's `open`/`close`/`error` hooks. The target is synchronous when the runner is ready (dialed in the same tick), and the manager refuses a dial to a runner swapped out meanwhile.
Pass srvx's `FastResponse` as httpxy's `proxyFetch()` `Response` option (httpxy 0.5.6), so a proxied response keeps the upstream Node.js stream and srvx pipes it without web stream conversions. Also bumps srvx to 1.0.7 (keeps the derived content-length when `FastResponse` materializes).
From the final review: - runner `fetch()` passes a `Request`'s signal to `proxyFetch()` (httpxy doesn't forward it), so a client disconnect aborts the worker request; it resolves with an empty 499 response when that signal aborted it - a HEAD request settles once its response arrives - Node.js stream bodies settle with `stream.finished()` (no `autoDestroy` needed) - the drain timer is `unref()`'d - tests: client disconnects, proxied 204/304/HEAD/set-cookie, an upgrade waiting for readiness, and the bridge refusing a swapped-out runner
Move eve's vendored env-runner to 0.3.10, the version the Nitro RC uses. Its worker now closes the server before running `ipc.onClose`, and `shutdown()` waits for the worker's exit before terminating it, so eve no longer patches the worker or manages worker exit itself. The development runner's `close()` now terminates right away and `shutdown()` is the graceful path; the drained dev server retires workers with `shutdown()` and drops client connections before shutting workers down on close, so an open stream doesn't hold up exit. Draining stays in eve for now; a TODO tracks moving it to env-runner's `RunnerManager` drained reload (unjs/env-runner#92). Signed-off-by: pi0x <[email protected]>
Resolves #91.
RunnerManager.reload(next)closes the previous runner at the swap, so in-flight requests, streamed responses and WebSockets end withECONNRESET. This adds a drain mode:New requests and upgrades go to the new runner right away. The previous runner keeps serving what it already accepted and is retired with
shutdown()(soipc.onCloseruns) once that settles. At the timeout (default 60s, also forNaN; negative values are 0), it is closed with whatever is still open. A graceful shutdown there would wait on those same exchanges and overrun the limit.reload()resolves once the new runner is attached and doesn't wait for the drain.How it works
RunnerManagercounts exchanges per runner:FastResponse. A runner's ownFastResponseon Node (e.g. fromself) is read with srvx's internal_toNodeResponse(), like its Node adapter does, so thecontent-type/content-lengthsrvx derives for string/byte bodies are kept (reading.bodywould turn it into a nativeResponseand drop them).HEADrequest settles once its response arrives (its body may never be read). Node.js stream bodies settle withstream.finished().fetch()now passes aRequest's signal toproxyFetch()(httpxy doesn't forward it), so a client that disconnects before the response aborts the worker request instead of holding the previous runner until the worker answers. When that signal aborted it,fetch()resolves with an empty499response instead of rejecting, which srvx would log as a fetch handler error. Deno'snode:httpignores the signal, so there the request still runs to completion, as before.fetch()passes srvxFastResponseas httpxy'sResponseoption (httpxy 0.5.6), so worker bodies stay Node.js streams.manager.upgrade()): until the client socket closeswsSrvxPlugin()): new optionaltrackcallback oncreateRunnerWSProxyPlugin(), backed by crossws'sonUpstreamConnect/onUpstreamCloselifecycle callbacks (exactly once each;crosswsbumped to^0.4.16, feat(proxy): lifecycle callbacks for proxied connections h3js/crossws#219). The target is synchronous when the runner is ready, so the dial happens in the tick the address is read; a dial resolved mid-reload to a runner swapped out meanwhile is refused (1011).close()/shutdown()of the manager also retire draining runners.close()force-closes one already shutting down; a secondshutdown()waits for the running one (no doubleipc.onClose).reload(activeRunner)keeps it. A runner'sclose()is wrapped once, so re-attaching doesn't report its close twice.unref()'d.shutdown()/close()that throws synchronously is logged, not thrown out of a socketcloselistener or timer.reloadModule()no longer clears the new runner's invalidation, and isn't shared with fetches to the new runner.Trade-offs
url/redirectedand Response subclasses. A quick local benchmark (srvx → manager → node-worker) showed no difference frommainbeyond noise withFastResponse; with a nativeResponsewrapper, 1 MiB streams looked a few percent slower.selfrunner's nativeResponsewith a string body is sent chunked through the manager (the runtime would otherwise derivecontent-length). Worker runners are unaffected, since their proxied responses carry the upstreamcontent-length.shutdown()never settles isn't force-closed at the drain timeout (the timer is cleared once the shutdown starts). Built-in runners bound their own shutdown.503, while the old runner is still up); requests sent torunner.fetch()directly aren't counted; no CLI flag.Tests
test/drain.test.ts: streamed response survives a drained reload (and is cut without drain), idle runner retires right away, canceled body, timeout closes on time (upper bound), 60s default andInfinity(fake timers),NaN/negative timeouts, manager close/shutdown during a drain, self-closing runner (not retired again), re-attaching a draining runner,reload(activeRunner), a throwingshutdown(), a request racing the reload, per-runner invalidation,FastResponseheaders (GET/HEAD) and stream bodies (web and Node.js streams), WebSocket survives viawsSrvxPlugin(),EnvServerdrainoption. Through a srvx server with a real worker: a client disconnecting before the response and mid-stream,204/304/HEAD/multipleset-cookie, and aHEADbody never read. Also an upgrade waiting for readiness, and the bridge's refusal of a swapped-out runner (trackthrows).test/websocket.test.ts: on Bun/Deno hosts, a drained reload through a realRunnerManager.wsSrvxPlugin()retires the previous runner only once the bridged WebSocket closes, also for a WebSocket bridged while the runner was still starting (async target).Upstream follow-ups
FastResponsedrops derivedcontent-lengthonce it becomes a nativeResponseh3js/srvx#332:FastResponsekeeps derivedcontent-lengthas a nativeResponse(done in 1.0.7, required here).trackNodeFastResponse()stays, so worker Node.js stream bodies aren't converted to web streamsNodeResponse._toNodeResponse()public (or expose a helper) h3js/srvx#333: public_toNodeResponse()proxyFetch(): option to keep the upstream Node.js stream as the response body httpxy#185: keep the upstream Node.js stream as theproxyFetch()body (done in 0.5.6 via theResponseoption: on Node, runnerfetch()passesFastResponse)createWebSocketProxy(): lifecycle callbacks for proxied connections h3js/crossws#218: lifecycle callbacks (done in 0.4.16, used here)Docs: README ("Drained reload"),
.agents/ARCHITECTURE.md,AGENTS.md.🤖 Generated with AI assistant
Summary by CodeRabbit