Browse Source

监听连接添加重试机制

chengqian 5 months ago
parent
commit
7f8f3ba940
1 changed files with 10 additions and 4 deletions
  1. 10 4
      server/service/tcp/tcp.go

+ 10 - 4
server/service/tcp/tcp.go

@@ -10,11 +10,17 @@ import (
 )
 
 func ListenTcp() {
+	var listen net.Listener
+	var err error
 	// 监听当前的tcp连接
-	listen, err := net.Listen("tcp", "0.0.0.0:9200")
-	if err != nil {
-		logger.Logger.Errorf("listen failed, err:%v", err)
-		return
+	for {
+		listen, err = net.Listen("tcp", "0.0.0.0:9200")
+		if err != nil {
+			logger.Logger.Errorf("Listen failed, err: %v. Retrying in 5 seconds...", err)
+			time.Sleep(5 * time.Second) // 休眠一段时间后重试
+			continue
+		}
+		break // 成功监听后退出循环
 	}
 
 	tracker := NewConnectionTracker() //创建连接检测器