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_url=<url> broker URL for worker connections (nats:// or tls://) host=<hostname> advertising host's reported name version=<version> server build version
The browser uses nats_url as advertised. The URL's scheme carries TLS, so there is no separate TLS record for the broker, and a server that does not advertise nats_url is not usable: there is no fallback.
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 ApplyTLS ¶ added in v0.4.0
func ApplyTLS(ctx context.Context, cfg *workerconfig.NATSConfig, found Result, logger *slog.Logger)
ApplyTLS folds what mDNS learned about the server into the worker's NATS configuration.
This is what makes nats.tls_enabled's "auto" mode able to see a TLS-required broker. A discovered tls:// URL makes nats.go use TLS on its own, but only with the system roots; folding it into the configuration is what applies the worker's own tls_* settings and lets this explain a certificate failure that would otherwise name neither the cause nor the setting that fixes it.
An explicit nats.url discovers nothing, so found's TLS fields are false and this is a no-op: an operator who configures the URL by hand also configures the transport by hand.
On trusting an advertisement at all: the signal cannot be used to weaken the connection. Forging a tls:// nats_url upgrades a worker to TLS; withholding it leaves the worker attempting plaintext against a TLS broker, which fails closed. Any locally configured tls_* setting still wins, so an advertisement can never downgrade a worker that was configured for TLS. And an attacker able to forge mDNS already controls NATSURL entirely, so the tls:// scheme adds no surface that was not already there.
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
// NATSTLS reports whether the discovered server's broker requires TLS:
// true exactly when the advertised nats_url has the tls scheme. An
// explicit URL discovers nothing and leaves this false.
//
// This is an input to nats.tls_enabled's "auto" mode: it is the one signal
// that can tell a worker the broker needs TLS without the operator having
// configured anything locally.
NATSTLS bool
// HTTPTLS reports whether the discovered server's REST API is
// TLS-terminated, from the "tls" TXT record. Enrollment does not use a
// discovered URL (nats.server_url is always explicit), so this is used to
// warn about an http:// server_url pointed at an https:// server rather
// than to rewrite anything.
HTTPTLS bool
// contains filtered or unexported fields
}
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.
func Resolve ¶ added in v0.4.0
func Resolve(ctx context.Context, explicitURL string, mdnsEnabled bool, timeout time.Duration, logger *slog.Logger) (Result, error)
Resolve returns the discovered server, 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 and return what it found.
It returns the whole Result, not just the URL: the TLS records travel with it, and a caller that only took the URL would silently drop the one signal that can tell a worker its broker needs TLS.
An explicit URL yields a Result with only NATSURL set — nothing was discovered, so nothing is known about the server's transport, and the TLS fields stay false rather than claiming otherwise.
This is the single entry point that start.go calls before dialing NATS.