Joyit 2 tygodni temu
rodzic
commit
0193d2f7a2
2 zmienionych plików z 7 dodań i 6 usunięć
  1. 5 4
      hub.go
  2. 2 2
      line.go

+ 5 - 4
hub.go

@@ -847,11 +847,12 @@ func (h *Hub) ConnectToServerX(channel string, force bool, host *HostInfo) {
 		if err == nil {
 			return
 		}
-		log.Println("[ConnectToServer ERROR, try it again]", channel, host, err)
-		host = nil
+		log.Println("[ConnectToServer ERROR]", err)
 		// 产生一个随机数避免刹间重连过载
-		r := rand.New(rand.NewSource(time.Now().UnixNano()))
-		time.Sleep(time.Duration(r.Intn(h.cf.ConnectTimeout)+(h.cf.ConnectTimeout/2)) * time.Millisecond)
+		delay := time.Duration(rand.Intn(h.cf.ConnectTimeout)+(h.cf.ConnectTimeout/2)) * time.Millisecond
+		log.Println("[ConnectToServerX reconnect]", delay, channel, host)
+		host = nil
+		time.Sleep(delay)
 	}
 }
 

+ 2 - 2
line.go

@@ -178,12 +178,12 @@ func (c *Line) writePump() {
 		pingTicker.Stop()
 		c.conn.Close()
 		// 检查是否需要重新连接
-		if c.host != nil && c.state != Closed && c.state != Connected {
+		if c.autoReconnect && c.host != nil && c.state != Closed && c.state != Connected {
 			go func() {
 				c.host.Errors++
 				c.host.Updated = time.Now()
 
-				time.Sleep(5 * time.Second)
+				time.Sleep(5 * time.Second * time.Duration(c.host.Errors))
 				if c.host.Errors < 2 {
 					c.hub.ConnectToServerX(c.channel, false, c.host)
 				} else {