Inside the polyserve Go host: fasthttp, unix-socket slots, and zero-drop hot swaps
How one fasthttp process serves many compiled Go apps with microsecond proxy overhead, atomic generation swaps with a 5s drain, and Postgres addons via injected DATABASE_URL.
polyserve serves many apps ("spaces") from one long-lived host process per language. This post is about the Go host: how one fasthttp front serves hundreds of compiled Go binaries with microsecond-level proxy overhead, and how deploys swap a running binary without dropping a request. If you have not read the announcement, start there.
Architecture: one fasthttp front, N binaries
The host is a single fasthttp server (default port 8440). Your app is a normal compiled Go binary — net/http, zero polyserve dependencies — that the host runs behind a unix socket. Requests arrive at the host as /s/<owner>/<name>/<rest> (or by Host header for mapped custom domains, resolved from the host's spaces.json state file) and are proxied to the space's socket with path /<rest>.
Why unix sockets? No TCP handshake, no port allocation, kernel-local transfer — the proxy hop costs microseconds, not the milliseconds of a network hop to a sidecar or gateway. For spaces that want to eliminate even that, the host can load a .so plugin fully in-process instead.
Deploying is one curl
Straight from the README — build for linux, POST the binary:
GOOS=linux go build -o hello ./examples/go-hello
curl -X POST localhost:8440/_polyserve/deploy -F space=lee101/hello -F binary=@hello
curl localhost:8440/s/lee101/hello/The deploy endpoint responds with the new generation:
{"ok":true,"space":"lee101/hello","generation":N}If POLYSERVE_ADMIN_TOKEN is set, control endpoints require Authorization: Bearer <token>.
Hot swap: generation slots and a 5s drain
Each deploy of a space is a generation. On POST /_polyserve/deploy the host:
- Saves the uploaded executable to
slots/<owner>/<name>/bin-<N> - Starts the new process on a fresh unix socket
- Atomically switches traffic to the new socket
- Drains the old process for 5 seconds, then kills it
In-flight requests on the old generation finish during the drain; new requests already go to the new one. Zero dropped requests, no load balancer choreography, no rolling-update machinery — it is a pointer swap inside one process.
Scale-to-zero and cold starts
A space idle past POLYSERVE_IDLE_TTL (default 900s) has its process stopped. The binary and its state entry stay on disk, so the next request cold-starts it transparently — the budget for a Go binary is under 100ms, which is why scale-to-zero is on by default rather than an enterprise checkbox.
Metering for free
Because the host proxies every request, it meters every request. GET /_polyserve/usage returns monotonic per-space counters:
{"spaces":{"lee101/hello":{"requests":123,"busy_ms":4567,"warm_s":890,"loaded":true}}}busy_ms is cumulative wall time with at least one in-flight request (billable compute); warm_s is cumulative seconds loaded in memory (billable residency). The control plane diffs between scrapes, and the host appends usage once per minute to usage.jsonl so billing survives crashes.
Postgres addons via injected DATABASE_URL
Spaces receive per-space environment through the deploy call: env is a repeatable KEY=VALUE field. On app.nz the control plane provisions the Postgres addon and passes credentials at deploy time; the examples/go-postgres example is an ordinary Go app reading DATABASE_URL. Locally:
GOOS=linux go build -o pgapp ./examples/go-postgres
curl -X POST localhost:8440/_polyserve/deploy \
-F space=lee101/pgapp \
-F binary=@pgapp \
-F env=DATABASE_URL=postgres://user:pass@host:5432/dbRemoving a space is symmetric: DELETE /_polyserve/spaces/<owner>/<name> unloads and removes it.
Where this fits
The same protocol powers the Python and Bun hosts — the Python host is what makes in-process ONNX serving work — and one tiny autoscaler loop adds or removes Hetzner and RunPod workers from aggregate usage. Full per-language quickstarts are in the deploy docs. Source: github.com/lee101/polyserve, mirrored at app.nz/lee101/polyserve.