Documentation
¶
Overview ¶
Package broadcast provides a generic fan-out event distributor.
A Broadcaster has exactly one event source (channel) and N subscribers. It is the sole consumer of the source channel, eliminating the channel-race anti-pattern where multiple goroutines compete to read from the same channel.
Usage across deepwork subsystems:
- BS-11 CLI Bridge: CLI stdout → Broadcaster → [RingBuffer, WS-1, WS-2, ...]
- BS-03 Conversation: EventBus → Broadcaster → [SSE-1, SSE-2, ...]
- BS-08 CLI Session: PTY stdout → Broadcaster → [WS] (single subscriber, but correct pattern)
- BS-12 Council: Per-channel events → Broadcaster → [SSE subscribers]
Design decisions:
- Non-blocking publish to subscribers (slow consumers get dropped, not blocking)
- Subscriber channels are buffered (configurable, default 64)
- When source closes, all subscriber channels are closed
- Thread-safe subscribe/unsubscribe during active broadcast
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broadcaster ¶
type Broadcaster[T any] struct { // contains filtered or unexported fields }
Broadcaster[T] distributes events from a single source to N subscribers.
func New ¶
func New[T any](sourceCh <-chan T, onEvent func(T), onDone func()) *Broadcaster[T]
New creates a Broadcaster and starts consuming from sourceCh. onEvent is called for each event before broadcasting (e.g., write to buffer). When sourceCh closes, all subscriber channels are closed and onDone is called.
func (*Broadcaster[T]) Subscribe ¶
func (b *Broadcaster[T]) Subscribe(bufSize int) chan T
Subscribe returns a buffered channel that receives broadcast events. The caller must Unsubscribe when done to prevent goroutine leaks.
func (*Broadcaster[T]) SubscriberCount ¶
func (b *Broadcaster[T]) SubscriberCount() int
SubscriberCount returns the number of active subscribers.
func (*Broadcaster[T]) Unsubscribe ¶
func (b *Broadcaster[T]) Unsubscribe(ch chan T)
Unsubscribe removes a subscriber and drains its channel.