feat(规则匹配): 事件名支持通配符匹配
Motivation: 通知规则的事件名此前仅支持精确匹配,无法用一条规则覆盖多类事件(如 trade.* 下的全部交易事件),规则配置维护成本高。本次引入通配符匹配,让规则可按模式批量命中事件,同时保证精确规则优先。 Changes: * 事件名支持 * 和 ? 通配符匹配 * 匹配优先级:精确匹配 > 更具体的通配 > 泛匹配,同分时取更小 ID * 精确命中未命中时回退为列出源下启用规则并按优先级挑选 * 抽取缓存预热逻辑为独立方法 * 更新文档说明事件名通配规则
This commit is contained in:
+29
-19
@@ -33,28 +33,38 @@ func (m *Matcher) Match(ctx context.Context, sourceID int, event string) (*model
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to DB
|
||||
rule, err := m.store.GetRuleBySourceEvent(ctx, sourceID, event)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("match rule: %w", err)
|
||||
}
|
||||
|
||||
// Warm cache
|
||||
if m.cache != nil {
|
||||
tmpl, err := m.store.GetTemplate(ctx, rule.TemplateID)
|
||||
if err != nil {
|
||||
return rule, nil // rule found but template fetch failed — still return rule
|
||||
}
|
||||
cr := &cache.CachedRule{
|
||||
RuleID: rule.ID,
|
||||
TemplateID: rule.TemplateID,
|
||||
Content: tmpl.Content,
|
||||
}
|
||||
if rule.Conditions != nil {
|
||||
cr.Conditions = string(*rule.Conditions)
|
||||
}
|
||||
_ = m.cache.SetRule(ctx, sourceID, event, cr)
|
||||
rules, listErr := m.store.ListEnabledRulesBySource(ctx, sourceID)
|
||||
if listErr != nil {
|
||||
return nil, fmt.Errorf("match rule: %w", listErr)
|
||||
}
|
||||
picked := PickEventRule(event, rules)
|
||||
if picked == nil {
|
||||
return nil, fmt.Errorf("match rule: %w", err)
|
||||
}
|
||||
return picked, nil
|
||||
}
|
||||
|
||||
m.warmRuleCache(ctx, sourceID, event, rule)
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
func (m *Matcher) warmRuleCache(ctx context.Context, sourceID int, event string, rule *model.Rule) {
|
||||
if m.cache == nil {
|
||||
return
|
||||
}
|
||||
tmpl, err := m.store.GetTemplate(ctx, rule.TemplateID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cr := &cache.CachedRule{
|
||||
RuleID: rule.ID,
|
||||
TemplateID: rule.TemplateID,
|
||||
Content: tmpl.Content,
|
||||
}
|
||||
if rule.Conditions != nil {
|
||||
cr.Conditions = string(*rule.Conditions)
|
||||
}
|
||||
_ = m.cache.SetRule(ctx, sourceID, event, cr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user