Documentation
¶
Overview ¶
Package dispatch routes evaluation results to named handler functions. Handlers are registered immutably on a Dispatcher, then composed into [Plan]s that specify which handlers run and under what conditions.
rules: github.com/rhyselsmore/anyexpr/rules
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDuplicateHandler is returned when two handlers share the // same name. ErrDuplicateHandler = errors.New("dispatch: duplicate handler") // ErrUnknownHandler is returned when a plan references a handler // name not registered on the dispatcher. ErrUnknownHandler = errors.New("dispatch: unknown handler") )
Functions ¶
This section is empty.
Types ¶
type Dispatched ¶
type Dispatched struct {
// Handler is the name of the handler.
Handler string
// MatchedExpr is the When expression that triggered this handler.
MatchedExpr string
// Duration is how long the handler took to execute.
Duration time.Duration
// Error is non-nil if the handler returned an error or panicked.
Error error
// Panicked is true if the handler panicked (Error will contain
// the recovered value).
Panicked bool
}
Dispatched records a single handler invocation.
type Dispatcher ¶
Dispatcher is an immutable registry of named handlers. Build plans from it to control which handlers run and when.
- E is the environment type (e.g. Email).
- A is the actions struct (e.g. EmailActions).
func New ¶
func New[E any, A any](opts ...DispatcherOpt[E, A]) (*Dispatcher[E, A], error)
New creates an immutable Dispatcher with the given handlers.
func (*Dispatcher[E, A]) Describe ¶
func (d *Dispatcher[E, A]) Describe() []HandlerInfo
Describe returns metadata for all registered handlers, in registration order. Useful for introspection — an agent can discover what handlers are available.
type DispatcherOpt ¶
DispatcherOpt configures a Dispatcher.
func Handle ¶
func Handle[E any, A any](name string, fn HandlerFunc[E, A], opts ...HandleOpt) DispatcherOpt[E, A]
Handle registers a named handler function with optional description.
func WithLogger ¶
func WithLogger[E any, A any](l *slog.Logger) DispatcherOpt[E, A]
WithLogger sets a structured logger for dispatch events.
type HandleOpt ¶
type HandleOpt func(*handleConfig)
HandleOpt configures a handler registration.
func WithDescription ¶
WithDescription sets a human-readable description for a handler. Surfaced via Dispatcher.Describe for introspection.
type HandlerFunc ¶
HandlerFunc is the function signature for dispatch handlers.
type HandlerInfo ¶
type HandlerInfo struct {
// Name is the handler's registered name.
Name string
// Description is the human-readable description, if provided.
Description string
}
HandlerInfo describes a registered handler.
type Plan ¶
Plan is an immutable, compiled dispatch plan. Created from a Dispatcher via Plan(). Safe for concurrent use.
func (*Plan[E, A]) Describe ¶
Describe returns metadata for each handler in the plan, in execution order.
type PlanInfo ¶
type PlanInfo struct {
// Handler is the handler name.
Handler string
// Whens lists the gating expressions for this handler. Empty
// means always runs.
Whens []string
}
PlanInfo describes a handler entry within a plan.
type PlanOpt ¶
PlanOpt configures a Plan.
func Gate ¶
Gate sets a top-level expression that must pass before any handlers execute. If the gate expression returns false, no handlers run.
type Result ¶
type Result[E any, A any] struct { // Plan is the name of the plan that was executed. Plan string // Evaluation is the original evaluation that was dispatched. Evaluation *rules.Evaluation[E, A] // Dispatched lists every handler that was invoked, in execution // order, with timing and error information. Dispatched []Dispatched // Duration is the total dispatch time (all handlers). Duration time.Duration // Gated is true if a gate expression was evaluated. Gated bool // GateExpr is the gate expression string, if Gated is true. GateExpr string // GatePassed is true if the gate expression passed. If false, // no handlers were invoked. GatePassed bool }
Result is the outcome of a dispatch run.
type Strategy ¶
type Strategy int
Strategy controls how the dispatcher executes matching handlers.
const ( // AllContinue runs all matching handlers. Errors are collected // and returned together. All handlers run regardless of failures. AllContinue Strategy = iota // AllHaltOnError runs matching handlers in registration order. // Stops on the first error. AllHaltOnError // FirstMatch runs only the first matching handler and stops. FirstMatch )