registration

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: AGPL-3.0 Imports: 17 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 the worker.register JetStream subject. On graceful shutdown, Registrar.Deregister publishes a protocol.DeregisterMsg to worker.deregister 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. This ensures the server always has a current, online record for the worker without requiring 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 strands the worker permanently, since the server only learns of it via worker.register and NAKs the heartbeats of workers it has no record of.

That "no stream yet" window is real and routinely hit: a worker started alongside its server (as in a compose stack) connects as soon as the NATS listener opens, which is 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 via a JetStream push consumer and does not send a reply of its own; the stream ack is the acknowledgment. Explicit application-level accept/reject is a planned protocol enhancement.

In-memory capability map

The Registrar stores the merged capabilities.Capabilities for the lifetime of the process. Heartbeat publishers and log formatters access it via Registrar.Capabilities so they can include capability context in their output without re-detecting hardware on every call.

Index

Constants

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 generous relative to the window it covers (a server provisions its streams milliseconds after opening its NATS listener). It exists to outlast a slow or restarting server, not to be reached: a worker that cannot register is useless, so waiting beats exiting.

Variables

This section is empty.

Functions

This section is empty.

Types

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,
) (*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 to the server is what the server assumes it will enforce, and a transposition between the two would make the server's dispatch gate compare against a number no evaluation ever 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)

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

Deregister is best-effort: failures are logged as warnings since the server will detect the absence via heartbeat timeout regardless. The caller is responsible for flushing any buffered publishes before closing the NATS connection (typically via natsclient.Drain deferred in start.go).

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 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