Add a broker-neutral service registry contract (Endpoint, EventKind, Event, ServiceRegistry, ErrNotInstalled) and a NATS JetStream KV implementation, plus the registration side a service uses to announce itself. The contract's governing semantic -- NotInstalled is not Down -- is modeled by two buckets: a durable installed marker (never expires) distinct from expiring heartbeat keys. Instances heartbeat their key on an interval; a stopped process leaves via TTL expiry. Register writes the marker once then heartbeats; Resolve returns ErrNotInstalled only when the marker is absent, and an empty roster for installed-but-down. Watch dedupes heartbeat refreshes so it emits membership changes, not liveness pings. Unwired: no daemon/bootstrap path consumes it yet. Deliberately uses a dot key separator instead of the research sketch's slash so per-service watch wildcards match.
30 lines
860 B
Go
30 lines
860 B
Go
package nats
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/samcharles93/archie-core/internal/servicediscovery"
|
|
)
|
|
|
|
// endpointJSON is the on-disk (KV value) encoding of a registered endpoint. It
|
|
// is deliberately the contract type so a consumer reading raw records gets the
|
|
// same shape ServiceRegistry hands back.
|
|
func encodeEndpoint(ep servicediscovery.Endpoint) ([]byte, error) {
|
|
b, err := json.Marshal(ep)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode endpoint %s/%s: %w", ep.Service, ep.ID, err)
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// decodeEndpoint parses a KV value back into an Endpoint. It reports false for
|
|
// a value it cannot parse.
|
|
func decodeEndpoint(value []byte) (servicediscovery.Endpoint, bool) {
|
|
var ep servicediscovery.Endpoint
|
|
if err := json.Unmarshal(value, &ep); err != nil {
|
|
return servicediscovery.Endpoint{}, false
|
|
}
|
|
return ep, true
|
|
}
|