registry

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 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, projected to a pluggable directory.Directory (in-memory by default; SQL for a shared fleet). Live tunnels 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; match it with errors.Is.

Functions

This section is empty.

Types

type Event

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

Event is one attach/detach transition. Reason is set on detach only ("superseded", "connection-lost", application reasons, …). PeerVersion and Type describe the tunnel the transition was about, carried in the event so a replayed one still tells the whole story after the tunnel it describes is gone.

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 WithEventHistory added in v0.48.0

func WithEventHistory(n int) Option

WithEventHistory sets how many past attach/detach events RecentEvents keeps. Zero disables the history. Default defaultEventHistory.

func WithHubID

func WithHubID(id string) Option

WithHubID sets this hub's instance id, recorded in the Directory so a fleet can tell which hub owns a peer. Use a stable, unique id (hostname, pod name) 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. Default: the global provider, a no-op until an SDK is installed.

func WithPeerLog added in v0.48.0

func WithPeerLog(log PeerLog) Option

WithPeerLog sets the durable peer log written on attach and detach. Default: none, so the Registry remembers a peer only while it is attached.

type PeerLog added in v0.48.0

type PeerLog interface {
	Attached(peer, version string) error
	Detached(peer, reason string) error
}

PeerLog is the durable side of peer identity: which peers this hub knows, and when each was last seen. The Registry writes to it best-effort on attach and detach, exactly like the Directory, and a nil PeerLog disables the writes.

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 with the defaults above.

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, closing any existing one with "superseded" — a crashed-and-redialed peer must never wait out a keepalive timeout on its own corpse. The returned detach func is idempotent and removes only THIS entry, so a superseding attach cannot 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) RecentEvents added in v0.48.0

func (r *Registry) RecentEvents() []Event

RecentEvents returns the attach/detach transitions still held in memory, oldest first. It is the backlog for a watcher that subscribed too late, not a record: bounded, and empty after a restart.

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 is transparent to holders. Requests fail with ErrPeerDetached when no tunnel is attached ON THIS hub — a 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 one was present.

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 — the local, live view, distinct from a Directory PeerRecord.

Jump to

Keyboard shortcuts

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