discovery

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package discovery advertises the running sqi-server on the local network via multicast DNS (mDNS / DNS-SD) so that workers and the sqi CLI can find it without manual address configuration.

The server publishes a single "_sqi._tcp" service instance whose SRV record points at the HTTP API port, the externally bound sqi endpoint. Additional connection details, including the broker URL, are carried in TXT records:

id=<instance-id>   unique per process; lets clients dedupe re-announcements
http=<http-port>   REST + WebSocket API port (also the SRV port)
nats_url=<url>     broker URL for worker connections; the nats:// or tls://
                   scheme says whether the broker requires TLS
host=<hostname>    advertising host's reported name
version=<version>  server build version
tls=1              present only when the REST API is TLS-terminated

Advertisement is toggleable via configuration (see Config.Enabled) for environments that prohibit multicast, such as most cloud VPCs.

Index

Constants

View Source
const MaxTXTString = 255

MaxTXTString is the DNS TXT character-string limit; one "key=value" record cannot exceed it.

View Source
const NATSURLKey = "nats_url"

NATSURLKey is the TXT record key the broker URL is advertised under.

Variables

View Source
var (
	// ErrNATSURLInvalid: the URL does not parse or names no host.
	ErrNATSURLInvalid = errors.New("must be nats://host:port or tls://host:port")
	// ErrNATSURLCredentials: mDNS would publish them in plaintext.
	ErrNATSURLCredentials = errors.New("carries credentials, which mDNS would publish in plaintext")
	// ErrNATSURLScheme: the scheme is neither nats nor tls.
	ErrNATSURLScheme = errors.New("must use the nats:// or tls:// scheme")
	// ErrNATSURLPortZero: no worker can dial port 0.
	ErrNATSURLPortZero = errors.New("names port 0, which no worker can dial")
	// ErrNATSURLTooLong: the record would not fit one TXT string.
	ErrNATSURLTooLong = errors.New("is too long for its DNS TXT record")
)

The ways an advertised broker URL can break the contract workers rely on. CheckNATSURL wraps exactly one of them.

Functions

func CheckNATSURL added in v0.4.0

func CheckNATSURL(raw string) (*url.URL, error)

CheckNATSURL is the one statement of what an advertised broker URL must be: it parses, names a host, carries no credentials, uses the nats or tls scheme, does not name port 0, and fits its TXT record. Configuration, the responder and the worker's browser all apply it. It returns the parsed URL and an error wrapping one of the ErrNATSURL errors, checked in that order; the URL is nil only for ErrNATSURLInvalid.

func NATSURLRecordLen added in v0.4.0

func NATSURLRecordLen(raw string) int

NATSURLRecordLen is the length of the TXT record that advertises raw.

func SRVHost added in v0.4.0

func SRVHost() string

SRVHost returns the host the service's SRV record names: the machine's hostname in the ".local" domain. The server uses it as the host of a derived broker URL, so that URL names the same host as the SRV record and a discovering worker can fall back to the advertised address when that name does not resolve.

Types

type Config

type Config struct {
	// Enabled controls whether the responder publishes an mDNS record.
	// When false, [New] still succeeds but [Responder.Start] is a no-op.
	Enabled bool

	// InstanceName is the DNS-SD service instance name (the human-readable
	// label shown in service browsers). Each sqi-server on the same subnet
	// should use a distinct name. Must not be empty when Enabled is true.
	InstanceName string

	// HTTPAddr is the server's HTTP listen address ("host:port"). The port is
	// extracted and advertised as the SRV record port.
	HTTPAddr string

	// NATSURL is the broker URL workers should connect to, advertised as the
	// "nats_url" TXT record. Its nats:// or tls:// scheme is how a discovering
	// worker learns whether the broker requires TLS. Must not be empty when
	// Enabled is true.
	NATSURL string

	// InstanceID uniquely identifies this server process. When empty, [New]
	// generates a random UUID. Advertised in the "id" TXT record.
	InstanceID string

	// HTTPTLS reports whether the REST/WebSocket listener is TLS-terminated,
	// advertised as the "tls" TXT record.
	//
	// internal/worker/discovery surfaces it as Result.HTTPTLS. Enrollment never
	// uses a discovered URL, so a worker uses it only to warn that its http://
	// nats.server_url points at an HTTPS server.
	HTTPTLS bool

	// Interfaces restricts which network interfaces the service is advertised
	// on. Nil advertises on every multicast-capable interface.
	//
	// Tests set it to loopback so a test run never transmits an advertisement
	// onto the surrounding network. It has no config key.
	Interfaces []net.Interface
}

Config holds the parameters needed to advertise the sqi-server instance.

type Responder

type Responder struct {
	// contains filtered or unexported fields
}

Responder owns the lifecycle of the mDNS service advertisement. Create one with New, then call Responder.Start and Responder.Shutdown.

func New

func New(cfg Config, logger *slog.Logger) (*Responder, error)

New validates cfg and returns a Responder. It parses the HTTP port and checks the broker URL up front so configuration errors surface before the server boots. An empty Config.InstanceID is filled with a freshly generated UUID.

When cfg.Enabled is false, validation is skipped and the returned responder's Start is a no-op.

func (*Responder) Shutdown

func (r *Responder) Shutdown()

Shutdown stops the mDNS advertisement, sending goodbye packets so browsers remove the service promptly. It is safe to call when Start was never called or discovery is disabled.

Start and Shutdown are not safe for concurrent use; the server lifecycle calls them sequentially (Start during boot, Shutdown during graceful stop).

func (*Responder) Start

func (r *Responder) Start(ctx context.Context) error

Start publishes the mDNS service record on every multicast-capable interface, or on Config.Interfaces when set. It returns nil immediately when discovery is disabled. The advertisement runs in background goroutines managed by the underlying responder until Responder.Shutdown is called.

ctx is used only for logging: zeroconf registration is non-blocking and does not observe it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL