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
- 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; match it with errors.Is.
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 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.
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 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 ¶
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 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 ¶
StopAllTunnels closes every live tunnel — hub shutdown/drain.
func (*Registry) StopTunnel ¶
StopTunnel force-closes peer's tunnel with reason and reports whether one was present.
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.