Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventBus ¶
type EventBus[T any] struct { // contains filtered or unexported fields }
EventBus is an n-to-n pub-sub structure to send and receive event messages between services.
func New ¶
New returns a new instance of EventBus with the given type T as message type.
Optionally, you can pass a custon buffer size used for the event subscription channels. It defines the number of messages which can be stored in each subscription channel without blocking the publishing go routine until the message has been picked up by the subscribing go routine. The default value when not passed is 100. When the EventBus might experience high traffic, it is recommendet to allocate larger buffer sized to avoid blocking the publishing go routine.
func (*EventBus[T]) Publish ¶
func (t *EventBus[T]) Publish(v T)
Publish sends the passed message to each subscription to the EventBus.
If no one subscribed to the EventBus, the message will be discarded.
func (*EventBus[T]) Subscribe ¶
func (t *EventBus[T]) Subscribe() (<-chan T, func())
Subscribe returns a channel receiving messages published to the EventBus as well as a function to unsubscribe.
When the unsubscribe function is called, the channel will be closed and the subscription will be removed from the EventBus.
func (*EventBus[T]) SubscribeFunc ¶
func (t *EventBus[T]) SubscribeFunc(f func(T)) func()
SubscribeFunc is a wrapper for Subscribe which calls the given function f when a message has been received passing the received message.
The returned function unsubscribes from the EventBus.