mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 18:04:32 +08:00
refactor: standardize code comments
This commit is contained in:
@@ -17,7 +17,7 @@ type CombinedStreamsClient struct {
|
||||
subscribers map[string]chan []byte
|
||||
reconnect bool
|
||||
done chan struct{}
|
||||
batchSize int // 每批订阅的流数量
|
||||
batchSize int // Number of streams per batch subscription
|
||||
}
|
||||
|
||||
func NewCombinedStreamsClient(batchSize int) *CombinedStreamsClient {
|
||||
@@ -34,29 +34,29 @@ func (c *CombinedStreamsClient) Connect() error {
|
||||
HandshakeTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
// 组合流使用不同的端点
|
||||
// Combined streams use a different endpoint
|
||||
conn, _, err := dialer.Dial("wss://fstream.binance.com/stream", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("组合流WebSocket连接失败: %v", err)
|
||||
return fmt.Errorf("Combined stream WebSocket connection failed: %v", err)
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.conn = conn
|
||||
c.mu.Unlock()
|
||||
|
||||
log.Println("组合流WebSocket连接成功")
|
||||
log.Println("Combined stream WebSocket connected successfully")
|
||||
go c.readMessages()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchSubscribeKlines 批量订阅K线
|
||||
// BatchSubscribeKlines subscribes to K-lines in batches
|
||||
func (c *CombinedStreamsClient) BatchSubscribeKlines(symbols []string, interval string) error {
|
||||
// 将symbols分批处理
|
||||
// Split symbols into batches
|
||||
batches := c.splitIntoBatches(symbols, c.batchSize)
|
||||
|
||||
for i, batch := range batches {
|
||||
log.Printf("订阅第 %d 批, 数量: %d", i+1, len(batch))
|
||||
log.Printf("Subscribing batch %d, count: %d", i+1, len(batch))
|
||||
|
||||
streams := make([]string, len(batch))
|
||||
for j, symbol := range batch {
|
||||
@@ -64,10 +64,10 @@ func (c *CombinedStreamsClient) BatchSubscribeKlines(symbols []string, interval
|
||||
}
|
||||
|
||||
if err := c.subscribeStreams(streams); err != nil {
|
||||
return fmt.Errorf("第 %d 批订阅失败: %v", i+1, err)
|
||||
return fmt.Errorf("Batch %d subscription failed: %v", i+1, err)
|
||||
}
|
||||
|
||||
// 批次间延迟,避免被限制
|
||||
// Delay between batches to avoid rate limiting
|
||||
if i < len(batches)-1 {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func (c *CombinedStreamsClient) BatchSubscribeKlines(symbols []string, interval
|
||||
return nil
|
||||
}
|
||||
|
||||
// splitIntoBatches 将切片分成指定大小的批次
|
||||
// splitIntoBatches splits a slice into batches of specified size
|
||||
func (c *CombinedStreamsClient) splitIntoBatches(symbols []string, batchSize int) [][]string {
|
||||
var batches [][]string
|
||||
|
||||
@@ -91,7 +91,7 @@ func (c *CombinedStreamsClient) splitIntoBatches(symbols []string, batchSize int
|
||||
return batches
|
||||
}
|
||||
|
||||
// subscribeStreams 订阅多个流
|
||||
// subscribeStreams subscribes to multiple streams
|
||||
func (c *CombinedStreamsClient) subscribeStreams(streams []string) error {
|
||||
subscribeMsg := map[string]interface{}{
|
||||
"method": "SUBSCRIBE",
|
||||
@@ -103,10 +103,10 @@ func (c *CombinedStreamsClient) subscribeStreams(streams []string) error {
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
if c.conn == nil {
|
||||
return fmt.Errorf("WebSocket未连接")
|
||||
return fmt.Errorf("WebSocket not connected")
|
||||
}
|
||||
|
||||
log.Printf("订阅流: %v", streams)
|
||||
log.Printf("Subscribing to streams: %v", streams)
|
||||
return c.conn.WriteJSON(subscribeMsg)
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func (c *CombinedStreamsClient) readMessages() {
|
||||
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
log.Printf("读取组合流消息失败: %v", err)
|
||||
log.Printf("Failed to read combined stream message: %v", err)
|
||||
c.handleReconnect()
|
||||
return
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (c *CombinedStreamsClient) handleCombinedMessage(message []byte) {
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(message, &combinedMsg); err != nil {
|
||||
log.Printf("解析组合消息失败: %v", err)
|
||||
log.Printf("Failed to parse combined message: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func (c *CombinedStreamsClient) handleCombinedMessage(message []byte) {
|
||||
select {
|
||||
case ch <- combinedMsg.Data:
|
||||
default:
|
||||
log.Printf("订阅者通道已满: %s", combinedMsg.Stream)
|
||||
log.Printf("Subscriber channel is full: %s", combinedMsg.Stream)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,11 +174,11 @@ func (c *CombinedStreamsClient) handleReconnect() {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("组合流尝试重新连接...")
|
||||
log.Println("Combined stream attempting to reconnect...")
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
if err := c.Connect(); err != nil {
|
||||
log.Printf("组合流重新连接失败: %v", err)
|
||||
log.Printf("Combined stream reconnection failed: %v", err)
|
||||
go c.handleReconnect()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user