bus

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithBufferSize

func WithBufferSize(s int) option

WithBufferSize is used to set the buffer size of each queue each subscriber gets a channel that its listening on without a high enough buffer size the channel will stall

func WithWorkers

func WithWorkers(w int) option

WithWorkers sets the amount of workers that will be used

Types

type Bus

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

Bus is the main structure used to keep track of all subscribers

func New

func New(opts ...option) *Bus

New instanciates a new instance of Bus

func (*Bus) Publish

func (b *Bus) Publish(e *Event) error

Publish a new event

func (*Bus) Start

func (b *Bus) Start(ctx context.Context)

Start Bus by starting the workers

func (*Bus) Stop

func (b *Bus) Stop(ctx context.Context)

Stop the Bus gracefully by waiting for workers to exit

func (*Bus) Subscribe

func (b *Bus) Subscribe(e EventType, h EventHandler)

Subscribe adds a handler to listen for a specific event type

type Event

type Event struct {
	Type EventType
	Data interface{}
}

Event is the main type that gets sent thr9ough the bus it is up to the callers to validate the data when received

type EventHandler

type EventHandler interface {
	// This function will be called when an event is received
	// that the handler is subscribed to
	Handle(ctx context.Context, e *Event)
	// On Shutfown of the bus all Stop functyions will be called
	// NOTE: Stop should be idempotent as it will be called once per
	// subscribed event. so if a handler is subscribed to more than one event it
	// will be called multiple times
	Stop(ctx context.Context)
}

EventHandler is an interface that callers can use to make use of the bus

type EventType

type EventType int

EventType is the identifier for the type of event you are dealing with

Jump to

Keyboard shortcuts

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