bus

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 13 Imported by: 63

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDirectiveDisposed is returned when the directive was unexpectedly disposed.
	ErrDirectiveDisposed = errors.New("directive disposed unexpectedly")
)

Functions

func ExecCollectValues

func ExecCollectValues[T directive.Value](
	ctx context.Context,
	bus Bus,
	dir directive.Directive,
	waitOne bool,
	valDisposeCb func(),
) ([]T, directive.Instance, directive.Reference, error)

ExecCollectValues collects one or more values while ctx is active and directive is not idle.

Returns vals, ref, nil if the directive is idle. Returns err if any resolver returns an error. valDisposeCb is called if any of the values are no longer valid. valDisposeCb might be called multiple times. If err != nil, ref == nil. If waitOne=true, waits for at least one value before returning.

func ExecOneOff

func ExecOneOff(
	ctx context.Context,
	bus Bus,
	dir directive.Directive,
	returnIfIdle bool,
	valDisposeCallback func(),
) (directive.AttachedValue, directive.Instance, directive.Reference, error)

ExecOneOff executes a one-off directive.

If returnIfIdle is set, returns nil, nil, nil if idle. If any resolvers return an error, returns that error. valDisposeCb is called if the value is no longer valid. valDisposeCb might be called multiple times. If err != nil, ref == nil.

func ExecOneOffWatch added in v0.21.0

ExecOneOffWatch executes a one-off directive and watches for changes. Continues to watch for changes until the directive is released.

func ExecOneOffWatchCh added in v0.8.7

func ExecOneOffWatchCh(
	b Bus,
	dir directive.Directive,
) (<-chan directive.AttachedValue, directive.Instance, directive.Reference, error)

ExecOneOffWatchCh executes a one-off directive and watches for changes. Returns a channel with size 1 which will hold the latest value. Continues to watch for changes until the directive is released.

func ExecOneOffWatchRoutine added in v0.22.4

func ExecOneOffWatchRoutine[T directive.Value](
	b Bus,
	dir directive.Directive,
	cb func(ctx context.Context, value T) error,
) (*routine.RoutineContainer, directive.Instance, directive.Reference, error)

ExecOneOffWatchRoutine executes a one-off directive and watches for changes.

The routine will not start until SetContext is called. The routine will be restarted when the value changes. If the routine returns nil or any error, the function returns that value. If the routine context is canceled, return context.Canceled. If the directive is disposed, the routine will return ErrDirectiveDisposed.

func ExecOneOffWithFilter added in v0.20.8

func ExecOneOffWithFilter(
	ctx context.Context,
	bus Bus,
	dir directive.Directive,
	returnIfIdle bool,
	valDisposeCallback func(),
	filterCb func(val directive.AttachedValue) (bool, error),
) (directive.AttachedValue, directive.Instance, directive.Reference, error)

ExecOneOffWithFilter executes a one-off directive with a filter cb.

Waits until the callback returns true before returning a value. If returnIfIdle is set, returns nil, nil, nil if idle. If any resolvers return an error, returns that error. valDisposeCb is called if the value is no longer valid. valDisposeCb might be called multiple times. If err != nil, ref == nil.

func ExecWaitValue added in v0.15.0

func ExecWaitValue[T directive.Value](
	ctx context.Context,
	b Bus,
	dir directive.Directive,
	returnIfIdle bool,
	valDisposeCallback func(),
	checkCb func(val T) (bool, error),
) (T, directive.Instance, directive.Reference, error)

ExecWaitValue executes a directive and waits for a value matching the cb.

valDisposeCallback can be nil, will be called if the value is disposed. If returnIfIdle is set, returns nil, nil, nil if idle. If cb returns true, nil, returns the value. If checkCb is nil, returns first value.

func NewCallbackHandler

func NewCallbackHandler(
	valCb func(directive.AttachedValue),
	removedCb func(directive.AttachedValue),
	disposeCb func(),
) directive.ReferenceHandler

NewCallbackHandler wraps callback functions into a handler object.

func NewPassThruHandler

func NewPassThruHandler(
	passthru directive.ResolverHandler,
	disposeCb func(),
) directive.ReferenceHandler

NewPassThruHandler builds a new pass-through handler.

Types

type Bus

type Bus interface {
	// Controller is the directive controller.
	directive.Controller

	// GetControllers returns a list of all currently active controllers.
	GetControllers() []controller.Controller

	// AddController adds a controller to the bus and calls Execute().
	// The controller will exit if ctx is canceled.
	// Returns a release function for the controller reference.
	// The controller will receive directive callbacks until removed.
	// Any fatal error in the controller is written to cb.
	// If the controller is released, cb will be called with nil.
	// cb can be nil
	AddController(ctx context.Context, ctrl controller.Controller, cb func(exitErr error)) (func(), error)

	// ExecuteController adds a controller to the bus and calls Execute().
	// The controller will exit if ctx is canceled.
	// Any fatal error in the controller is returned.
	// The controller will receive directive callbacks.
	// If this function returns nil, call RemoveController to remove the controller.
	ExecuteController(context.Context, controller.Controller) error

	// RemoveController removes the controller from the bus.
	// The controller will no longer receive callbacks.
	// Note: this might not cancel the Execute() context automatically.
	RemoveController(controller.Controller)
}

Bus manages running controllers. It has an attached directive controller, which is used to build declarative state requests between controllers.

type BusController added in v0.15.1

type BusController[T config.Config] struct {
	// contains filtered or unexported fields
}

BusController implements a base Controller with an attached Bus for directives. It has stubs for the functions implementing all required interfaces. Usually you should extend this type, overriding functions as needed.

func NewBusController added in v0.15.1

func NewBusController[T config.Config](
	le *logrus.Entry,
	b Bus,
	conf T,
	controllerID string,
	controllerVersion semver.Version,
	controllerDescrip string,
) *BusController[T]

NewBusController constructs a new Controller with details.

func (*BusController[T]) Close added in v0.15.1

func (c *BusController[T]) Close() error

Close releases any resources used by the controller.

func (*BusController[T]) Execute added in v0.15.1

func (c *BusController[T]) Execute(ctx context.Context) error

Execute executes the given controller.

func (*BusController[T]) GetBus added in v0.15.1

func (c *BusController[T]) GetBus() Bus

GetBus returns the bus.

func (*BusController[T]) GetConfig added in v0.15.1

func (c *BusController[T]) GetConfig() T

GetConfig returns the config. Note: don't modify this object!

func (*BusController[T]) GetControllerInfo added in v0.15.1

func (c *BusController[T]) GetControllerInfo() *controller.Info

GetControllerInfo returns information about the controller.

func (*BusController[T]) GetLogger added in v0.15.1

func (c *BusController[T]) GetLogger() *logrus.Entry

GetLogger returns the root logger.

func (*BusController[T]) HandleDirective added in v0.15.1

func (c *BusController[T]) HandleDirective(ctx context.Context, di directive.Instance) ([]directive.Resolver, error)

HandleDirective asks if the handler can resolve the directive.

type BusFactory added in v0.15.0

type BusFactory[T config.Config, C controller.Controller] struct {
	// contains filtered or unexported fields
}

BusFactory implements Factory with an attached Bus for directives.

func NewBusControllerFactory added in v0.15.1

func NewBusControllerFactory[T config.Config, C controller.Controller](
	b Bus,
	configID string,
	controllerID string,
	version semver.Version,
	controllerDescription string,
	newConfig func() T,
	newController func(base *BusController[T]) (C, error),
) *BusFactory[T, C]

NewBusControllerFactory builds a Factory for a type wrapping BusController.

func NewBusFactory added in v0.23.2

func NewBusFactory[T config.Config, C controller.Controller](
	b Bus,
	configID string,
	version semver.Version,
	newConfig func() T,
	newController func(le *logrus.Entry, b Bus, conf T) (C, error),
) *BusFactory[T, C]

NewBusFactory constructs a new Factory with details.

func (*BusFactory[T, C]) Construct added in v0.15.0

func (f *BusFactory[T, C]) Construct(cc config.Config, opts controller.ConstructOpts) (controller.Controller, error)

Construct constructs the associated controller given configuration.

func (*BusFactory[T, C]) ConstructConfig added in v0.15.0

func (f *BusFactory[T, C]) ConstructConfig() config.Config

ConstructConfig constructs an instance of the controller configuration.

func (*BusFactory[T, C]) GetConfigID added in v0.15.0

func (f *BusFactory[T, C]) GetConfigID() string

GetConfigID returns the unique config ID for the controller.

func (*BusFactory[T, C]) GetVersion added in v0.15.0

func (f *BusFactory[T, C]) GetVersion() semver.Version

GetVersion returns the version of this controller.

type CallbackHandler

type CallbackHandler struct {
	// contains filtered or unexported fields
}

CallbackHandler is a reference handler that uses function callbacks.

func (*CallbackHandler) HandleInstanceDisposed

func (h *CallbackHandler) HandleInstanceDisposed(directive.Instance)

HandleInstanceDisposed is called when a directive instance is disposed. This will occur if Close() is called on the directive instance.

func (*CallbackHandler) HandleValueAdded

func (h *CallbackHandler) HandleValueAdded(
	_ directive.Instance,
	v directive.AttachedValue,
)

HandleValueAdded is called when a value is added to the directive.

func (*CallbackHandler) HandleValueRemoved

func (h *CallbackHandler) HandleValueRemoved(
	_ directive.Instance,
	v directive.AttachedValue,
)

HandleValueRemoved is called when a value is removed from the directive.

type PassThruHandler

type PassThruHandler struct {
	// contains filtered or unexported fields
}

PassThruHandler is a reference handler that passes through to a resolver handler.

func (*PassThruHandler) HandleInstanceDisposed

func (h *PassThruHandler) HandleInstanceDisposed(di directive.Instance)

HandleInstanceDisposed is called when a directive instance is disposed. This will occur if Close() is called on the directive instance.

func (*PassThruHandler) HandleValueAdded

func (h *PassThruHandler) HandleValueAdded(
	_ directive.Instance,
	v directive.AttachedValue,
)

HandleValueAdded is called when a value is added to the directive.

func (*PassThruHandler) HandleValueRemoved

func (h *PassThruHandler) HandleValueRemoved(
	_ directive.Instance,
	v directive.AttachedValue,
)

HandleValueRemoved is called when a value is removed from the directive.

Directories

Path Synopsis
api

Jump to

Keyboard shortcuts

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