as

Projects / Hotwire

Hotwire

A latency-aware load balancer control plane in Go. I built it to see the control loop without standing up Envoy. The data plane is fake on purpose. The same repo holds the pieces around it: simhttp, plate, cloak, relay, and ipv1.

Why

A real load balancer hides the interesting part. Envoy already has a control plane, clusters, and a dozen ways to weight endpoints. I wanted the loop in one process I could read: backends report how they feel, something scores them, proxies shift traffic, and I can watch the weights move when I make a backend slow.

go run ./cmd/hotwire demo starts the control plane, a few simhttp backends with different latency profiles, and a proxy on one port. Nothing here is meant to front production traffic. Each folder in sideprojects is its own module.

Architecture

simhttp is a library. It serves HTTP with a configured latency, jitter, and error rate, and it snapshots p50, p99, error rate, inflight, and RPS. A hotwire backend is a thin wrapper that streams those snapshots to the control plane. The proxy never parses them. It only receives weights, and it already knows each backend’s URL from its own flags.

The control loop. Addresses stay on the proxy. The control plane only publishes weights.
data plane simhttp backends Configurable latency, jitter, error rate. Snapshots on a timer.

ReportMetrics, bidirectional gRPC. Telemetry in, weight ack out.

control registry + scorer Latest report per backend. Silent for more than 3 seconds means weight 0.

SubscribeWeights, server stream. ListBackends is a unary snapshot.

proxy local route table --route alpha=http://127.0.0.1:18081. Picks a backend in proportion to its weight.

Ordinary reverse proxy. All weights at 0 returns 503.

The score of a live backend is (1 / max(p99ms, 1)) × (1 − min(error rate, 1)). Its weight is that score divided by the sum across backends that are not stale. p50, inflight, and RPS are stored on the report and ignored by the formula. Stale is a cliff: no report for more than 3 seconds, and the weight is zero. There is no decay.

Diagrams

A solid outline is the path in the repo. A dashed outline is the alternative that shape is refusing.

Backends stream snapshots. The control plane keeps the latest report and publishes weights. The proxy already knows each URL from --route and never parses the metrics.

flowchart TD
  sim["simhttp backends"]
  reg["Registry and scorer"]
  proxy["Proxy route table"]
  sim -->|"ReportMetrics, bidirectional gRPC"| reg
  reg -->|"SubscribeWeights, server stream"| proxy
  proxy -->|"weight 0 answers 503"| caller[Caller]
  class sim,reg,proxy,caller chosen
          

The backend already measured the request, so the control plane does not send its own probes. A slow report lowers that backend’s share. It does not drop the backend on the first bad window. The formula reads p99 and error rate. Inflight stays on the report and out of the score.

flowchart TD
  signal{Who measures latency}
  signal -->|"the backend already has the snapshot"| push["Backends push metrics"]
  signal -->|"probe time mixed with user time"| pull["Control plane probes"]
  pick{How traffic moves}
  pick -->|"a slow report turns the share down"| weights["Proportional weights"]
  pick -->|"the first bad window empties it"| winner["A single winner"]
  score{What the formula reads}
  score -->|"tail and failures"| p99["p99 and error rate"]
  score -->|"a full backend is punished twice"| inflight["Inflight in the score"]
  class push,weights,p99 chosen
  class pull,winner,inflight rejected
          

Why this shape

Backends push metrics. The control plane does not probe them.

The backend already has the latency snapshot, so the control plane does not need to send its own probes and then confuse probe latency with user latency. The cost is trust. A backend that stops reporting looks healthy until the 3 second cutoff, and a backend can advertise a low p99 while its HTTP handler is wedged. The proxy does not health-check the HTTP path. The metric stream is the only signal.

Weights, not a single winner

The proxy builds a cumulative distribution over the routes it was given and draws one. A slow report turns traffic down. It does not empty the backend on the first bad window. The cost is that a bad backend keeps a share until its p99 or error rate actually moves the score. A backend at weight 0 is skipped. A backend the proxy has no --route for is stored on the control plane and never picked. Discovery is out of scope. The control plane does not know Docker DNS or a cloud inventory.

p99 and errors only

Inflight would punish a backend that is slow because it is full, which the tail already shows, late. Leaving inflight out of the formula means a saturated backend keeps its weight until p99 moves. The field is still on the report, so a later formula can use it without a new RPC. The scorer does not read it today.

simhttp is a library, not a demo flag

The fake server is tested without gRPC. Hotwire backends wrap it. That keeps “what latency did we configure” separate from “did the stream stay up.” minstd, in the same repo, is the pile of stdlib shims I pulled out while writing that HTTP stack. It is not a second standard library.

Failure modes

Two different deaths, two different behaviors

If reports stop and the weight stream stays up, every backend goes stale and the proxy answers 503. The control plane is authoritative. A partition that drops metrics takes the route down even when the backends would still serve HTTP. If the weight stream itself errors, the proxy’s read loop returns and does not dial again. The last weight map stays in memory. A restarted control plane does not move traffic until the proxy process restarts. Loss of metrics fails closed. Loss of the stream freezes the last decision.

The stale cutoff is a step function

A backend reporting every 2.9 seconds keeps its full share, even if those snapshots are old relative to the traffic. At 3.1 seconds it is gone. There is no hysteresis and no smoothing. The score uses the latest p99, so jitter moves weights on every report. Damping that would hide the loop the demo is for watching.

A lying or half-dead backend still gets requests

The proxy will keep sending to a backend whose stream says p99 is 15ms. It does not time the HTTP call it just proxied, and it does not feed that time back. The control loop cannot correct a backend that reports fiction. That is acceptable in a lab where I set the latency flags myself. It is the first thing that would have to change if the backends were not simhttp.

Plate, the VPS control plane

plate is the other control plane in the repo. plate serve on a host, then create machines through the HTTP API, the CLI, or the panel at /panel. A provider interface has two implementations. Docker is for local dev. Proxmox clones a cloud-init template, resizes CPU, RAM, and disk, injects SSH keys, and starts a real KVM VM.

One process owns the API, the IP pool, and the provider. State is files under .plate/.
clients CLI, panel, HTTP /v1/vms, accounts, billing, snapshots, firewall, reverse DNS.

Bearer token once any token exists. Creating an account and listing plans stay open.

plate control + ippool Assigns a public IPv4 on create, releases it on delete. CIDR or an explicit list. Skips network and broadcast.

Provider interface

dev Docker iptables DNAT on Linux when both the public and container addresses are known.
kvm Proxmox Template VM, cloud-init, bridge. Gateway and prefix come from env.

State is JSON on disk: VMs, accounts, billing, the pool. There is no second replica and no leader election. Two plate serve processes would both believe they own the pool. That is the boundary. One host, one process. Plans are tiny, small, medium, and large. A new VM opens SSH, HTTP, and HTTPS unless the request says otherwise. Snapshots and a billing file exist so the API looks like a small provider. They are not a metering pipeline.

Cloak, a point-to-point tunnel

cloak is a Layer-3 VPN over UDP. Each side opens a TUN device (cloak0) and sends IP packets to the other, wrapped with NaCl secretbox. The lab addresses are 10.8.0.1/24 and 10.8.0.2/24. Ping the peer’s tunnel address and the packet went through the UDP socket, not a SOCKS hop. It is one server and one client. It is not a mesh, and it does not allocate addresses for a third peer.

The key is a 64-character hex string or a passphrase run through SHA-256. The datagram is version | nonce | secretbox(type + stream + seq + body). VPN mode uses packet and hello messages. TUN comes from golang.zx2c4.com/wireguard/tun, which needs an admin shell and, on Windows, wintun.dll. -socks keeps the same crypto and exits as a userspace proxy when TUN is unavailable. -route can add a CIDR via the tunnel gateway. A full-tunnel 0.0.0.0/0 without also excluding the UDP path to the server sends the tunnel’s own packets into the tunnel.

Relay and ipv1

relay is a gRPC pub/sub CLI. Publish to a named topic, watch a server stream, or join a bidirectional chat room. It is the streaming patterns from hotwire, without the scoring. ipv1 is a flat-address UDP toy: names like 1 and host-3, resolved through a JSON file. Plate’s ippool still owns real IPv4. ipv1 does not.

The rest of this repo has its own notes: simhttp, plate, cloak, relay, and ipv1. astack is the other Go program, a CLI orchestrator.