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
- Variables
- type Event
- type EventKind
- type Option
- type Registry
- func (r *Registry) Attach(peer, version string, kind tunneltype.Type, cc *http2.ClientConn, ...) func(reason string)
- func (r *Registry) Attached(peer string) bool
- func (r *Registry) ClearStale(ctx context.Context) error
- func (r *Registry) CountTunnels() int
- func (r *Registry) HubID() string
- func (r *Registry) ListTunnels() []TunnelInfo
- func (r *Registry) LookupPeer(ctx context.Context, peer string) (directory.PeerRecord, bool, error)
- func (r *Registry) Peers(ctx context.Context) ([]directory.PeerRecord, error)
- func (r *Registry) RoundTripper(peer string) http.RoundTripper
- func (r *Registry) StopAllTunnels(reason string)
- func (r *Registry) StopTunnel(peer, reason string) bool
- func (r *Registry) Tunnel(peer string) (TunnelInfo, bool)
- func (r *Registry) Watch(ctx context.Context) <-chan Event
- type TunnelInfo
Constants ¶
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 ¶
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 ¶
Event is one attach/detach transition. Reason is set on detach only ("superseded", "connection-lost", application reasons, …).
type Option ¶
type Option func(*Registry)
Option configures a Registry.
func WithDirectory ¶
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 ¶
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 ¶
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 ¶
Attached reports whether peer currently has a live tunnel ON THIS hub. For a fleet-wide answer, use LookupPeer.
func (*Registry) ClearStale ¶
ClearStale removes any Directory rows this hub left behind after a crash. Call once on boot, before accepting attaches.
func (*Registry) CountTunnels ¶
CountTunnels returns the number of live tunnels on this hub.
func (*Registry) ListTunnels ¶
func (r *Registry) ListTunnels() []TunnelInfo
ListTunnels returns every live tunnel this hub owns, ordered by peer id.
func (*Registry) LookupPeer ¶
LookupPeer returns the Directory record for peer — a fleet-wide answer to "is this peer attached, and to which hub?".
func (*Registry) Peers ¶
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 ¶
StopAllTunnels closes every live tunnel — hub shutdown/drain.
func (*Registry) StopTunnel ¶
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.
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.