brokerclient

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 12 Imported by: 0

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

View Source
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) Close

func (c *Client) Close() error

Close terminates the connection (if any) and refuses further requests.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

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.

func (*Client) ListPeers

func (c *Client) ListPeers(ctx context.Context) ([]string, error)

ListPeers issues a wire.ListPeers and returns the resulting peer list.

func (*Client) Send

func (c *Client) Send(ctx context.Context, to, message string) (wire.SendAck, error)

Send issues a wire.Send and waits for the matching SendAck (or an error / disconnect). Reconnects automatically if previously disconnected. Honors ctx for cancellation.

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

type HelloError struct {
	Code    wire.Code
	Message string
}

HelloError is returned when the broker rejects our hello.

func (*HelloError) Error

func (e *HelloError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL