Documentation
¶
Overview ¶
Package broker provides a simple in-process notification broker for broadcasting messages to multiple consumers without blocking.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker manages notification subscriptions and broadcasts messages to all subscribers. It uses non-blocking sends to ensure that slow consumers cannot block the system. For methods listed in coalesceable, a per-subscriber drain goroutine delivers the latest notification whenever the output channel has space, preventing stale drops.
func NewBroker ¶
func NewBroker(ctx context.Context, source <-chan models.Notification, coalesceableMethods ...string) *Broker
NewBroker creates a new notification broker that reads from the source channel and broadcasts to all subscribers. coalesceableMethods lists notification methods that use last-write-wins coalescing when a subscriber's channel is full; all other methods are dropped with a warning (existing behaviour).
func (*Broker) Start ¶
func (b *Broker) Start()
Start begins the broker's main broadcast loop in a goroutine. It reads notifications from the source channel and sends them to all subscribers using non-blocking sends. When the source channel closes or context is cancelled, it closes all subscriber channels and exits.
func (*Broker) Stop ¶
func (b *Broker) Stop()
Stop gracefully shuts down the broker by closing all subscriber channels. This should be called during service shutdown.
func (*Broker) Subscribe ¶
func (b *Broker) Subscribe(bufferSize int) (notifChan <-chan models.Notification, id int)
Subscribe creates a new subscription and returns a channel that will receive notifications. The bufferSize determines how many notifications can be queued before coalescing (for coalesceable methods) or dropping (for all others) kicks in.
Returns the notification channel and a subscription ID that can be used for unsubscribing.
func (*Broker) Unsubscribe ¶
Unsubscribe removes a subscription and waits for its drain goroutine to exit, after which the subscription channel is closed. It is safe to call multiple times with the same ID.