Documentation
¶
Overview ¶
Package conn provides connection lifecycle management for the compose library.
It handles module ID allocation, name-to-ID mapping, hot-plug detection, graceful disconnect, and reconnection matching. The package is standalone with no internal dependencies.
The core types are:
- Registry manages module ID allocation and lookup.
- Manager orchestrates the full module lifecycle (connect, handshake, active, disconnect) and fires event callbacks.
- Module holds metadata about a connected module.
- State represents the lifecycle state of a module connection.
All exported methods are safe for concurrent use from multiple goroutines.
Index ¶
- Variables
- type Manager
- type Module
- type Registry
- func (r *Registry) All() []*Module
- func (r *Registry) Count() int
- func (r *Registry) Lookup(id uint64) (*Module, bool)
- func (r *Registry) LookupByName(name string) (*Module, bool)
- func (r *Registry) Register(name string, width, height, fps uint16) (uint64, error)
- func (r *Registry) SetState(id uint64, state State)
- func (r *Registry) Unregister(id uint64)
- func (r *Registry) UpdateLastFrame(id uint64, t time.Time)
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMaxModules is returned when attempting to register a module beyond // the configured maximum capacity. ErrMaxModules = errors.New("compose: maximum module count reached") // ErrNameTaken is returned when attempting to register a module with a // name that is already in use by an active module. ErrNameTaken = errors.New("compose: module name already registered") // ErrNotFound is returned when a lookup or operation references a module // ID that does not exist in the registry. ErrNotFound = errors.New("compose: module not found") )
Sentinel errors for the conn package.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager orchestrates module lifecycle: connect, handshake, active, disconnect. It wraps a Registry and adds event callbacks plus reconnection matching. All methods are safe for concurrent access from multiple goroutines.
func NewManager ¶
NewManager creates a Manager with the given max module count. The maxModules parameter must be positive; values less than 1 are clamped to 1.
func (*Manager) HandleConnect ¶
HandleConnect processes a new module connection. It allocates an ID, transitions the module to Active state, and fires the OnConnect callback if registered.
If a module with the same name was previously disconnected and unregistered, it is treated as a reconnection — a new ID is allocated but the name slot is reused seamlessly.
Returns ErrMaxModules if the registry is at capacity. Returns ErrNameTaken if a module with the same name is currently active.
func (*Manager) HandleDisconnect ¶
HandleDisconnect processes a module disconnection (graceful or crash). It transitions the module to Disconnected state, fires the OnDisconnect callback, and removes the module from the registry so its name can be reused.
If the module ID does not exist, this is a no-op.
func (*Manager) OnConnect ¶
OnConnect sets the callback fired when a module becomes active. Only one callback can be set; subsequent calls replace the previous one. Passing nil removes the callback.
func (*Manager) OnDisconnect ¶
OnDisconnect sets the callback fired when a module disconnects. Only one callback can be set; subsequent calls replace the previous one. Passing nil removes the callback.
type Module ¶
type Module struct {
// ID is the compositor-assigned unique identifier for this module.
// IDs are monotonically increasing and never reused within a process lifetime.
ID uint64
// Name is the human-readable module name (e.g., "clock", "weather").
Name string
// State is the current lifecycle state of the module connection.
State State
// Width is the frame width in pixels.
Width uint16
// Height is the frame height in pixels.
Height uint16
// FPS is the requested frame rate.
FPS uint16
// ConnectedAt is the time the module first connected.
ConnectedAt time.Time
// LastFrameAt is the time the most recent frame was received.
LastFrameAt time.Time
}
Module holds metadata about a connected module.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages module ID allocation and name-to-ID mapping. All methods are safe for concurrent access from multiple goroutines.
func NewRegistry ¶
NewRegistry creates a registry with the given maximum capacity. The maxModules parameter must be positive; values less than 1 are clamped to 1.
func (*Registry) All ¶
All returns a snapshot of all registered modules. The returned slice contains copies; modifying them does not affect the registry.
func (*Registry) LookupByName ¶
LookupByName returns the module by name. Returns (nil, false) if not found.
func (*Registry) Register ¶
Register allocates a new module ID and stores the module. Returns ErrMaxModules if at capacity. Returns ErrNameTaken if a module with the same name is already active.
func (*Registry) SetState ¶
SetState updates a module's state. If the module ID does not exist, this is a no-op.
func (*Registry) Unregister ¶
Unregister removes a module from the registry, freeing its slot. The module's name becomes available for reuse. If the module ID does not exist, this is a no-op.
type State ¶
type State uint8
State represents the lifecycle state of a module connection.
const ( // StateConnecting indicates a connection has been initiated but the // handshake has not yet started. StateConnecting State = iota // StateHandshaking indicates the handshake is in progress. StateHandshaking // StateActive indicates the module is fully connected and sending frames. StateActive // StateDisconnected indicates the connection was lost or gracefully closed. StateDisconnected )