Documentation
¶
Overview ¶
Package brokerclient provides a single-connection client for the intercom broker. It auto-spawns the broker on first use, pumps inbound frames into a callback, and answers send/list_peers requests with id-correlated replies.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDisconnected = errors.New("broker client: broker disconnected")
ErrDisconnected is returned from Send/ListPeers when the broker connection drops mid-call.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client owns at most one connection to the broker at a time. It auto-spawns the broker on the first connect attempt if no broker is listening, and reconnects on demand if the connection drops.
func NewClient ¶
func NewClient(opts ClientOptions) *Client
NewClient constructs a client. Network I/O does not start until Connect (or one of the request methods, which connects on demand).
func (*Client) Connect ¶
Connect establishes a connection (auto-spawning the broker if needed) and completes the hello/welcome handshake. Safe to call multiple times and concurrently: a successful Connect is idempotent; concurrent callers serialize on connectGate so the broker only ever sees one hello per peer.
func (*Client) ConnectionEvents ¶
func (c *Client) ConnectionEvents() <-chan ConnectionEvent
ConnectionEvents returns a latest-state notification stream for connection lifecycle changes. The channel has capacity one: when a consumer falls behind, a newer state replaces the older state. Generation lets reconnect loops distinguish a new connection from the one that failed. The channel is never closed; ConnectionStateClosed is the terminal event.
type ClientOptions ¶
type ClientOptions struct {
Name string // peer name (already validated by the caller)
Version string // client version reported in the hello frame
SocketPath string // broker socket
BrokerBin string // path to the broker binary; if empty, os.Executable() is used
Logger *slog.Logger
OnDeliver func(wire.Deliver) // called from the read goroutine for each inbound deliver
OnGoodbye func(reason string) // called when the broker explicitly sends goodbye
}
ClientOptions configures a Client.
type ConnectionEvent ¶
type ConnectionEvent struct {
State ConnectionState
Generation uint64
Cause ConnectionEventCause
// Reason carries the broker-provided text for goodbye events.
Reason string
Err error
}
ConnectionEvent is the latest observed connection state. Generation starts at one and advances after each successful handshake. A disconnected event retains the generation of the connection that ended; a subsequent connected event therefore has a larger generation.
type ConnectionEventCause ¶
type ConnectionEventCause string
ConnectionEventCause identifies why a connection state changed.
const ( ConnectionEventCauseNone ConnectionEventCause = "" ConnectionEventCauseEOF ConnectionEventCause = "eof" ConnectionEventCauseGoodbye ConnectionEventCause = "goodbye" ConnectionEventCauseReadError ConnectionEventCause = "read_error" ConnectionEventCauseWriteError ConnectionEventCause = "write_error" ConnectionEventCauseClosed ConnectionEventCause = "closed" )
type ConnectionState ¶
type ConnectionState uint8
ConnectionState describes the client's current broker connection state.
const ( ConnectionStateInit ConnectionState = iota ConnectionStateConnected ConnectionStateDisconnected ConnectionStateClosed )
type HelloError ¶
HelloError is returned when the broker rejects our hello.
func (*HelloError) Error ¶
func (e *HelloError) Error() string