fix(消息去重): 去重键加入来源维度,避免跨来源信号被误去重
Motivation: 不同来源(如 crypto-strategy 与 trade-signal)可能产生策略、币种、周期、方向、价格完全一致的信号,旧去重键仅按这五维计算,导致后到的不同来源信号被误判为重复而漏推,无法支撑跟单策略等新增来源并行推送。 Changes: * 信号去重键新增来源(source)维度,不同来源的相同信号不再互相去重 * 信号哈希计算时透传来源名称,同一来源内的重复消息仍正常去重 * 补充跟单策略文案模板的渲染测试与接口创建文档
This commit is contained in:
@@ -24,12 +24,12 @@ func MessageHash(body []byte) string {
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func SignalHash(data map[string]interface{}) string {
|
||||
sum := sha256.Sum256([]byte(signalKey(data)))
|
||||
func SignalHash(source string, data map[string]interface{}) string {
|
||||
sum := sha256.Sum256([]byte(signalKey(source, data)))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func signalKey(data map[string]interface{}) string {
|
||||
func signalKey(source string, data map[string]interface{}) string {
|
||||
if data == nil {
|
||||
data = map[string]interface{}{}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func signalKey(data map[string]interface{}) string {
|
||||
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")
|
||||
return strings.Join([]string{strings.TrimSpace(source), strategy, symbol, period, direction, price}, "\x1f")
|
||||
}
|
||||
|
||||
func firstNonEmptyField(a, b string) string {
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestMessageHashStable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
|
||||
a := SignalHash(map[string]interface{}{
|
||||
a := SignalHash("crypto-strategy", map[string]interface{}{
|
||||
"strategyCode": "ai-crypto-signals",
|
||||
"symbol": "CRV",
|
||||
"period": "1h",
|
||||
@@ -34,7 +34,7 @@ func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
|
||||
"price": 0.2528,
|
||||
"eventTime": int64(1),
|
||||
})
|
||||
b := SignalHash(map[string]interface{}{
|
||||
b := SignalHash("crypto-strategy", map[string]interface{}{
|
||||
"strategyCode": "ai-crypto-signals",
|
||||
"currency": "CRV",
|
||||
"period": "1h",
|
||||
@@ -45,7 +45,7 @@ func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
|
||||
if a == "" || a != b {
|
||||
t.Fatalf("same signal fields should hash equal, a=%q b=%q", a, b)
|
||||
}
|
||||
c := SignalHash(map[string]interface{}{
|
||||
c := SignalHash("crypto-strategy", map[string]interface{}{
|
||||
"strategyCode": "ai-crypto-signals",
|
||||
"symbol": "CRV",
|
||||
"period": "1h",
|
||||
@@ -55,6 +55,16 @@ func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
|
||||
if a == c {
|
||||
t.Fatal("different price should hash differently")
|
||||
}
|
||||
otherSrc := SignalHash("trade-signal", map[string]interface{}{
|
||||
"strategyCode": "ai-crypto-signals",
|
||||
"symbol": "CRV",
|
||||
"period": "1h",
|
||||
"direction": "LONG",
|
||||
"price": 0.2528,
|
||||
})
|
||||
if a == otherSrc {
|
||||
t.Fatal("different sources should hash differently")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryDeduperClaimOnce(t *testing.T) {
|
||||
@@ -121,6 +131,37 @@ func TestHandleDedupByStrategySymbolPeriodDirectionPrice(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDedupKeepsDifferentSources(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, name string) (*model.Source, error) {
|
||||
return &model.Source{ID: 1, Name: name, Status: 1}, nil
|
||||
}
|
||||
conv := cryptostrategy.NewConverter()
|
||||
body := []byte(`{
|
||||
"eventType":"SIGNAL_RECEIVED","symbol":"CRV","direction":"LONG",
|
||||
"payload":"{\"strategyCode\":\"ai-crypto-signals\",\"period\":\"1h\",\"currency\":\"CRV\",\"isClose\":true,\"isGain\":true,\"price\":0.2528}",
|
||||
"eventTime":1786899538978
|
||||
}`)
|
||||
if d := HandleMessage(context.Background(), HandleInput{
|
||||
Body: body, SourceName: "crypto-strategy", MaxRetry: 3, Deduper: dedup,
|
||||
}, conv, lookup, process); d != DispositionAck {
|
||||
t.Fatalf("first=%v", d)
|
||||
}
|
||||
if d := HandleMessage(context.Background(), HandleInput{
|
||||
Body: body, SourceName: "trade-signal", MaxRetry: 3, Deduper: dedup,
|
||||
}, conv, lookup, process); d != DispositionAck {
|
||||
t.Fatalf("other source=%v", d)
|
||||
}
|
||||
if n.Load() != 2 {
|
||||
t.Fatalf("different sources should both process, got %d", n.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDuplicateAckSkipsProcess(t *testing.T) {
|
||||
dedup := NewMemoryDeduper()
|
||||
var n atomic.Int32
|
||||
|
||||
@@ -87,7 +87,7 @@ func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, l
|
||||
}
|
||||
|
||||
owned := false
|
||||
hash := SignalHash(data)
|
||||
hash := SignalHash(in.SourceName, data)
|
||||
if in.Deduper != nil {
|
||||
ok, err := in.Deduper.Claim(ctx, hash)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user