keyed

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: MIT Imports: 5 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogExitedCallback

func NewLogExitedCallback[K comparable, V any](le *logrus.Entry) func(key K, routine Routine, data V, err error)

NewLogExitedCallback returns a ExitedCb which logs when a controller exited.

Types

type KeyWithData

type KeyWithData[K comparable, V any] struct {
	// Key is the key.
	Key K
	// Data is the value.
	Data V
}

KeyWithData is a key with associated data.

type Keyed

type Keyed[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Keyed manages a set of goroutines with associated Keys.

K is the type of the key. V is the type of the value.

func NewKeyed

func NewKeyed[K comparable, V any](
	ctorCb func(key K) (Routine, V),
	opts ...Option[K, V],
) *Keyed[K, V]

NewKeyed constructs a new Keyed execution manager. Note: routines won't start until SetContext is called.

func NewKeyedWithLogger

func NewKeyedWithLogger[K comparable, V any](
	ctorCb func(key K) (Routine, V),
	le *logrus.Entry,
) *Keyed[K, V]

NewKeyedWithLogger constructs a new keyed instance. Logs when a controller exits without being removed from the Keys set.

Note: routines won't start until SetContext is called.

func (*Keyed[K, V]) GetKey

func (k *Keyed[K, V]) GetKey(key K) (V, bool)

GetKey returns the value for the given key and existed.

func (*Keyed[K, V]) GetKeys

func (k *Keyed[K, V]) GetKeys() []K

GetKeys returns the list of keys registered with the Keyed instance.

func (*Keyed[K, V]) GetKeysWithData

func (k *Keyed[K, V]) GetKeysWithData() []KeyWithData[K, V]

GetKeysWithData returns the keys and the data for the keys.

func (*Keyed[K, V]) RemoveKey

func (k *Keyed[K, V]) RemoveKey(key K) bool

RemoveKey removes the given key from the set, if it exists. Returns if it existed.

func (*Keyed[K, V]) ResetRoutine

func (k *Keyed[K, V]) ResetRoutine(key K, conds ...func(V) bool) (existed bool, reset bool)

ResetRoutine resets the given routine after checking the condition functions. If any of the conds functions return true, resets the instance.

Resetting the instance constructs a new Routine and data with the constructor. Note: this will overwrite the existing Data, if present! In most cases RestartRoutine is actually what you want.

If len(conds) == 0, always resets the given key.

func (*Keyed[K, V]) RestartRoutine

func (k *Keyed[K, V]) RestartRoutine(key K, conds ...func(V) bool) (existed bool, reset bool)

RestartRoutine restarts the given routine after checking the condition functions. If any return true, and the routine is running, restarts the instance.

If len(conds) == 0, always resets the given key.

func (*Keyed[K, V]) SetContext

func (k *Keyed[K, V]) SetContext(ctx context.Context, restart bool)

SetContext updates the root context, restarting all running routines. If ctx == nil, stops all routines. if restart is true, all errored routines also restart

func (*Keyed[K, V]) SetKey

func (k *Keyed[K, V]) SetKey(key K, start bool) (V, bool)

SetKey inserts the given key into the set, if it doesn't already exist. If start=true, restarts the routine from any stopped or failed state. Returns if it existed already or not.

func (*Keyed[K, V]) SyncKeys

func (k *Keyed[K, V]) SyncKeys(keys []K, restart bool)

SyncKeys synchronizes the list of running routines with the given list. If restart=true, restarts any routines in the failed state.

type KeyedRef

type KeyedRef[K comparable, V any] struct {
	// contains filtered or unexported fields
}

KeyedRef is a reference to a key.

func (*KeyedRef[K, V]) Release

func (k *KeyedRef[K, V]) Release()

Release releases the reference.

type KeyedRefCount

type KeyedRefCount[K comparable, V any] struct {
	// contains filtered or unexported fields
}

KeyedRefCount manages a list of running routines with reference counts.

func NewKeyedRefCount

func NewKeyedRefCount[K comparable, V any](
	ctorCb func(key K) (Routine, V),
	opts ...Option[K, V],
) *KeyedRefCount[K, V]

NewKeyedRefCount constructs a new Keyed execution manager with reference counting. Note: routines won't start until SetContext is called.

func NewKeyedRefCountWithLogger

func NewKeyedRefCountWithLogger[K comparable, V any](
	ctorCb func(key K) (Routine, V),
	le *logrus.Entry,
) *KeyedRefCount[K, V]

NewKeyedRefCountWithLogger constructs a new Keyed execution manager with reference counting. Logs when a controller exits without being removed from the Keys set. Note: routines won't start until SetContext is called.

func (*KeyedRefCount[K, V]) AddKeyRef

func (k *KeyedRefCount[K, V]) AddKeyRef(key K) (ref *KeyedRef[K, V], data V, existed bool)

AddKeyRef adds a reference to the given key. Returns if the key already existed or not.

func (*KeyedRefCount[K, V]) GetKey

func (k *KeyedRefCount[K, V]) GetKey(key K) (V, bool)

GetKey returns the value for the given key and if it existed.

func (*KeyedRefCount[K, V]) GetKeys

func (k *KeyedRefCount[K, V]) GetKeys() []K

GetKeys returns the list of keys registered with the Keyed instance.

func (*KeyedRefCount[K, V]) GetKeysWithData

func (k *KeyedRefCount[K, V]) GetKeysWithData() []KeyWithData[K, V]

GetKeysWithData returns the keys and the data for the keys.

func (*KeyedRefCount[K, V]) ResetRoutine

func (k *KeyedRefCount[K, V]) ResetRoutine(key K, conds ...func(V) bool) (existed bool, reset bool)

ResetRoutine resets the given routine after checking the condition functions. If any return true, resets the instance.

If len(conds) == 0, always resets the given key.

func (*KeyedRefCount[K, V]) RestartRoutine

func (k *KeyedRefCount[K, V]) RestartRoutine(key K, conds ...func(V) bool) (existed bool, reset bool)

RestartRoutine restarts the given routine after checking the condition functions. If any return true, and the routine is running, restarts the instance.

If len(conds) == 0, always resets the given key.

func (*KeyedRefCount[K, V]) SetContext

func (k *KeyedRefCount[K, V]) SetContext(ctx context.Context, restart bool)

SetContext updates the root context, restarting all running routines. if restart is true, all errored routines also restart

type Option

type Option[K comparable, V any] interface {
	// ApplyToKeyed applies the option to the Keyed.
	ApplyToKeyed(k *Keyed[K, V])
}

Option is an option for a Keyed instance.

func WithExitCb

func WithExitCb[K comparable, V any](cb func(key K, routine Routine, data V, err error)) Option[K, V]

WithExitCb adds a callback after a routine exits.

func WithExitLogger

func WithExitLogger[K comparable, V any](le *logrus.Entry) Option[K, V]

WithExitLogger adds a exited callback which logs information about the exit.

func WithReleaseDelay

func WithReleaseDelay[K comparable, V any](delay time.Duration) Option[K, V]

WithReleaseDelay adds a delay after removing a key before canceling the routine.

type Routine

type Routine func(ctx context.Context) error

Routine is a function called as a goroutine. If nil is returned, exits cleanly permanently. If an error is returned, can be restarted later.

Jump to

Keyboard shortcuts

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