registration

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: 20 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 registration manages worker registration with sqi-server over NATS.

Lifecycle

A Registrar is created once at worker startup, after the NATS connection is established. Its Registrar.Register method publishes a protocol.RegisterMsg to its own worker.register.<worker> JetStream subject. On graceful shutdown, Registrar.Deregister publishes a protocol.DeregisterMsg to worker.deregister.<worker> so the server marks the worker offline immediately.

Re-registration

Registrar.SetupReconnectHook installs a NATS reconnect callback that re-publishes the registration message whenever the connection is restored after a drop, so the server keeps a current, online record for the worker without a process restart.

Acknowledgment

Registration is published with a JetStream ack, so Registrar.Register returns only once the SQI_WORKER stream has durably stored the message. A plain core-NATS publish would be silently discarded when no stream is behind the subject, which would strand the worker permanently: the server learns of a worker only via worker.register.<worker> and NAKs the heartbeats of workers it has no record of.

A "no stream yet" window occurs when a worker is started alongside its server (as in a compose stack): it connects as soon as the NATS listener opens, a few milliseconds before the server finishes provisioning its streams. Register therefore retries while the stream is absent, up to RetryBudget or the caller's context deadline, whichever is sooner.

The server processes worker.register.<worker> via a JetStream push consumer and does not send a reply of its own; the stream ack is the acknowledgment. The protocol has no application-level accept or reject.

In-memory capability map

The Registrar stores the merged capabilities.Capabilities for the lifetime of the process and returns a copy from Registrar.Capabilities, so a caller can read it without re-detecting hardware. No production code calls it.

Index

Constants

View Source
const DeregisterFlushTimeout = 2 * time.Second

DeregisterFlushTimeout bounds how long Registrar.Deregister waits for the NATS server to receive the message.

View Source
const (
	RetryBudget = 30 * time.Second
)

Retry pacing for the registration publish. The wait between attempts starts at retryInitial and doubles up to retryMax; RetryBudget caps the total time Register spends waiting for the SQI_WORKER stream to appear.

RetryBudget is long relative to the window it covers (a server provisions its streams milliseconds after opening its NATS listener). It is meant to outlast a slow or restarting server: a worker that cannot register is useless, so it waits rather than exits.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.4.0

type Option func(*Registrar)

Option configures a Registrar.

func WithCloudInstanceID added in v0.4.0

func WithCloudInstanceID(id string) Option

WithCloudInstanceID reports id as the cloud instance this worker runs on.

func WithHost added in v0.4.0

func WithHost(hostname, ip string) Option

WithHost reports hostname and ip in every registration instead of the host's own (os.Hostname and the outbound interface's address). The load harness's simulated workers, thousands to one host, each report their own.

func WithInterfaces added in v0.4.0

func WithInterfaces(ifaces []protocol.NetInterface) Option

WithInterfaces reports ifaces in every registration instead of the host's own interfaces. The load harness passes a synthetic interface per simulated worker, so thousands of them do not each report the generator host's network cards.

type Registrar

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

Registrar publishes worker registration and deregistration messages and stores the worker's capability map in memory for the process lifetime.

func New

func New(
	nc *nats.Conn,
	workerID string,
	cfg workerconfig.WorkerSettings,
	exprLimits fmtres.ExprLimits,
	caps capabilities.Capabilities,
	logger *slog.Logger,
	opts ...Option,
) (*Registrar, error)

New creates a Registrar over an established NATS connection. caps should already have manual tags merged in (via capabilities.Capabilities.MergeManualTags) before being passed here.

exprLimits must be the same value the worker's session manager enforces with, not a second mapping of the same configuration: what this worker advertises is what the server assumes it enforces, and a transposition between the two would make the server's dispatch gate compare against a number no evaluation uses. cmd/sqi-worker passes exprLimitsFromConfig(cfg.Expr) to both.

It returns an error only if a JetStream context cannot be derived from nc.

func (*Registrar) Capabilities

func (r *Registrar) Capabilities() capabilities.Capabilities

Capabilities returns the merged capability set stored at construction time. The returned value is a copy; mutations do not affect the Registrar's state.

func (*Registrar) Deregister

func (r *Registrar) Deregister(reason string, kind protocol.DeregisterKind)

Deregister publishes a DeregisterMsg to worker.deregister.<worker> on graceful shutdown so the server marks this worker offline immediately rather than waiting for the heartbeat timeout sweep.

kind says whether the worker may come back: "" for a restart, after which it returns with the same identity, protocol.DeregisterRetired when a capacity group retires the machine, or protocol.DeregisterPreempted when its machine is being reclaimed.

Deregister is best-effort: failures are logged as warnings since the server will detect the absence via heartbeat timeout regardless. It flushes the connection before it returns, waiting up to DeregisterFlushTimeout, so the message is not lost when the caller then stops the process or suspends the machine.

func (*Registrar) InstanceID added in v0.4.0

func (r *Registrar) InstanceID() string

InstanceID returns the identifier this process sends in every registration.

func (*Registrar) LastRegisteredAt

func (r *Registrar) LastRegisteredAt() time.Time

LastRegisteredAt returns the time of the most recent successful Register call, or the zero time if Register has never succeeded in this process lifetime. Safe for concurrent use; the heartbeat watchdog uses this to determine whether it needs to re-register after a reconnect.

func (*Registrar) Register

func (r *Registrar) Register(ctx context.Context) error

Register publishes a RegisterMsg to worker.register.<worker> and returns once the stream has acked it. It is safe to call multiple times (at boot and on NATS reconnect).

It blocks while the SQI_WORKER stream does not exist yet, returning an error once RetryBudget or ctx expires (see package-level documentation).

func (*Registrar) SetupReconnectHook

func (r *Registrar) SetupReconnectHook(_ context.Context)

SetupReconnectHook installs a NATS reconnect handler that re-publishes the registration message whenever the connection is restored. It replaces any previously set ReconnectHandler on the connection, so callers must install this after any other reconnect hooks they require.

Re-registration failures are logged but do not cause a fatal error; the server's heartbeat sweep will time out the stale worker record regardless and the next successful heartbeat will re-establish liveness.

Jump to

Keyboard shortcuts

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