moonsd service discovery

The instrument

A register, running

Everything below is the library, compiled for this browser: the registry in this process with a clock, hold keeping one endpoint alive, View following both services, and a sweeper ending leases whose time has run out. Nothing on the page is a mock-up of a registry; the page is reading one.

the register rev 0 · ttl 8.0s
greeter 0 · checkout 0
watcher events
01

What you are watching

Two ways to hold a lease

A registration is only as good as its renewal. Hold an instance puts the whole loop inside the library: hold registers, renews at a third of the lease, and deregisters when the task is cancelled. Lease one by hand registers once and leaves the renewal to this page, which is what a caller with its own timer does — and what makes a lapse visible: stop pressing ↻ and the bar empties, the sweeper drops the lease, and the watcher reports the endpoint leaving.

On the boardIn the libraryWhen it ends
holdhold(registry, service, endpoint, ttl) in its own taskthe task is cancelled, which deregisters first
leaseregister(service, endpoint, ttl), then renew(lease)nobody renews it, and the clock passes the lease

The revision in the header is the number the watcher last reported. It grows with each change, which is how the page knows something happened without diffing the list.

02

Under the page

What was compiled

The engine is src/examples/browser in the repository, built to JavaScript by the documentation workflow and served from engine/. It exports seven functions — ready, join, lease_manually, renew, withdraw, reset, state — and the page calls them; the registry, the leases and the watchers are all MoonBit.

The clock is the monotonic clock of the runtime, the sweep runs every 200 ms, and the lease is 8 s so a lapse fits on a screen. Nothing here is a special case inside the library: the same calls on a Redis, etcd or Consul registry behave the same way, only the latency of a change differs.