eventbus

package
v0.0.0-...-06831c0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

# pkg/eventbus

Package handles event dispatching and handler registration

## Event

Event (in context of eventbus package) contains of resource type, event type and a matcher.

### Resource type

Resource types are identifiers of internal resources, like user, role, request, ...

### Event type

Type describes direct or indirect user action or some other event in the system.

#### Deferred events

Deferred events (onTimestamp and onInterval) are executed by scheduler package service.

#### Manual events

Manual events (onManual) are a special case.
These events are not (nor should be) dispatched directly or through event-bus.

We use it to route requests and pack attributes from the API to the Corredor service

### Event matcher

Matcher is a function on event that helps filtering fired events.
It decides (returns true or false) if fired event is a match for any of a registered handlers

Bus has basic internal filtering for resource & event type.
Other constraints are passed to event's matcher function.

.Matcher will receive 2+ string parameters:
 - name
 - operator
 - zero or more values

It's matcher responsibility to handle contents of name, operator and values parameters.

Constraint checker procedure will call matcher for each constraint.
All constraints must match.
First non-match will break constraint checking procedure.


## Handler

Handler is combination of (event matching) rules/constraints and handler callback function.
Function is called when a event compatible with registered rules & constraints is fired.

Handler can respond to multiple resource/event combinations.

### Constraints

Matcher function is called multiple times, once per each handler constraint.
If any of the calls result in a non-match, check

Handler without any constraints is considered a match.

### Weight

Weight controls order of execution.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedName = fmt.Errorf("constraint name not supported")
View Source
var ErrUnsupportedOp = fmt.Errorf("operator not supported")

Functions

func MatchFirst

func MatchFirst(checks ...func() bool) bool

func New

func New() *eventbus

func NewHandler

func NewHandler(h HandlerFn, ops ...HandlerRegOp) *handler

func Service

func Service() *eventbus

Service returns global event bus service

func Set

func Set(eb *eventbus)

Types

type ConstraintMatcher

type ConstraintMatcher interface {
	Name() string
	Values() []string
	Match(value string) bool
}

func ConstraintMaker

func ConstraintMaker(name, op string, vv ...string) (ConstraintMatcher, error)

Converts raw ConstraintMatcher into one of the ConstraintMatcher handlers

func MustBeEqual

func MustBeEqual(name string, vv ...string) (ConstraintMatcher, error)

func MustBeLike

func MustBeLike(name string, vv ...string) (ConstraintMatcher, error)

func MustMakeConstraint

func MustMakeConstraint(name, op string, vv ...string) ConstraintMatcher

func MustMatch

func MustMatch(name string, vv ...string) (ConstraintMatcher, error)

func MustNotBeEqual

func MustNotBeEqual(name string, vv ...string) (ConstraintMatcher, error)

func MustNotBeLike

func MustNotBeLike(name string, vv ...string) (ConstraintMatcher, error)

func MustNotMatch

func MustNotMatch(name string, vv ...string) (ConstraintMatcher, error)

type Event

type Event interface {
	// ResourceType from resource that fired the event
	ResourceType() string

	// EventType returns type of event fired
	EventType() string

	// Match tests if given constraints match
	// event's internal values
	Match(ConstraintMatcher) bool
}

type HandlerFn

type HandlerFn func(ctx context.Context, ev Event) error

type HandlerRegOp

type HandlerRegOp func(t *handler)

func Constraint

func Constraint(c ConstraintMatcher) HandlerRegOp

func For

func For(rr ...string) HandlerRegOp

func On

func On(ee ...string) HandlerRegOp

func Weight

func Weight(weight int) HandlerRegOp

type HandlerSet

type HandlerSet []*handler

@todo add sorting interface

func (HandlerSet) Len

func (set HandlerSet) Len() int

func (HandlerSet) Less

func (set HandlerSet) Less(i, j int) bool

func (HandlerSet) Swap

func (set HandlerSet) Swap(i, j int)

Jump to

Keyboard shortcuts

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