Documentation
¶
Overview ¶
Package hub is the server half of the holt: it accepts Attach streams from peers, keeps a registry of 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.
Identity is the application's concern: the Handler is constructed with an Identity func that extracts the peer ID from the request context (JWT claims, mTLS SAN, header — whatever the surrounding middleware established). The handshake itself carries no identity.
Presence can be projected to a pluggable Directory (in-memory by default; SQL for a shared fleet — see hub/sqldir). The live tunnels themselves are always local to the owning hub process; the Directory only records who is attached where.
Index ¶
- Variables
- type Directory
- type Event
- type EventKind
- type Handler
- type HandlerOption
- type Identity
- type MemoryDirectory
- func (d *MemoryDirectory) Attach(_ context.Context, rec PeerRecord) error
- func (d *MemoryDirectory) ClearHub(_ context.Context, hub string) error
- func (d *MemoryDirectory) Detach(_ context.Context, peer, hub string) error
- func (d *MemoryDirectory) List(_ context.Context) ([]PeerRecord, error)
- func (d *MemoryDirectory) Lookup(_ context.Context, peer string) (PeerRecord, bool, error)
- type PeerRecord
- type Registry
- func (r *Registry) Attach(peer, version string, cc *http2.ClientConn, closeTunnel func(reason string)) 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) (PeerRecord, bool, error)
- func (r *Registry) Peers(ctx context.Context) ([]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 RegistryOption
- type TunnelInfo
Constants ¶
This section is empty.
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 Directory ¶
type Directory interface {
// Attach records (or upserts) a peer as attached to rec.Hub.
Attach(ctx context.Context, rec PeerRecord) error
// Detach removes peer's record, but only if hub still owns it —
// a stale detach from a superseded owner must not evict the new
// one.
Detach(ctx context.Context, peer, hub string) error
// Lookup returns peer's record and whether it was found.
Lookup(ctx context.Context, peer string) (PeerRecord, bool, error)
// List returns every attached peer, ordered by peer id.
List(ctx context.Context) ([]PeerRecord, error)
// ClearHub removes every record owned by hub — a hub calls this on
// boot with its own stable id to clear rows it left behind after a
// crash.
ClearHub(ctx context.Context, hub string) error
}
Directory is the pluggable presence backend behind a Registry. It records attach/detach and answers presence queries. Implementations range from in-memory (single hub, default) to SQL (sqlite/postgres, shared across a fleet so any hub can see who is attached where).
A Directory never holds live connections — RoundTripper always resolves against the owning hub's local session map. The Directory is for routing and observability: "is peer X attached, and to which hub?" Cross-hub request forwarding, if wanted, is the application's job on top of Lookup.
All methods take a context and may do I/O. The Registry calls them best-effort — a Directory error is logged but never fails a live attach or detach, so a database blip cannot sever a working tunnel.
type Event ¶
Event is one attach/detach transition. Reason is set on detach only ("superseded", "connection-lost", application reasons, …).
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler accepts reverse-tunnel attachments over WebSockets: the peer upgrades a plain HTTP request, then every binary message carries one TunnelFrame. WebSockets are the carrier precisely because they pass through the layers that gRPC cannot — CDN public hostnames, access proxies, HTTP/1.1-only edges.
Mount it behind the application's auth middleware; the Identity func reads whatever that middleware established from the upgrade request's context.
func NewHandler ¶
func NewHandler(registry *Registry, identity Identity, logger *zap.Logger, opts ...HandlerOption) *Handler
NewHandler builds the attach handler. identity maps the authenticated request context to the registry key.
func (*Handler) ServeHTTP ¶ added in v0.13.0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP accepts one peer's reverse tunnel: identity, WebSocket upgrade, handshake, then an HTTP/2 client session over the raw byte stream until the peer disconnects or the registry closes the tunnel.
type HandlerOption ¶
type HandlerOption func(*Handler)
HandlerOption configures a Handler.
func WithPeerTLS ¶
func WithPeerTLS(cfg *tls.Config) HandlerOption
WithPeerTLS makes the hub run a TLS client over each tunnel before speaking HTTP/2, so the payload is encrypted end-to-end between hub and peer. Peers must attach with a matching TLS server config (dial.Options.TLSConfig). Use it to verify peer certificates and to keep the payload confidential even when the outer WebSocket hop is plaintext or TLS-terminated at a proxy. NextProtos is forced to h2.
func WithTracerProvider ¶
func WithTracerProvider(tp trace.TracerProvider) HandlerOption
WithTracerProvider sets the OTel TracerProvider used to span each tunnel's lifetime. Optional — without it the global provider is used, which is a no-op until the application installs an SDK.
type Identity ¶
Identity extracts the peer ID from a request context. The application supplies it — it's the bridge from whatever auth middleware wraps the attach handler (JWT claims, mTLS SAN, …) to the registry key. Returning ("", err) rejects the attach.
type MemoryDirectory ¶
type MemoryDirectory struct {
// contains filtered or unexported fields
}
MemoryDirectory is the default Directory: a mutex-guarded map. Zero dependencies, correct for a single hub. Safe for concurrent use.
func NewMemoryDirectory ¶
func NewMemoryDirectory() *MemoryDirectory
NewMemoryDirectory returns an empty in-memory directory.
func (*MemoryDirectory) Attach ¶
func (d *MemoryDirectory) Attach(_ context.Context, rec PeerRecord) error
func (*MemoryDirectory) ClearHub ¶
func (d *MemoryDirectory) ClearHub(_ context.Context, hub string) error
func (*MemoryDirectory) Detach ¶
func (d *MemoryDirectory) Detach(_ context.Context, peer, hub string) error
func (*MemoryDirectory) List ¶
func (d *MemoryDirectory) List(_ context.Context) ([]PeerRecord, error)
func (*MemoryDirectory) Lookup ¶
func (d *MemoryDirectory) Lookup(_ context.Context, peer string) (PeerRecord, bool, error)
type PeerRecord ¶
type PeerRecord struct {
Peer string
Hub string // hub instance that owns the live tunnel
PeerVersion string
AttachedAt time.Time
}
PeerRecord is one peer's presence entry: which hub currently owns its live tunnel, its build version, and when it attached. This is the durable, shareable projection of a live tunnel — everything EXCEPT the tunnel itself, which is a live *http2.ClientConn that only its owning hub process holds.
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 ...RegistryOption) *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, 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 ¶
func (r *Registry) Peers(ctx context.Context) ([]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 ¶
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 RegistryOption ¶
type RegistryOption func(*Registry)
RegistryOption configures a Registry.
func WithDirectory ¶
func WithDirectory(dir Directory) RegistryOption
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) RegistryOption
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) RegistryOption
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.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package admin implements the holt hub's Admin gRPC service over a *hub.Registry: list live tunnels, force one closed, and — when a Blocker is supplied — ban/unban a peer id.
|
Package admin implements the holt hub's Admin gRPC service over a *hub.Registry: list live tunnels, force one closed, and — when a Blocker is supplied — ban/unban a peer id. |
|
Package proxy is the hub's data plane: an http.Handler that picks the peer a request targets and dials it through that peer's tunnel.
|
Package proxy is the hub's data plane: an http.Handler that picks the peer a request targets and dials it through that peer's tunnel. |
|
Package sqldir is a SQL-backed hub.Directory: a shared peer-presence store so a fleet of hubs can each see who is attached where.
|
Package sqldir is a SQL-backed hub.Directory: a shared peer-presence store so a fleet of hubs can each see who is attached where. |