d2e8476398
Motivation: 止盈、止损等不同策略信号会映射到同一事件(如 trade.close),但原有唯一约束要求每个事件只能有一条规则,无法按策略区分处理。放开该约束后,同一事件可配置多条规则,通过规则条件与精确/通配优先级区分,命中条件的规则全部发送;同时将去重键从消息原文改为信号维度,避免同一信号因时间戳等无关字段差异被误判为重复。 Changes: * 移除规则 source_id+event 的唯一约束,改为普通索引 * 事件匹配改为返回命中优先级内所有启用规则,并按 ID 逐条派发 * 通知服务遍历多条规则,按条件过滤后聚合发送渠道 * 去重键由消息 body 哈希改为策略/币种/周期/方向/价格信号维度哈希
121 lines
2.6 KiB
Go
121 lines
2.6 KiB
Go
package subscriber
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"aiaa-notification-service/internal/cache"
|
|
)
|
|
|
|
type Deduper interface {
|
|
Claim(ctx context.Context, hash string) (bool, error)
|
|
Release(ctx context.Context, hash string) error
|
|
}
|
|
|
|
func MessageHash(body []byte) string {
|
|
sum := sha256.Sum256(body)
|
|
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{}
|
|
}
|
|
|
|
func NewMemoryDeduper() *MemoryDeduper {
|
|
return &MemoryDeduper{seen: make(map[string]struct{})}
|
|
}
|
|
|
|
func (d *MemoryDeduper) Claim(_ context.Context, hash string) (bool, error) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if _, ok := d.seen[hash]; ok {
|
|
return false, nil
|
|
}
|
|
d.seen[hash] = struct{}{}
|
|
return true, nil
|
|
}
|
|
|
|
func (d *MemoryDeduper) Release(_ context.Context, hash string) error {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
delete(d.seen, hash)
|
|
return nil
|
|
}
|
|
|
|
type cacheDeduper struct {
|
|
c *cache.Cache
|
|
ttl time.Duration
|
|
}
|
|
|
|
func NewCacheDeduper(c *cache.Cache, ttl time.Duration) Deduper {
|
|
if c == nil {
|
|
return NewMemoryDeduper()
|
|
}
|
|
if ttl <= 0 {
|
|
ttl = time.Hour
|
|
}
|
|
return &cacheDeduper{c: c, ttl: ttl}
|
|
}
|
|
|
|
func (d *cacheDeduper) Claim(ctx context.Context, hash string) (bool, error) {
|
|
return d.c.ClaimDedup(ctx, hash, d.ttl)
|
|
}
|
|
|
|
func (d *cacheDeduper) Release(ctx context.Context, hash string) error {
|
|
return d.c.ReleaseDedup(ctx, hash)
|
|
}
|