Documentation ¶
Index ¶
- func SetLogFile(path string)
- type Conductor
- func NewConductorWithCtx[T any](conductor Conductor[T], ctx context.Context) Conductor[T]
- func Simple[T any]() Conductor[T]
- func SimpleFromContext[T any](parent context.Context) Conductor[T]
- func Tagged[T any]() Conductor[T]
- func TaggedFromContext[T any](parent context.Context) Conductor[T]
- func TaggedFromSimple[T any](s Conductor[T]) Conductor[T]
- func WithCancel[T any](conductor Conductor[T]) (Conductor[T], context.CancelFunc)
- func WithDeadline[T any](conductor Conductor[T], deadline time.Time) (Conductor[T], context.CancelFunc)
- func WithTag[T any](conductor Conductor[T], tag string, discriminator ...any) Conductor[T]
- func WithTimeout[T any](conductor Conductor[T], interval time.Duration) (Conductor[T], context.CancelFunc)
- type Notifier
- type Policy
- type Sender
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetLogFile ¶
func SetLogFile(path string)
SetLogFile explicitly sets the file where to log the inner workings of the library. Panics if called more than once.
Types ¶
type Conductor ¶
type Conductor[T any] interface { // Cmd returns a channel where to listen to for commands. Is the analogous of // [context.Context.Done]. Cmd() <-chan T // WithContext assigns the given [context.Context] to the [Conductor], replacing // the one currently assigned to it. WithContext(context.Context) Conductor[T] // WithContextPolicy attaches a [Policy] to the given [Conductor]. WithContextPolicy(Policy[T]) Conductor[T] context.Context }
Conductor is a type useful to convey commands (represented by the generic type T) across API and goroutine boundaries. It can be thought as a generalization of as context.Context and, as such, it also implements the context.Context interface.
func NewConductorWithCtx ¶
NewConductorWithCtx creates a new conductor that hinerits the features of the given one, but replaces the inner context.Context. NOTE: the conductor must be non-nil, or the function will panic.
func SimpleFromContext ¶
SimpleFromContext creates a Simple Conductor from a given context.Context.
func TaggedFromContext ¶
TaggedFromContext creates a Tagged Conductor from a given context.Context.
func TaggedFromSimple ¶
TaggedFromSimple transforms a Simple Conductor in a Tagged one.
func WithCancel ¶
func WithCancel[T any](conductor Conductor[T]) (Conductor[T], context.CancelFunc)
WithCancel mimics what context.WithCancel does, but for a Conductor. It returns a *copy* of the given conductor, that behaves as a context subject to a cancel function returned as second value of the output.
func WithDeadline ¶
func WithDeadline[T any](conductor Conductor[T], deadline time.Time) (Conductor[T], context.CancelFunc)
WithDeadline mimics what context.WithCancel does, but for a Conductor. It returns a *copy* of the given conductor, that behaves as a context subject to a cancel function returned as second value of the output, and that will be cancelled at the given deadline.
func WithTimeout ¶
func WithTimeout[T any](conductor Conductor[T], interval time.Duration) (Conductor[T], context.CancelFunc)
WithTimeout mimics what context.WithCancel does, but for a Conductor. It returns a *copy* of the given conductor, that behaves as a context subject to a cancel function returned as second value of the output, and that will be cancelled after the given interval.
type Notifier ¶
Notifier is the return type of the Notify function.
type Policy ¶
type Policy[T any] interface { // Decide gets invoked when the [Conductor] is canceled. Decide(args ...any) (T, bool) }
Policy is invoked when the Conductor gets cancelled, in the sense of context (i.e. being it a context.Context, it can be canceled in the usual way). When this happens, a Policy is a way to decide what to do with all the listeners loaded in a Conductor.
func ConstantPolicy ¶
ConstantPolicy creates a Policy that fires the given command to all the listeners.
Example ¶
simple, cancel := WithCancel(Simple[string]()) simple.WithContextPolicy(ConstantPolicy("ciao")) lis := simple.Cmd() go cancel() cmd := <-lis fmt.Println(cmd)
Output: ciao
func SetPolicy ¶
SetPolicy creates a Policy that uses the provided map to conditionally fire the command found at the associated identifier in the map. It is to be used only with a Tagged Conductor. If attached to a Simple, it is a noop.
Example ¶
tagged, cancel := WithCancel(Tagged[string]()) tagged.WithContextPolicy(SetPolicy(map[any]string{ "first": "ciao", "second": "miao", "third": "bau", })) lisFirst := WithTag(tagged, "first").Cmd() lisSecond := WithTag(tagged, "second").Cmd() lisThird := WithTag(tagged, "third").Cmd() go cancel() loop: for { select { case cmd := <-lisFirst: fmt.Println(cmd) case cmd := <-lisSecond: fmt.Println(cmd) case cmd := <-lisThird: fmt.Println(cmd) case <-time.After(successTimeout): break loop } }
Output: ciao miao bau
type Sender ¶
type Sender[T any] func(cmd T)
Sender is the return type of the Send function.
func Send ¶
Send may be used on a Conductor to create a function to send a command to the interested listeners. It accepts a variadic amount of arguments to accommodate custom behavior, depending on the specific instance of a Conductor it acts on.
Example (Simple) ¶
simple := SimpleFromContext[string](context.Background()) lis := simple.Cmd() go Send(simple)("ciao") cmd := <-lis fmt.Println(cmd)
Output: ciao
Example (Tagged) ¶
tagged := TaggedFromContext[string](context.Background()) lisFirst := WithTag(tagged, "first").Cmd() lisSecond := WithTag(tagged, "second").Cmd() lisThird := WithTag(tagged, "third").Cmd() tagged, cancel := WithCancel(tagged) feedback := make(chan struct{}, 3) go func() { Send(tagged, "first")("ciao") Send(tagged, "second")("miao") Send(tagged, "third")("bau") <-feedback <-feedback <-feedback cancel() }() loop: for { select { case cmd := <-lisFirst: fmt.Println(cmd) feedback <- struct{}{} case cmd := <-lisSecond: fmt.Println(cmd) feedback <- struct{}{} case cmd := <-lisThird: fmt.Println(cmd) feedback <- struct{}{} case <-tagged.Done(): break loop } }
Output: ciao miao bau