Files
aiaa-notification-server/internal/engine/event_match.go
T
ryan de7a52e81f feat(规则匹配): 事件名支持通配符匹配
Motivation:
通知规则的事件名此前仅支持精确匹配,无法用一条规则覆盖多类事件(如 trade.* 下的全部交易事件),规则配置维护成本高。本次引入通配符匹配,让规则可按模式批量命中事件,同时保证精确规则优先。

Changes:

* 事件名支持 * 和 ? 通配符匹配
* 匹配优先级:精确匹配 > 更具体的通配 > 泛匹配,同分时取更小 ID
* 精确命中未命中时回退为列出源下启用规则并按优先级挑选
* 抽取缓存预热逻辑为独立方法
* 更新文档说明事件名通配规则
2026-08-15 23:39:00 +08:00

56 lines
1.1 KiB
Go

package engine
import (
"path"
"strings"
"aiaa-notification-service/internal/model"
)
// PickEventRule selects the best enabled rule for an event.
// Exact event wins; otherwise glob patterns (* and ?) via path.Match.
// Among globs, more literal characters win; ties go to the smaller ID.
func PickEventRule(event string, rules []model.Rule) *model.Rule {
var exact *model.Rule
var best *model.Rule
bestScore := -1
for i := range rules {
r := &rules[i]
if r.Enabled == 0 {
continue
}
if r.Event == event {
if exact == nil || r.ID < exact.ID {
exact = r
}
continue
}
if !strings.ContainsAny(r.Event, "*?") {
continue
}
ok, err := path.Match(r.Event, event)
if err != nil || !ok {
continue
}
score := globSpecificity(r.Event)
if score > bestScore || (score == bestScore && (best == nil || r.ID < best.ID)) {
bestScore = score
best = r
}
}
if exact != nil {
return exact
}
return best
}
func globSpecificity(pattern string) int {
n := 0
for _, r := range pattern {
if r != '*' && r != '?' {
n++
}
}
return n
}