Files
archie/internal/infrastructure/servicediscovery/nats/keys.go
T
sam 0bc65b680d feat(servicediscovery): scaffold ServiceRegistry contract and NATS KV implementation
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.
2026-09-05 03:11:43 +10:00

32 lines
1.1 KiB
Go

package nats
import "strings"
// keySeparator joins a service and an instance ID in a registry key. It is a
// dot rather than the research sketch's slash so that a per-service watch
// filter ("curator.*") matches "curator.inst-1" as two NATS subject tokens;
// a slash would make the whole key one opaque token no wildcard could match.
// See the package doc comment for the full reasoning.
const keySeparator = "."
// endpointKey is the heartbeat bucket key for one live instance.
func endpointKey(service, id string) string {
return service + keySeparator + id
}
// endpointKeyPrefix is the watch/list filter matching every instance of a
// service. It is a single-token wildcard over the instance ID.
func endpointKeyPrefix(service string) string {
return service + keySeparator + "*"
}
// idFromEndpointKey returns the instance ID carried by a registry key, or "" if
// the key does not belong to the given service (or carries an empty ID).
func idFromEndpointKey(service, key string) string {
prefix := service + keySeparator
if !strings.HasPrefix(key, prefix) {
return ""
}
return strings.TrimPrefix(key, prefix)
}