Repository navigation
Expand file tree
/
Copy pathinterfaces.go
More file actions
95 lines (77 loc) · 3.24 KB
/
Copy pathinterfaces.go
File metadata and controls
95 lines (77 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package firehose
import (
"context"
"github.com/emad-elsaid/boolexpr"
"github.com/mitchellh/hashstructure/v2"
)
// EventID computes a hash-based identifier for an event.
func EventID(event any) (uint64, error) {
return hashstructure.Hash(event, hashstructure.FormatV2, nil)
}
// EventSymbols extracts symbols from an event if it implements boolexpr.Symbols.
// Events can embed boolexpr.Symbols directly to provide attribute access for condition evaluation.
// Returns a cached wrapper around the symbols for efficient repeated lookups. If the event
// doesn't implement boolexpr.Symbols, returns an empty SymbolsMap directly (no caching needed).
func EventSymbols(event any) boolexpr.Symbols {
if symbols, ok := event.(boolexpr.Symbols); ok {
return boolexpr.NewCachedSymbols(symbols)
}
return boolexpr.SymbolsMap{}
}
// Source produces events of type T.
type Source[T any] interface {
Start(ctx context.Context, cb Callback[T]) (done <-chan struct{}, err error)
}
// Action transforms input events to output events.
type Action[I, O any] interface {
Process(ctx context.Context, event I, syms boolexpr.Symbols) (O, error)
}
// Destination consumes events of type T.
type Destination[T any] interface {
Send(ctx context.Context, event T) error
}
// Condition evaluates whether an event should be processed by a rule.
// It receives the context, event, and symbols extracted from the event attributes,
// and returns true if the condition is met, false otherwise, along with any error.
type Condition[I any] interface {
Evaluate(ctx context.Context, event I, syms boolexpr.Symbols) (bool, error)
}
// Rule handler that accumulates rules and manages their execution.
type Rule interface {
GetNext() Rule
SetNext(n Rule)
GetPrev() Rule
SetPrev(p Rule)
GetSource() any
Start(ctx context.Context) (<-chan struct{}, error)
SetNextSameSource(n Rule)
SetPrevSameSource(p Rule)
GetNextSameSource() Rule
GetID() string
GetEnvironments() []string
Init(ctx context.Context) error
}
// Callback is a function type that sources use to send events to the
// engine. It takes a context, an event, and a report sink callback.
type Callback[I any] func(context.Context, I, ErrorHandler)
// Runnable represents a rule that can be executed to process events.
type Runnable[I any] interface {
Run(ctx context.Context, event I, syms boolexpr.Symbols) error
NextRunnable() Runnable[I]
}
// Reducer combines an intermediary value with an accumulator to produce a
// new output value. Used by MapReduceRule to reduce mapped events into a
// running result.
type Reducer[M, Out any] interface {
Reduce(ctx context.Context, intermediary M, accumulator Out) (Out, error)
}
// Middleware wraps callbacks, actions, and destinations to add cross-cutting concerns
// such as conditional execution, panic recovery, logging, retry logic, or telemetry.
type Middleware[I, O any] interface {
WrapCallback(ctx context.Context, rule Rule, callback Callback[I]) (Callback[I], error)
WrapAction(ctx context.Context, rule Rule, action Action[I, O]) (Action[I, O], error)
WrapDestination(ctx context.Context, rule Rule, destination Destination[O]) (Destination[O], error)
}
// ErrorHandler is a callback function type that handles errors occurring during
// event processing.
type ErrorHandler func(err error)