Service discovery for MoonBit: the contract backends implement, with one in-memory registry and a conformance suite.
async fn greeter(registry : @memory.Registry) -> Unit {
@async.with_task_group(group => {
// A service keeps itself registered for as long as this task runs, and leaves on the way out.
group.spawn_bg(() => @moonsd.hold(registry, "greeter", @moonsd.Endpoint::new("10.0.0.1", 12000)))
// A caller follows the service in one task, and reads the list on the request path without waiting.
let view = @moonsd.View::new()
group.spawn_bg(() => view.follow(registry, "greeter"))
@async.sleep(50)
println(view.endpoints().map(e => e.address()).join(", "))
})
}moon add moonbitstack/moonsd| Trait | Methods | What it promises |
|---|---|---|
| Registrar | register, renew, deregister | An endpoint is known for ttl milliseconds from its last renewal. renew raises Expired once the backend has let it go; deregister is harmless on a lease already gone. |
| Discovery | resolve, watch | resolve answers once, and an unknown service has no endpoints. |
| Watcher | next, close | The first next gives the service as it stands; each later one waits for a change. |
| Package | Contents |
|---|---|
| moonsd | Endpoint, Lease, Update, Refused, the three traits, hold, View |
| moonsd/memory | Registry, the contract in this process: leases when given a clock, revisions, snapshot and restore |
| moonsd/conformance | check, every behaviour the contract promises, run against any backend |
| Module | Directory | sd package |
|---|---|---|
| moonbitstack/moonredis | drivers/redis | Expiring keys, watched by reading again |
| moonbitstack/moonetcd | drivers/etcd | Keys on leases, watched by revision |
| moonbitstack/moonconsul | drivers/consul | Services with TTL checks, watched by blocking queries |
async test "my backend keeps the contract" {
let backend = connect()
@conformance.check(backend, backend)
}pub struct View {
// private fields
}let retry : Intlet ttl : IntInstall
Download zipService discovery for MoonBit: the contract backends implement, with one in-memory registry and a conformance suite.