async

package
v0.0.0-...-d8cb705 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broadcaster

type Broadcaster interface {
	// Send broadcasts the provided message to all registered listeners. This method is non-blocking,
	// and will guarantee that all registered listeners receive the message.
	Send(any)
	// Register registers a channel to receive messages from the broadcaster.
	Register(channel chan any, id string)
	// Deregister deregisters a channel from receiving messages from the broadcaster.
	Deregister(id string)
}

Broadcaster is an interface for broadcasting events to multiple listeners (channels). This is a simple wrapper around go channels to avoid the limitation that channels are only one-to-one pipes. The Broadcaster interface allows for multiple listeners to receive the same message from a single broadcasting channel.

func EventGroup

func EventGroup() (*errgroup.Group, Broadcaster)

func NewBroadcaster

func NewBroadcaster() Broadcaster

type Listener

type Listener interface {
	// Listen will start consuming events and send them to the configured Callback function.
	Listen(ctx context.Context) error
}

func RegisterListener

func RegisterListener[T proto.Message](_ context.Context, broadcaster Broadcaster, options *ListenerOptions[T]) Listener

RegisterListener will create a new Listener which is registered to receive events of the given type from the provided Broadcaster. A subsequent call to Listener.Listen() is required to process events and trigger the provided Callback function.

type ListenerOptions

type ListenerOptions[T proto.Message] struct {
	// Timeout specifies the maximum allowed time between received events.
	//
	// Default: 15 seconds
	Timeout time.Duration
	// Callback is the function that will be called when an event matching the given type is broadcast.
	// The Broadcaster will continue sending events to this Listener until either the Callback function
	// returns don=true or returns a non-nil error.
	Callback func(event T) (done bool, err error)
	// Buffer specifies the buffer capacity of the listener. This is the limit of pending messages the
	// Broadcaster will queue before dropping messages. Your Listener is reponsible for keeping up with
	// the queue to avoid dropped messages.
	//
	// Default: 16
	Buffer int
}

Jump to

Keyboard shortcuts

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