Documentation
¶
Overview ¶
Package event is a small in-process hub. Publishing never blocks: each subscriber has a bounded queue and an event that does not fit is dropped for that subscriber and counted, so a slow subscriber costs the publisher nothing and the other subscribers nothing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus fans events out to its subscribers. A nil Bus is valid and drops everything.
func (*Bus) Close ¶
func (b *Bus) Close()
Close ends all subscriptions. Events still queued can be received, then the channels report closed. It is safe to call more than once.
func (*Bus) Publish ¶
Publish hands an event to every subscriber with room for it. After Close it does nothing.
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(queue int) *Subscription
Subscribe returns a subscription with a queue of the given size, at least one. On a closed Bus its channel is already closed.
type Event ¶
type Event struct {
ID string `json:"id"`
Type string `json:"type"`
Time time.Time `json:"time"`
Data any `json:"data,omitempty"`
}
Event is one thing that happened. ID is unique across restarts, so a receiver can use it to ignore a delivery it has seen. Data must not change after publishing: it is read by subscribers later, on their goroutines.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is one subscriber's queue.
func (*Subscription) C ¶
func (s *Subscription) C() <-chan Event
C returns the queue. It is closed when the subscription or the Bus closes.
func (*Subscription) Close ¶
func (s *Subscription) Close()
Close ends the subscription. It is safe to call more than once and after the Bus closed.
func (*Subscription) Dropped ¶
func (s *Subscription) Dropped() uint64
Dropped returns how many events did not fit in the queue.