Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrShutdownTimeout error = fmt.Errorf("eventbus: context timeout or cancelled before all subscribers exited")
ErrShutdownTimeout is returned if calling eventbus.Shutdown(ctx) causes the context to timeout before all subscribers have exited
Functions ¶
This section is empty.
Types ¶
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus dispatches events to all subcribers on one or more topics. If no topic is set, a default channel is created that dispatches events to every subscriber. Subscribers can use the EventType to filter which events they respond to rather than configuring multiple topics.
func New ¶
func New() *EventBus
New returns a new event bus. A default topic is created, but subscribers may create other topics when they register.
func (*EventBus) Dispatch ¶
Dispatch will send the event to 0 or more topics. All events are broadcast to default topic subscribers, even when other topics may be specified.
func (*EventBus) Shutdown ¶
Shutdown will send the shutdown signal to all subscribers and block until they exit. Best practice is to use a context timeout to prevent shutdown from hanging if a go routine cannot finish processing all events in a reasonable time. Shutdown returns an error if it reaches context timeout or cancel to distinguish from a completely successful shutdown.
func (*EventBus) Subscribe ¶
func (e *EventBus) Subscribe(topics ...Topic) (chan Event, ShutdownFunc)
Subscribe will register a subscriber to 0 or more topics. If no topic is defined, the subscriber will added to the default channel and receive all events published on any channel. The default channel acts like a multicast channel so events published on other topics also are received by default channel subscribers.
The subscriber receives a channel to receive events and a shutdown function. The event channel will be closed when the event bus is shut down. Subscribers should detect a closed event channel and interpret that as a shutdown signal. When the channel is closed, subscribers should wait for any existing go routines to exit and then call the ShutdownFunc to indicate that the subscriber has finished all work. It is safe to defer the ShutdownFunc at the top of your event handler to ensure that the event bus knows when your subscriber has exited.
func (*EventBus) Unsubscribe ¶
Unsubscribe removes the subscriber from receiving any more events and closes all its channels
type EventDispatcher ¶
EventDispatcher is an interface for functions that only emit events to the bus
type EventType ¶
type EventType string
EventType represents the type of event being passed on the bus. It allows handlers receiving the event to to properly unmarshal the data or decide if processing is required.
type ShutdownFunc ¶
type ShutdownFunc func()
ShutdownFunc tells the event bus that this subscriber has finished the shutdown process and it is safe to exit