eventdistributor

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 1 Imported by: 2

README

eventdistributor

GoDoc

Internal event distribution package for Go.

The implementation uses signalling channels to notify waiting "readers", which can then "consume" an event.

Events are buffered until all readers have seen them.

The implementation is rather small - feel free to read there for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Distributor

type Distributor[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](options ...Options[T]) *Distributor[T]

New creates a new Distributor with the provided options.

If you don't have any options to set, the zero value of an Distributor is also valid.

func (*Distributor[T]) Submit

func (d *Distributor[T]) Submit(value T) <-chan struct{}

Submit adds an event to the queue, notifying any waiting Readers.

The returned channel is closed when no remaining Readers are able to consume the value - either by Consume() or Unsubscribe().

Submit is thread-safe.

func (*Distributor[T]) Subscribe

func (d *Distributor[T]) Subscribe() Reader[T]

Subscribe creates a new Reader to receive future events from the Distributor.

It is STRONGLY recommended to defer (*Reader[T]).Unsubscribe() immediately after subscribing.

Subscribe is thread-safe.

type Options

type Options[T any] struct {
	// contains filtered or unexported fields
}

Options contains a set of options for Distributor initialization.

The zero value is safe to use.

func (*Options[T]) OnBufsizeChange

func (o *Options[T]) OnBufsizeChange(callback func(size int))

OnBufsizeChange adds a callback to the options that will be called whenever the number of items in the buffer changes.

NOTE: This is typically called during the Distributor's Submit(), Consume(), and Unsubscribe().

func (*Options[T]) OnFullyConsumed

func (o *Options[T]) OnFullyConsumed(callback func(item T))

OnFullyConsumed adds a callback to the options that will be called whenever an item is dropped from the buffer.

NOTE: If there are no active subscribers, the callback will be called *during* the call to (*Distributor[T]).Submit().

func (*Options[T]) OnSubmit

func (o *Options[T]) OnSubmit(callback func(item T))

OnSubmit adds a callback to the options that will be called whenever an item is submitted with (*Distributor[T]).Submit().

In the edge case where an item is immediately ignored because there's no readers, OnSubmit will be called before OnfullyConsumed.

type Reader

type Reader[T any] struct {
	// contains filtered or unexported fields
}

func (*Reader[T]) Consume

func (r *Reader[T]) Consume() T

Consume returns the first event that has not yet been seen by this Reader, marking it as "seen" so that the next call to WaitChan() will require a newer event.

Consume is thread-safe.

func (*Reader[T]) Unsubscribe

func (r *Reader[T]) Unsubscribe()

Unsubscribe de-registers the Reader, freeing any buffered events that may have been kept for it.

If you stop using an Reader and never call Unsubscribe, unread events will slowly accumulate, increasing the memory usage of your program.

Unsubscribe is thread-safe.

func (*Reader[T]) WaitChan

func (r *Reader[T]) WaitChan() <-chan struct{}

WaitChan returns a channel that will be closed once there is an event that this Reader has not yet seen.

WaitChan is thread-safe.

Jump to

Keyboard shortcuts

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