eventbus

package
v0.0.0-...-fd774ef Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Event

type Event struct {
	// contains filtered or unexported fields
}

func NewErrorEvent

func NewErrorEvent(t EventType, err error) (Event, error)

func NewEvent

func NewEvent(t EventType, data interface{}) (Event, error)

func (Event) Decode

func (e Event) Decode(receiver interface{}) error

func (Event) String

func (e Event) String() string

String satisfies the stringer interface

func (Event) Type

func (e Event) Type() EventType

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

func (e *EventBus) Dispatch(event Event, topics ...Topic)

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

func (e *EventBus) Shutdown(ctx context.Context) error

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

func (e *EventBus) Unsubscribe(c chan Event, done chan struct{})

Unsubscribe removes the subscriber from receiving any more events and closes all its channels

type EventDispatcher

type EventDispatcher interface {
	Dispatch(e Event, t ...Topic)
}

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

type Topic

type Topic string

Topic creates a group of subscribers that only receive events published to that channel

func OnErrorTopic

func OnErrorTopic() Topic

Jump to

Keyboard shortcuts

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