State, heavy jobs and long requests
Keeping a wasmhost service's state across a reload, running a job with a big memory peak, and giving long jobs their own worker.
The first page covered a service that answers from the files it’s given. Real ones tend to build something first (a parsed design, a laid-out board, an index), and that’s where three problems turn up. Building it can take seconds, and a reload throws it away. Building it can need far more memory than the result, and wasm memory never shrinks, so the worker keeps the peak forever. And a request that runs for seconds holds the worker’s one thread, so a quick question behind it waits.
wasmhost has one tool for each: a cache for the state, a throwaway worker for the peak, and lanes for the long jobs. The demo runs all three, using exercise/workerstate’s service, which builds a 4 MB result through a 64 MB scratch buffer:
Reload the demo (the button, or the page) and the first step changes. The second time, nothing ingests, because the serving worker’s warm finds the result in the cache. “Clear the cache and reload” puts it back to a first visit.
A cache for state the service can rebuild ¶
wasmhost.Cache is a key-to-blob store with two methods, Get and Put. Keys name the inputs, not the result. A service works out the key from a request before it has anything, and the same inputs always find the same blob. wasmhost.CacheKey(parts...) hashes the parts into a key. Put a version string first, so a build that changes what it stores stops finding what an older build wrote.
In the browser it’s wasmhost.BrowserCache(), which keeps blobs in the page origin’s private file system (OPFS, under wasmhost/<ns>/), so they outlive a reload. Natively there’s DirCache(dir) and MemCache, so the same service code runs in tests. The demo’s service opens its state like this (trimmed; the real one also reports cache errors and copes with no cache):
func (s *Service) open(w http.ResponseWriter, r *http.Request) {
req, _ := readIngest(w, r)
key := wasmhost.CacheKey([]byte("workerstate-v1"), []byte(strconv.Itoa(req.PeakMB)), []byte(strconv.Itoa(req.ResultMB)))
if b, err := s.Cache.Get(r.Context(), key); err == nil {
writeJSON(w, s.keep(b, true)) // restored
return
}
st := s.build(req) // the expensive part
_ = s.Cache.Put(r.Context(), key, s.current())
writeJSON(w, st)
}
It’s a cache, not a datastore. Anything in it has to be rebuildable from its inputs. A miss (ErrMiss), a browser without OPFS (ErrNoCache) and the user clearing site data all just mean the service builds it again, which is mostly a matter of waiting. Data a user would lose belongs on a server.
Get and Put wait on the browser, so call them from a handler (which runs on its own goroutine and is allowed to wait), never from inside a js.FuncOf callback.
A throwaway worker for a big peak ¶
A worker that has needed 64 MB once holds 64 MB until it ends, even if all it kept is 4 MB, because wasm linear memory only grows. The way to give memory back is to end the worker. oneShotFetch(opts) is a fetch that starts a fresh worker for each request and terminates it once the response is in:
import { oneShotFetch, startLane } from "@panyam/tsappkit/wasmhost";
const opts = { worker: "worker.js", wasm: "app.wasm", exec: "wasm_exec.js", ns: "state" };
// body is the JSON inputs, and serve is the long-lived worker's lane (see the next section).
// The ingest runs in a worker of its own, which Puts the result in the cache and ends.
await (await oneShotFetch(opts)(location.origin + "/open", { method: "POST", body })).json();
// The long-lived worker opens the same inputs and finds them in the cache.
await (await serve.fetch(location.origin + "/open", { method: "POST", body })).json();
Every worker for the same ns sees the same BrowserCache, and that shared cache makes the handoff work. The first step of the demo does exactly this, and it checks that the serving worker ends up holding well under the 64 MB peak.
Each call pays for a worker start and a wasm instantiate, a few hundred ms, so it’s for jobs that take seconds, not for every request. The worker ends when the response body has been read to the end, failed or been cancelled, so read or cancel the body, or the worker stays up.
Lanes for long jobs ¶
A Go handler that loops for three seconds without yielding has the worker to itself for three seconds. The page doesn’t freeze, since it’s a worker, but every other request to that worker queues behind it. startLane(opts, options) gives a kind of request its own replaceable worker, and an app usually wants two: a “serve” lane for quick queries and a “jobs” lane for long jobs.
const serve = startLane(opts, {
// A new serving worker loads the state from the cache, and never ingests.
warm: (f) => f(location.origin + "/restore", { method: "POST", body }),
maxMemoryBytes: 48 << 20,
});
const jobs = startLane(opts);
const client = createClient(Service, createConnectTransport({ baseUrl: location.origin, fetch: serve.fetch }));
Each lane’s fetch goes to that lane’s current worker. warm runs on every new worker before the lane hands it a request, which is where a service loads its state from the cache, so a replacement answers like the worker it replaced.
Aborting a request on a lane (init.signal) terminates the lane’s worker and starts a fresh, warmed one. That sounds drastic, but it’s the only way to stop a Go loop that never yields, because the worker can’t read the “cancel” message until the loop lets go of the thread. Other requests still waiting on the old worker are rejected too, so keep a lane to one kind of request.
maxMemoryBytes covers the slower problem, a worker that creeps up through many requests that each allocate and drop some memory. The lane checks its worker’s memory whenever it goes idle, and past the limit replaces it with a warmed one. It never interrupts a request. Size it above what the warmed state needs, or the lane restarts after every request. The demo’s last step pushes the serving worker past its 48 MB limit on purpose and watches it come back.
Wasm memory is the worker’s peak, though, so it can’t tell a worker full of garbage from one full of state. A replacement restores the same state, so replacing a worker whose state fills its memory gives nothing back and costs a warm. restartIf decides from workerStats, which adds the Go heap measured right after a collection:
const serve = startLane(opts, {
warm,
maxMemoryBytes: 96 << 20,
// Past 96 MB, restart only if the live heap is under half of it.
restartIf: ({ memoryBytes, heap }) => heap !== undefined && heap.heapAlloc * 2 < memoryBytes,
});
With both set, the lane restarts when the worker is past maxMemoryBytes and restartIf agrees, and it only asks for the collection once the worker is past the limit. That collection takes a while on a big heap (agni measured 187 ms on a 377 MB live heap), and a request arriving meanwhile waits for it, so set maxMemoryBytes whenever you set restartIf; on its own, restartIf pays a collection every time the lane goes idle. workerStats(worker, { gc: true }) gives the same figures on demand. heap.heapAlloc is the live heap, and sys is what Go has taken from the wasm memory.
Tuning the collector, and why it rarely helps ¶
Go’s collector lets the heap grow to twice what’s live before it collects (GOGC=100), so a request that allocates a lot and keeps a little grows the wasm memory to about twice the live heap, and that memory stays. The obvious fix is to make the collector work harder, with debug.SetGCPercent (a lower GOGC) or debug.SetMemoryLimit (GOMEMLIMIT), set in the worker’s main before serving. Natively that trades memory for time the way you’d expect. In wasm it mostly doesn’t, at least on the one large workload anyone has measured.
agni swept both settings on its largest design (a restore, then a check run over a live heap of about 560 MB) for agni#988:
| Setting | Native peak | Wasm peak | Wasm check run |
|---|---|---|---|
| GOGC 100 (the default) | 1171 MB | 1364 MB | 2.1 s |
| GOGC 50 | 886 MB | 1020 MB | 2.65 s |
| GOGC 25 | 738 MB | 1506 MB | 4.3 s |
| GOMEMLIMIT 700 / 800 / 900 MiB | 1738 / 1618 / 1325 MB | 4.1 / 3.0 / 3.1 s |
Below GOGC 50, and with every memory limit tried, the wasm peak came out above the default, though the native one fell. And in Chromium, GOGC 50 saved 130 MB on a first visit’s worker and nothing on a reloaded page’s, for 14% more check time, so agni left the collector at its default. As far as we can tell the native intuition doesn’t carry over, though nobody has pinned down why.
So measure before setting either one, in the browser and on your own workload, with workerStats before and after; Node’s figures didn’t predict Chromium’s here. The changes that did pay off were structural: keeping the peak out of the long-lived worker (oneShotFetch), replacing a worker only when its live heap is well under its memory (restartIf), and not holding what a worker doesn’t read. Neither setting helps when one allocation is itself the peak, like a single 100 MB buffer.
Streaming and cancelling ¶
A handler that writes and calls Flush (http.Flusher) streams to the page as it runs. workerFetch’s Response resolves at the first flush, and its body delivers each flush as it happens. That works even from a loop that never yields, because sending a chunk is a synchronous call from Go into JS. The demo’s “Run a 3 s job” moves its progress bar that way, from NDJSON lines the job flushes every 250 ms.
Aborting cancels the handler’s r.Context(), which a handler that waits on JS or yields will see. A loop that never yields won’t, because the cancel can’t arrive until it does, which is why a lane ends the worker instead. Data can stream out of a busy worker, then, but nothing gets in until the handler yields or returns.
We tripped over one more thing while building the demo. Once a body is streaming, Chrome’s text() and json() report an abort as TypeError: Failed to fetch rather than an AbortError, so tell an abort from a failure by signal.aborted, not by the error.