Repository navigation
Expand file tree
/
Copy pathrouting_selector.go
More file actions
314 lines (280 loc) · 8.33 KB
/
Copy pathrouting_selector.go
File metadata and controls
314 lines (280 loc) · 8.33 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package GoEventBus
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"sync"
"time"
)
var (
// ErrInvalidEventRule is returned when a rule is missing a predicate or choice.
ErrInvalidEventRule = errors.New("goeventbus: invalid event rule")
// ErrDecisionCache is returned when strict cache mode is enabled and a cache
// operation or cache-key calculation fails.
ErrDecisionCache = errors.New("goeventbus: decision cache failure")
)
// EventRuleMatch decides whether a deterministic rule applies to the current
// state and candidate set.
type EventRuleMatch func(context.Context, any, []EventCandidate) bool
// EventRule routes matching state directly to Choice without invoking cache or
// a model. Rules are evaluated in order and the first match wins.
type EventRule struct {
Name string
Choice string
Match EventRuleMatch
}
// DecisionCache stores event-routing decisions by a stable cache key.
//
// Implementations may be in-memory, Redis-backed, or distributed. The default
// RuleCacheSelector behavior is fail-open on cache errors.
type DecisionCache interface {
Get(context.Context, string) (EventDecision, bool, error)
Set(context.Context, string, EventDecision) error
}
// DecisionCacheKeyFunc creates a cache key from state and the candidate set.
type DecisionCacheKeyFunc func(any, []EventCandidate) (string, error)
// RuleCacheSelector implements the routing fast path:
//
// rules -> cache -> fallback selector (for example Jev) -> cache write
//
// Rules always run before cache so a newly-added deterministic rule can
// override a previously cached model decision immediately.
type RuleCacheSelector struct {
Rules []EventRule
Cache DecisionCache
// Fallback is called when no rule matches and cache misses. JevSelector is
// the typical fallback.
Fallback EventSelector
// CacheKey defaults to DefaultDecisionCacheKey.
CacheKey DecisionCacheKeyFunc
// StrictCache turns cache/key failures into routing errors. The default
// false value treats cache as an optimization and falls back normally.
StrictCache bool
}
// SelectEvent evaluates deterministic rules, then cache, then Fallback.
func (s *RuleCacheSelector) SelectEvent(
ctx context.Context,
state any,
candidates []EventCandidate,
) (EventDecision, error) {
if ctx == nil {
ctx = context.Background()
}
if s == nil {
return EventDecision{}, ErrNilEventSelector
}
if len(candidates) == 0 {
return EventDecision{}, ErrNoEventCandidates
}
validChoices := make(map[string]struct{}, len(candidates))
for _, candidate := range candidates {
key := strings.TrimSpace(candidate.Key)
if key == "" {
return EventDecision{}, fmt.Errorf("%w: empty candidate key", ErrInvalidEventCandidate)
}
if _, exists := validChoices[key]; exists {
return EventDecision{}, fmt.Errorf("%w: duplicate key %q", ErrInvalidEventCandidate, key)
}
validChoices[key] = struct{}{}
}
for i, rule := range s.Rules {
choice := strings.TrimSpace(rule.Choice)
if rule.Match == nil {
return EventDecision{}, fmt.Errorf("%w: rule %d (%q) has nil Match", ErrInvalidEventRule, i, rule.Name)
}
if choice == "" {
return EventDecision{}, fmt.Errorf("%w: rule %d (%q) has empty Choice", ErrInvalidEventRule, i, rule.Name)
}
if !rule.Match(ctx, state, candidates) {
continue
}
if _, ok := validChoices[choice]; !ok {
return EventDecision{}, fmt.Errorf("%w: rule %q selected %q", ErrUnknownEventChoice, rule.Name, choice)
}
return EventDecision{
Choice: choice,
Confidence: 1,
Probabilities: map[string]float64{choice: 1},
}, nil
}
cacheKey := ""
if s.Cache != nil {
keyFunc := s.CacheKey
if keyFunc == nil {
keyFunc = DefaultDecisionCacheKey
}
key, err := keyFunc(state, candidates)
if err != nil {
if s.StrictCache {
return EventDecision{}, fmt.Errorf("%w: build key: %v", ErrDecisionCache, err)
}
} else {
cacheKey = key
cached, ok, err := s.Cache.Get(ctx, cacheKey)
if err != nil {
if s.StrictCache {
return EventDecision{}, fmt.Errorf("%w: get: %v", ErrDecisionCache, err)
}
} else if ok {
if _, valid := validChoices[cached.Choice]; valid {
return cached, nil
}
}
}
}
if s.Fallback == nil {
return EventDecision{}, ErrNilEventSelector
}
decision, err := s.Fallback.SelectEvent(ctx, state, candidates)
if err != nil {
return EventDecision{}, err
}
if _, ok := validChoices[decision.Choice]; !ok {
return EventDecision{}, fmt.Errorf("%w: %q", ErrUnknownEventChoice, decision.Choice)
}
if s.Cache != nil && cacheKey != "" {
if err := s.Cache.Set(ctx, cacheKey, decision); err != nil && s.StrictCache {
return EventDecision{}, fmt.Errorf("%w: set: %v", ErrDecisionCache, err)
}
}
return decision, nil
}
// DefaultDecisionCacheKey hashes JSON-serializable state plus the stable
// candidate keys and descriptions. Projection values are intentionally omitted:
// they are execution details and can include types that are not JSON-serializable.
//
// Candidate order does not affect the key.
func DefaultDecisionCacheKey(state any, candidates []EventCandidate) (string, error) {
type cacheCandidate struct {
Key string `json:"key"`
Description string `json:"description,omitempty"`
}
normalized := make([]cacheCandidate, len(candidates))
for i, candidate := range candidates {
normalized[i] = cacheCandidate{
Key: strings.TrimSpace(candidate.Key),
Description: strings.TrimSpace(candidate.Description),
}
}
sort.Slice(normalized, func(i, j int) bool {
if normalized[i].Key == normalized[j].Key {
return normalized[i].Description < normalized[j].Description
}
return normalized[i].Key < normalized[j].Key
})
payload := struct {
State any `json:"state"`
Candidates []cacheCandidate `json:"candidates"`
}{
State: state,
Candidates: normalized,
}
encoded, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("encode cache key input: %w", err)
}
sum := sha256.Sum256(encoded)
return hex.EncodeToString(sum[:]), nil
}
type memoryDecisionCacheEntry struct {
decision EventDecision
expiresAt time.Time
}
// MemoryDecisionCache is a goroutine-safe, lazy-expiring decision cache.
//
// ttl <= 0 disables expiration. Expired entries are removed on access; no
// cleanup goroutine is started.
type MemoryDecisionCache struct {
mu sync.Mutex
ttl time.Duration
items map[string]memoryDecisionCacheEntry
}
// NewMemoryDecisionCache creates an in-memory decision cache.
func NewMemoryDecisionCache(ttl time.Duration) *MemoryDecisionCache {
return &MemoryDecisionCache{
ttl: ttl,
items: make(map[string]memoryDecisionCacheEntry),
}
}
// Get returns a cached decision when present and not expired.
func (c *MemoryDecisionCache) Get(
ctx context.Context,
key string,
) (EventDecision, bool, error) {
if ctx != nil {
if err := ctx.Err(); err != nil {
return EventDecision{}, false, err
}
}
if c == nil {
return EventDecision{}, false, nil
}
now := time.Now()
c.mu.Lock()
defer c.mu.Unlock()
entry, ok := c.items[key]
if !ok {
return EventDecision{}, false, nil
}
if !entry.expiresAt.IsZero() && !now.Before(entry.expiresAt) {
delete(c.items, key)
return EventDecision{}, false, nil
}
return cloneEventDecision(entry.decision), true, nil
}
// Set stores a decision using the cache's configured TTL.
func (c *MemoryDecisionCache) Set(
ctx context.Context,
key string,
decision EventDecision,
) error {
if ctx != nil {
if err := ctx.Err(); err != nil {
return err
}
}
if c == nil {
return nil
}
entry := memoryDecisionCacheEntry{decision: cloneEventDecision(decision)}
if c.ttl > 0 {
entry.expiresAt = time.Now().Add(c.ttl)
}
c.mu.Lock()
c.items[key] = entry
c.mu.Unlock()
return nil
}
// Delete removes one cache entry.
func (c *MemoryDecisionCache) Delete(key string) {
if c == nil {
return
}
c.mu.Lock()
delete(c.items, key)
c.mu.Unlock()
}
// Clear removes all cache entries.
func (c *MemoryDecisionCache) Clear() {
if c == nil {
return
}
c.mu.Lock()
c.items = make(map[string]memoryDecisionCacheEntry)
c.mu.Unlock()
}
func cloneEventDecision(decision EventDecision) EventDecision {
cloned := decision
if decision.Probabilities != nil {
cloned.Probabilities = make(map[string]float64, len(decision.Probabilities))
for key, value := range decision.Probabilities {
cloned.Probabilities[key] = value
}
}
return cloned
}