keyed

package
v0.18.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogExitedCallback added in v0.15.2

func NewLogExitedCallback[T comparable](le *logrus.Entry) func(key string, routine Routine, data T, err error)

NewLogExitedCallback returns a ExitedCb which logs when a controller exited.

Types

type KeyWithData added in v0.15.0

type KeyWithData[T comparable] struct {
	// Key is the key.
	Key string
	// Data is the associated data.
	Data T
}

KeyWithData is a key with associated data.

type Keyed

type Keyed[T comparable] struct {
	// contains filtered or unexported fields
}

Keyed manages a set of goroutines with associated Keys.

func NewKeyed

func NewKeyed[T comparable](
	ctorCb func(key string) (Routine, T),
	opts ...Option[T],
) *Keyed[T]

NewKeyed constructs a new Keyed execution manager. Note: routines won't start until SetContext is called. exitedCb is called after a routine exits unexpectedly.

func NewKeyedWithLogger added in v0.15.2

func NewKeyedWithLogger[T comparable](
	ctorCb func(key string) (Routine, T),
	le *logrus.Entry,
) *Keyed[T]

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. exitedCb is called after a routine exits unexpectedly.

func (*Keyed[T]) GetKey added in v0.15.0

func (k *Keyed[T]) GetKey(key string) (Routine, T)

GetKey returns the routine for the given key. Note: this is an instantaneous snapshot.

func (*Keyed[T]) GetKeys added in v0.15.0

func (k *Keyed[T]) GetKeys() []string

GetKeys returns the list of keys registered with the Keyed instance. Note: this is an instantaneous snapshot.

func (*Keyed[T]) GetKeysWithData added in v0.15.0

func (k *Keyed[T]) GetKeysWithData() []KeyWithData[T]

GetKeysWithData returns the keys and the data for the keys. Note: this is an instantaneous snapshot.

func (*Keyed[T]) RemoveKey added in v0.15.0

func (k *Keyed[T]) RemoveKey(key string) bool

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

func (*Keyed[T]) ResetRoutine added in v0.15.0

func (k *Keyed[T]) ResetRoutine(key string, conds ...func(T) 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 (*Keyed[T]) SetContext

func (k *Keyed[T]) 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[T]) SetKey added in v0.15.0

func (k *Keyed[T]) SetKey(key string, restart bool) bool

SetKey inserts the given key into the set, if it doesn't already exist. If restart=true, restarts if routines is currently in the failed state. Returns if it existed already or not.

func (*Keyed[T]) SyncKeys

func (k *Keyed[T]) SyncKeys(keys []string, restart bool)

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

type KeyedRef added in v0.15.0

type KeyedRef[T comparable] struct {
	// contains filtered or unexported fields
}

KeyedRef is a reference to a key.

func (*KeyedRef[T]) Release added in v0.15.0

func (k *KeyedRef[T]) Release()

Release releases the reference.

type KeyedRefCount added in v0.15.0

type KeyedRefCount[T comparable] struct {
	// contains filtered or unexported fields
}

KeyedRefCount manages a list of running routines with reference counts.

func NewKeyedRefCount added in v0.15.0

func NewKeyedRefCount[T comparable](
	ctorCb func(key string) (Routine, T),
	opts ...Option[T],
) *KeyedRefCount[T]

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

func NewKeyedRefCountWithLogger added in v0.15.2

func NewKeyedRefCountWithLogger[T comparable](
	ctorCb func(key string) (Routine, T),
	le *logrus.Entry,
) *KeyedRefCount[T]

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[T]) AddKeyRef added in v0.15.0

func (k *KeyedRefCount[T]) AddKeyRef(key string) (ref *KeyedRef[T], existed bool)

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

func (*KeyedRefCount[T]) GetKey added in v0.15.0

func (k *KeyedRefCount[T]) GetKey(key string) (Routine, T)

GetKey returns the routine for the given key. Note: this is an instantaneous snapshot.

func (*KeyedRefCount[T]) GetKeys added in v0.15.0

func (k *KeyedRefCount[T]) GetKeys() []string

GetKeys returns the list of keys registered with the Keyed instance. Note: this is an instantaneous snapshot.

func (*KeyedRefCount[T]) GetKeysWithData added in v0.15.0

func (k *KeyedRefCount[T]) GetKeysWithData() []KeyWithData[T]

GetKeysWithData returns the keys and the data for the keys. Note: this is an instantaneous snapshot.

func (*KeyedRefCount[T]) ResetRoutine added in v0.15.0

func (k *KeyedRefCount[T]) ResetRoutine(key string, conds ...func(T) 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[T]) SetContext added in v0.15.0

func (k *KeyedRefCount[T]) 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 added in v0.18.2

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

Option is an option for a Keyed instance.

func WithExitCb added in v0.18.2

func WithExitCb[T comparable](cb func(key string, routine Routine, data T, err error)) Option[T]

WithExitCb adds a callback after a routine exits.

func WithExitLogger added in v0.18.2

func WithExitLogger[T comparable](le *logrus.Entry) Option[T]

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

func WithReleaseDelay added in v0.18.2

func WithReleaseDelay[T comparable](delay time.Duration) Option[T]

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
JackTT - Gopher 🇻🇳