feat(规则): 支持同一事件多规则按条件区分并全部发送

Motivation:
止盈、止损等不同策略信号会映射到同一事件(如 trade.close),但原有唯一约束要求每个事件只能有一条规则,无法按策略区分处理。放开该约束后,同一事件可配置多条规则,通过规则条件与精确/通配优先级区分,命中条件的规则全部发送;同时将去重键从消息原文改为信号维度,避免同一信号因时间戳等无关字段差异被误判为重复。

Changes:

* 移除规则 source_id+event 的唯一约束,改为普通索引
* 事件匹配改为返回命中优先级内所有启用规则,并按 ID 逐条派发
* 通知服务遍历多条规则,按条件过滤后聚合发送渠道
* 去重键由消息 body 哈希改为策略/币种/周期/方向/价格信号维度哈希
This commit is contained in:
2026-08-17 01:20:49 +08:00
parent f5f64f7653
commit d2e8476398
13 changed files with 349 additions and 115 deletions
+50
View File
@@ -4,6 +4,10 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"time"
@@ -20,6 +24,52 @@ func MessageHash(body []byte) string {
return hex.EncodeToString(sum[:])
}
func SignalHash(data map[string]interface{}) string {
sum := sha256.Sum256([]byte(signalKey(data)))
return hex.EncodeToString(sum[:])
}
func signalKey(data map[string]interface{}) string {
if data == nil {
data = map[string]interface{}{}
}
strategy := fieldString(data["strategyCode"])
symbol := firstNonEmptyField(fieldString(data["symbol"]), fieldString(data["currency"]))
period := fieldString(data["period"])
direction := strings.ToUpper(firstNonEmptyField(fieldString(data["direction"]), fieldString(data["side"])))
price := fieldString(data["price"])
return strings.Join([]string{strategy, symbol, period, direction, price}, "\x1f")
}
func firstNonEmptyField(a, b string) string {
if a != "" {
return a
}
return b
}
func fieldString(v any) string {
if v == nil {
return ""
}
switch n := v.(type) {
case string:
return strings.TrimSpace(n)
case float64:
return strconv.FormatFloat(n, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(n), 'f', -1, 64)
case int:
return strconv.Itoa(n)
case int64:
return strconv.FormatInt(n, 10)
case json.Number:
return n.String()
default:
return strings.TrimSpace(fmt.Sprint(v))
}
}
type MemoryDeduper struct {
mu sync.Mutex
seen map[string]struct{}
+79
View File
@@ -3,11 +3,13 @@ package subscriber
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"aiaa-notification-service/internal/model"
"aiaa-notification-service/internal/notify"
"aiaa-notification-service/internal/subscriber/cryptostrategy"
"aiaa-notification-service/internal/subscriber/tradesignal"
)
@@ -23,6 +25,38 @@ func TestMessageHashStable(t *testing.T) {
}
}
func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
a := SignalHash(map[string]interface{}{
"strategyCode": "ai-crypto-signals",
"symbol": "CRV",
"period": "1h",
"direction": "LONG",
"price": 0.2528,
"eventTime": int64(1),
})
b := SignalHash(map[string]interface{}{
"strategyCode": "ai-crypto-signals",
"currency": "CRV",
"period": "1h",
"side": "long",
"price": 0.2528,
"eventTime": int64(2),
})
if a == "" || a != b {
t.Fatalf("same signal fields should hash equal, a=%q b=%q", a, b)
}
c := SignalHash(map[string]interface{}{
"strategyCode": "ai-crypto-signals",
"symbol": "CRV",
"period": "1h",
"direction": "LONG",
"price": 0.26,
})
if a == c {
t.Fatal("different price should hash differently")
}
}
func TestMemoryDeduperClaimOnce(t *testing.T) {
d := NewMemoryDeduper()
ok, err := d.Claim(context.Background(), "abc")
@@ -42,6 +76,51 @@ func TestMemoryDeduperClaimOnce(t *testing.T) {
}
}
func TestHandleDedupByStrategySymbolPeriodDirectionPrice(t *testing.T) {
dedup := NewMemoryDeduper()
var n atomic.Int32
process := func(context.Context, notify.Request) (notify.Result, error) {
n.Add(1)
return notify.Result{Matched: true}, nil
}
lookup := func(context.Context, string) (*model.Source, error) {
return &model.Source{ID: 1, Name: "crypto-strategy", Status: 1}, nil
}
conv := cryptostrategy.NewConverter()
in := func(eventTime int64) HandleInput {
body := []byte(fmt.Sprintf(`{
"eventType":"SIGNAL_RECEIVED","symbol":"CRV","direction":"LONG",
"payload":"{\"strategyCode\":\"ai-crypto-signals\",\"period\":\"1h\",\"currency\":\"CRV\",\"isClose\":true,\"isGain\":true,\"price\":0.2528}",
"eventTime":%d
}`, eventTime))
return HandleInput{Body: body, SourceName: "crypto-strategy", MaxRetry: 3, Deduper: dedup}
}
if d := HandleMessage(context.Background(), in(1786899538978), conv, lookup, process); d != DispositionAck {
t.Fatalf("first=%v", d)
}
if d := HandleMessage(context.Background(), in(1786899539730), conv, lookup, process); d != DispositionAck {
t.Fatalf("dup=%v", d)
}
if n.Load() != 1 {
t.Fatalf("same strategy/symbol/period/direction/price should process once, got %d", n.Load())
}
bodyDiffPrice := []byte(`{
"eventType":"SIGNAL_RECEIVED","symbol":"CRV","direction":"LONG",
"payload":"{\"strategyCode\":\"ai-crypto-signals\",\"period\":\"1h\",\"currency\":\"CRV\",\"isClose\":true,\"isGain\":true,\"price\":0.26}",
"eventTime":1786899539731
}`)
if d := HandleMessage(context.Background(), HandleInput{
Body: bodyDiffPrice, SourceName: "crypto-strategy", MaxRetry: 3, Deduper: dedup,
}, conv, lookup, process); d != DispositionAck {
t.Fatalf("diff price=%v", d)
}
if n.Load() != 2 {
t.Fatalf("different price should process again, got %d", n.Load())
}
}
func TestHandleDuplicateAckSkipsProcess(t *testing.T) {
dedup := NewMemoryDeduper()
var n atomic.Int32
+9 -12
View File
@@ -78,10 +78,17 @@ func errText(err error) string {
}
func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, lookup SourceLookup, process ProcessFunc) Disposition {
raw := string(in.Body)
event, data, err := conv.Convert(in.Body)
if err != nil {
hash := MessageHash(in.Body)
slog.Warn("invalid signal, ack", "hash", hash, "raw", raw, "error", errText(err))
return DispositionAck
}
owned := false
hash := ""
hash := SignalHash(data)
if in.Deduper != nil {
hash = MessageHash(in.Body)
ok, err := in.Deduper.Claim(ctx, hash)
if err != nil {
slog.Warn("dedup claim failed, processing anyway", "hash", hash, "error", errText(err))
@@ -93,18 +100,8 @@ func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, l
}
}
raw := string(in.Body)
if hash == "" {
hash = MessageHash(in.Body)
}
slog.Info("mq message", "name", in.Name, "queue", in.Queue, "hash", hash, "raw", raw)
event, data, err := conv.Convert(in.Body)
if err != nil {
slog.Warn("invalid signal, ack", "hash", hash, "raw", raw, "error", errText(err))
return DispositionAck
}
src, err := lookup(ctx, in.SourceName)
if err != nil || src == nil || src.Status != 1 {
slog.Warn("source unavailable, ack", "source", in.SourceName, "hash", hash, "raw", raw, "error", errText(err))