registry

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 14 Imported by: ? 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 registry keeps the hub's live tunnels keyed by peer ID and lets the application dial "through" any attached peer with an ordinary http.RoundTripper. Attach/detach events double as the peers' presence signal.

Presence can be projected to a pluggable directory.Directory (in-memory by default; SQL for a shared fleet — see internal/directory/sqldir). The live tunnels themselves are always local to the owning hub process; the directory only records who is attached where.

Index

Constants

View Source
const (
	ReasonSuperseded   = wire.ReasonSuperseded
	ReasonTokenRevoked = wire.ReasonTokenRevoked
	ReasonShuttingDown = wire.ReasonShuttingDown
	ReasonPeerStopping = wire.ReasonPeerStopping
	ReasonClosed       = wire.ReasonClosed
)

Well-known detach reasons, as carried by the tunnel's GoAway frame. Superseded, credential revocation, and deliberate stop are terminal for the peer (no redial); everything else means "redial with backoff".

Variables

View Source
var ErrPeerDetached = errors.New("holt: peer not attached")

ErrPeerDetached is returned by the per-peer RoundTripper when no tunnel is attached. Applications use errors.Is to map it onto their own "not reachable" error shape.

Functions

This section is empty.

Types

type Event

type Event struct {
	Peer   string
	Kind   EventKind
	Reason string
	At     time.Time
}

Event is one attach/detach transition. Reason is set on detach only ("superseded", "connection-lost", application reasons, …).

type EventKind

type EventKind uint8

EventKind labels a Registry event.

const (
	EventAttached EventKind = iota + 1
	EventDetached
)

type Option

type Option func(*Registry)

Option configures a Registry.

func WithDirectory

func WithDirectory(dir directory.Directory) Option

WithDirectory sets the presence backend. Default is an in-memory directory (correct for a single hub); pass a SQL directory to share presence across a fleet.

func WithHubID

func WithHubID(id string) Option

WithHubID sets this hub's stable instance id, recorded in the Directory so a fleet can tell which hub owns a peer. Give each hub a stable, unique id (hostname, pod name) in a multi-hub deployment so a restarting hub can clear its own stale rows. Default "local".

func WithMeterProvider

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider sets the OTel MeterProvider for tunnel metrics. Optional — without it the global provider is used, which is a no-op until the application installs an SDK.

type Registry

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

Registry tracks the live tunnel per peer. Safe for concurrent use.

func NewRegistry

func NewRegistry(logger *zap.Logger, opts ...Option) *Registry

NewRegistry builds a Registry. By default it keeps presence in-memory, identifies itself as "local", and records metrics against the global (no-op) OTel provider.

func (*Registry) Attach

func (r *Registry) Attach(
	peer, version string, kind tunneltype.Type, cc *http2.ClientConn, closeTunnel func(reason string),
) func(reason string)

Attach registers a live tunnel for peer. version is the peer's self-reported build (from the Hello frame). If a tunnel already exists it is closed with "superseded" and replaced — a crashed-and-redialed peer must never wait out a keepalive timeout on its own corpse. Returns a detach func the Attach handler calls with the detach reason; detach is idempotent and only removes THIS entry (a superseding attach can't be clobbered by the loser's cleanup).

func (*Registry) Attached

func (r *Registry) Attached(peer string) bool

Attached reports whether peer currently has a live tunnel ON THIS hub. For a fleet-wide answer, use LookupPeer.

func (*Registry) ClearStale

func (r *Registry) ClearStale(ctx context.Context) error

ClearStale removes any Directory rows this hub left behind after a crash. Call once on boot, before accepting attaches.

func (*Registry) CountTunnels

func (r *Registry) CountTunnels() int

CountTunnels returns the number of live tunnels on this hub.

func (*Registry) HubID

func (r *Registry) HubID() string

HubID returns this hub's instance id.

func (*Registry) ListTunnels

func (r *Registry) ListTunnels() []TunnelInfo

ListTunnels returns every live tunnel this hub owns, ordered by peer id.

func (*Registry) LookupPeer

func (r *Registry) LookupPeer(ctx context.Context, peer string) (directory.PeerRecord, bool, error)

LookupPeer returns the Directory record for peer — a fleet-wide answer to "is this peer attached, and to which hub?".

func (*Registry) Peers

func (r *Registry) Peers(ctx context.Context) ([]directory.PeerRecord, error)

Peers returns every attached peer known to the Directory (fleet-wide when the Directory is shared).

func (*Registry) RoundTripper

func (r *Registry) RoundTripper(peer string) http.RoundTripper

RoundTripper returns a stable http.RoundTripper for peer. Each RoundTrip resolves the CURRENT session, so a reattach mid-lifetime is transparent to callers holding the RoundTripper. Requests fail with ErrPeerDetached (use errors.Is) when no tunnel is attached ON THIS hub — RoundTripper never crosses hubs, since only the owning hub holds the live connection.

func (*Registry) StopAllTunnels

func (r *Registry) StopAllTunnels(reason string)

StopAllTunnels closes every live tunnel — hub shutdown/drain.

func (*Registry) StopTunnel

func (r *Registry) StopTunnel(peer, reason string) bool

StopTunnel force-closes peer's tunnel with reason and reports whether a tunnel was present. The Attach handler's detach emits the event and updates the Directory.

func (*Registry) Tunnel

func (r *Registry) Tunnel(peer string) (TunnelInfo, bool)

Tunnel returns the live tunnel info for peer on this hub.

func (*Registry) Watch

func (r *Registry) Watch(ctx context.Context) <-chan Event

Watch streams attach/detach events until ctx ends. The channel is buffered (watchChanSize) and lossy for slow consumers.

type TunnelInfo

type TunnelInfo struct {
	Peer        string
	PeerVersion string
	AttachedAt  time.Time
	// Type is what the peer said it carries (http, https).
	Type tunneltype.Type
}

TunnelInfo describes one live tunnel this hub owns. It is the local, live view — distinct from a Directory PeerRecord, which is the durable projection that may span hubs.

Jump to

Keyboard shortcuts

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