Browse Source

fix channel started time bug

Joyit 1 month ago
parent
commit
4519ed7818
3 changed files with 10 additions and 7 deletions
  1. 1 1
      README.md
  2. 1 1
      hub.go
  3. 8 5
      line.go

+ 1 - 1
README.md

@@ -28,13 +28,13 @@
 
 ## 问题与优化
 
-- 出现断线没有自动重连的问题
 - 优化命令匹配的算法,当有数量级的连接时需要考虑;如使用固定 ID,自动生成匹配缓存等方式
 - 建立内存池来分配内存,减少内存碎片
 - 同地址多连接共存,使用不同的连接发送消息,减少延时,提高消息送达可靠性
 
 ## 已经解决的问题
 
+- 出现断线没有自动重连的问题
 - 随机频道获取
 - 断线时命令在还没有超时的情况下,要等待重连后发送
 - 增加 gzip 的功能,只需要压缩数据部分

+ 1 - 1
hub.go

@@ -185,7 +185,7 @@ func (h *Hub) AllChannelWithStarted() []string {
 	cs := make([]string, 0)
 	h.lines.Range(func(id int, line *Line) bool {
 		if line.state == Connected {
-			cs = append(cs, fmt.Sprintf("%s|%d", line.channel, line.started.UnixMilli()))
+			cs = append(cs, fmt.Sprintf("%s|%d", line.channel, line.updated.UnixMilli()))
 		}
 		return true
 	})

+ 8 - 5
line.go

@@ -298,8 +298,9 @@ func (c *Line) cleanClose() {
 
 // 连接开始运行
 func (c *Line) Start(conn conn.Connect, host *HostInfo) {
-	c.updated = time.Now()
-	c.lastRead = time.Now() // 避免默认为0时被清理
+	now := time.Now()
+	c.updated = now
+	c.lastRead = now // 避免默认为0时被清理
 	c.conn = conn
 	c.host = host
 	go c.readPump()
@@ -316,19 +317,21 @@ func NewConnect(
 	conn conn.Connect,
 	host *HostInfo,
 ) *Line {
+	now := time.Now()
 	cc := &Line{
 		cf:      cf,
 		channel: channel,
 		hub:     hub,
-		pingID:  uint16(time.Now().UnixNano()) % config.ID_MAX,
+		pingID:  uint16(now.UnixNano()) % config.ID_MAX,
 
 		sendRequest:  make(chan *RequestData, 32),
 		sendResponse: make(chan *ResponseData, 32),
 		pingRequest:  make(chan *PingData, 5),
 		closeConnect: make(chan bool, 5),
 
-		lastRead: time.Now(), // 避免默认为0时被清理
-		started:  time.Now(),
+		lastRead: now, // 避免默认为0时被清理
+		started:  now,
+		updated:  now,
 	}
 	cc.Start(conn, host)
 	return cc