concurrent

package
v2.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSigInactive   = errors.New("signaller inactive")
	ErrSigNoListener = errors.New("no signal listener")
)

Error codes

Functions

This section is empty.

Types

type Listener

type Listener[S any] struct {
	// contains filtered or unexported fields
}

Listener for signals managed by Signaller

func (*Listener[S]) Close

func (l *Listener[S]) Close()

Close listener: This more an announcement than an operation as the channel is not closed immediately. It is possible for the listener to receive some more signals before it actually closes.

func (*Listener[S]) Signal

func (l *Listener[S]) Signal() <-chan Signal[S]

Signal returns the channel from which to read the signal. If the returned signal is nil, no further signals will be received on this listener and the select-loop MUST terminate.

type Signal

type Signal[S any] struct {
	// contains filtered or unexported fields
}

Signal can be any object (intrinsic or custom); it is the responsibility of the sender and the receiver of signals to handle them accordingly.

A signal in this context is a stand-alone unit of information. Its "meaning" does neither depend on other signals nor on their sequence. Its only purpose is to communicate state changes to listeners instead of sharing the state globally via memory reference ("Do not communicate by sharing memory; instead, share memory by communicating." -- https://go.dev/doc/effective_go.html)

func NewSignal

func NewSignal[S any](value S) Signal[S]

NewSignal instantiates a signal,

func (*Signal[S]) Value

func (s *Signal[S]) Value() S

Value returns the value associated with the signal.

type Signaller

type Signaller[S any] struct {
	// contains filtered or unexported fields
}

Signaller dispatches signals to multiple concurrent listeners. The sequence in which listeners are served is stochastic.

In highly concurrent environments with a lot of signals the sequence of signals seen by a listener can vary. This is due to the fact that a signal gets dispatched in a Go routine, so the next signal can be dispatched before a listener got the first one if the second Go routine handles the listener earlier. It is therefore mandatory that received signals from a listener get handled in a Go routine as well to keep latency low. If a listener violates that promise, it got removed from the list.

func NewSignaller

func NewSignaller[S any]() *Signaller[S]

NewSignaller instantiates a new signal manager:

func (*Signaller[S]) Listener

func (s *Signaller[S]) Listener() (*Listener[S], error)

Listener returns a new channel to listen on each time it is called. Function interested in listening should get the channel, start the for/select loop and drop the channel if the loop terminates. Requesting an listener and than not reading from it will block all other listeners of the signaller.

func (*Signaller[S]) Retire

func (s *Signaller[S]) Retire()

Retire a signaller: This will terminate the dispatch loop for signals; no further send operations are supported. A retired signaller cannot be re-activated. Running dispatches will not be interrupted.

func (*Signaller[S]) Send

func (s *Signaller[S]) Send(val S) error

Send a signal to be dispatched to all listeners.

func (*Signaller[S]) SetLatency

func (s *Signaller[S]) SetLatency(d time.Duration)

SetLatency sets the max latency for listener. A listener is removed from the list if it violates this policy.

Jump to

Keyboard shortcuts

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