fix(api): use UUID to ensure traderID uniqueness (#893) (#1008)

## Problem
When multiple users create traders with the same exchange + AI model
combination within the same second, they generate identical traderIDs,
causing data conflicts.
Old code (Line 496):
```go
traderID := fmt.Sprintf("%s_%s_%d", req.ExchangeID, req.AIModelID, time.Now().Unix())
```
## Solution
Use UUID to guarantee 100% uniqueness while preserving prefix for debugging:
```go
traderID := fmt.Sprintf("%s_%s_%s", req.ExchangeID, req.AIModelID, uuid.New().String())
```
Example output: `binance_gpt-4_a1b2c3d4-e5f6-7890-abcd-ef1234567890`
## Changes
- `api/server.go:495-497`: Use UUID for traderID generation
- `api/traderid_test.go`: New test file with 3 comprehensive tests
## Tests
 All tests passed (0.189s)
 TestTraderIDUniqueness - 100 unique IDs generated
 TestTraderIDFormat - 3 exchange/model combinations validated
 TestTraderIDNoCollision - 1000 iterations without collision
Fixes #893
Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com>
This commit is contained in:
0xYYBB | ZYY | Bobo
2025-11-14 23:33:25 +08:00
committed by tangmengqiu
parent 4bb65397f6
commit c1cf44b98f
2 changed files with 126 additions and 2 deletions

View File

@@ -492,8 +492,9 @@ func (s *Server) handleCreateTrader(c *gin.Context) {
}
}
// 生成交易员ID
traderID := fmt.Sprintf("%s_%s_%d", req.ExchangeID, req.AIModelID, time.Now().Unix())
// 生成交易员ID (使用 UUID 确保唯一性,解决 Issue #893)
// 保留前缀以便调试和日志追踪
traderID := fmt.Sprintf("%s_%s_%s", req.ExchangeID, req.AIModelID, uuid.New().String())
// 设置默认值
isCrossMargin := true // 默认为全仓模式