Documentation
¶
Overview ¶
Package discovery provides mDNS-based sqi-server discovery for sqi-worker.
At startup the worker needs the NATS URL of a running sqi-server. When an explicit URL is configured it is used directly. When no URL is configured and mDNS is enabled, Browse scans the local network for "_sqi._tcp" services — the same service type that sqi-server's internal/discovery package advertises — and returns the first discovered server's NATS URL.
TXT record format advertised by sqi-server:
id=<instance-id> unique per process http=<http-port> REST + WebSocket API port nats=<nats-port> NATS JetStream port for worker connections host=<hostname> advertising host's reported name version=<version> server build version
The browser constructs a "nats://<host>:<nats-port>" URL from the SRV record's hostname and the nats TXT record value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDiscoveryDisabled = errors.New("discovery: mDNS is disabled and no explicit NATS URL is configured")
ErrDiscoveryDisabled is returned by Browse when mDNS is disabled and no explicit NATS URL was provided. Callers should surface a clear message.
var ErrDiscoveryTimeout = errors.New("discovery: mDNS browse timed out with no sqi-server found")
ErrDiscoveryTimeout is returned by Browse when the timeout expires with no server found and mDNS is enabled.
Functions ¶
func ResolveNATSURL ¶
func ResolveNATSURL(ctx context.Context, explicitURL string, mdnsEnabled bool, timeout time.Duration, logger *slog.Logger) (string, error)
ResolveNATSURL returns the NATS URL to connect to, applying the following precedence:
- If explicitURL is non-empty, return it directly (mDNS bypassed entirely).
- If mdnsEnabled is false, return an error with a clear message.
- Run Browse with the given timeout and return the discovered URL.
This is the single entry point that start.go calls before dialing NATS.
Types ¶
type Result ¶
type Result struct {
// NATSURL is a ready-to-use NATS connection URL, e.g. "nats://host:4222".
NATSURL string
// InstanceName is the DNS-SD service instance name of the discovered server.
InstanceName string
// InstanceID is the unique process ID extracted from the "id" TXT record.
InstanceID string
// Version is the server build version from the "version" TXT record.
Version string
}
Result holds the information extracted from a discovered sqi-server mDNS announcement.
func Browse ¶
Browse scans the local network for "_sqi._tcp" mDNS services and returns the first discovered server's connection details.
Browse blocks until:
- a server is found (returns the Result immediately), or
- timeout elapses (returns ErrDiscoveryTimeout), or
- ctx is canceled (returns ctx.Err()).
The timeout parameter should come from [workerconfig.DiscoveryConfig.MDNSTimeout] (default 5 s). Pass a zero timeout to use a 5 s default.