浏览代码

add print switch

Joyit 1 月之前
父节点
当前提交
ed4b9b021d
共有 3 个文件被更改,包括 43 次插入20 次删除
  1. 10 6
      config/config.go
  2. 14 6
      hub.go
  3. 19 8
      line.go

+ 10 - 6
config/config.go

@@ -33,12 +33,14 @@ const (
 
 // 全局配置
 type Config struct {
-	ConnectTimeout       int // 没有收到数据包的连接超时时间(ms)
-	PingInterval         int // 发送ping包的时间间隔,前提是没有收到数据包(ms)
-	WriteWait            int // 写入网络数据等待最长时间(ms)
-	ReadWait             int // 获取命令数据超时时间 (ms)
-	LongReadWait         int // 长时间等待读取数据的超时时间(ms)
-	CleanDeadConnectWait int // 清理异常的连接(ms)
+	ConnectTimeout       int  // 没有收到数据包的连接超时时间(ms)
+	PingInterval         int  // 发送ping包的时间间隔,前提是没有收到数据包(ms)
+	WriteWait            int  // 写入网络数据等待最长时间(ms)
+	ReadWait             int  // 获取命令数据超时时间 (ms)
+	LongReadWait         int  // 长时间等待读取数据的超时时间(ms)
+	CleanDeadConnectWait int  // 清理异常的连接(ms)
+	PrintPing            bool // 打印连接ping包
+	PrintMsg             bool // 打印数据包
 }
 
 // 获取实例好的配置
@@ -51,5 +53,7 @@ func NewConfig() *Config {
 		ReadWait:             30 * 1000,
 		LongReadWait:         150 * 1000,
 		CleanDeadConnectWait: 3600 * 1000,
+		PrintPing:            true,
+		PrintMsg:             true,
 	}
 }

+ 14 - 6
hub.go

@@ -233,7 +233,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 							conn:  conn,
 						})
 						// 检查是否已经很久时间没有使用连接了
-						if time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*3)*time.Millisecond {
+						if time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*3*int(time.Millisecond)) {
 							// 超时关闭当前的连接
 							log.Println("get message timeout", conn.channel)
 							// 有可能连接出现问题,断开并重新连接
@@ -258,7 +258,9 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 				backchan: gd.backchan,
 				conn:     conn,
 			}
-			log.Println("[SEND]->", conn.channel, "["+gd.Cmd+"]", subStr(string(gd.Data), 200))
+			if h.cf.PrintMsg {
+				log.Println("[SEND]->", conn.channel, "["+gd.Cmd+"]", subStr(string(gd.Data), 200))
+			}
 			count++
 			if gd.Max > 0 && count >= gd.Max {
 				return false
@@ -273,7 +275,9 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 func (h *Hub) requestFromNet(request *RequestData) {
 	cmd := request.Cmd
 	channel := request.conn.channel
-	log.Println("[REQU]<-", channel, "["+cmd+"]", subStr(string(request.Data), 200))
+	if h.cf.PrintMsg {
+		log.Println("[REQU]<-", channel, "["+cmd+"]", subStr(string(request.Data), 200))
+	}
 	// 执行中间件
 	for _, mdFunc := range h.middle {
 		rsp := mdFunc(request)
@@ -311,7 +315,9 @@ func (h *Hub) requestFromNet(request *RequestData) {
 						State: state,
 						Data:  data,
 					}
-					log.Println("[RESP]->", channel, "["+cmd+"]", state, subStr(string(data), 200))
+					if h.cf.PrintMsg {
+						log.Println("[RESP]->", channel, "["+cmd+"]", state, subStr(string(data), 200))
+					}
 				}
 				return
 			}
@@ -370,7 +376,9 @@ func (h *Hub) GetX(channel *regexp.Regexp, cmd string, data []byte, backFunc Get
 				return
 			}
 			ch := rp.conn.channel
-			log.Println("[RECV]<-", ch, "["+gd.Cmd+"]", rp.State, subStr(string(rp.Data), 200))
+			if h.cf.PrintMsg {
+				log.Println("[RECV]<-", ch, "["+gd.Cmd+"]", rp.State, subStr(string(rp.Data), 200))
+			}
 			count++
 			// 如果这里返回为false这跳出循环
 			if backFunc != nil && !backFunc(rp) {
@@ -470,7 +478,7 @@ func (h *Hub) addLine(line *Line) {
 	h.connects.Range(func(key, _ any) bool {
 		conn := key.(*Line)
 		// 删除超时的连接
-		if conn.state != Connected && conn.host == nil && time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*5)*time.Millisecond {
+		if conn.state != Connected && conn.host == nil && time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*5*int(time.Millisecond)) {
 			h.connects.Delete(key)
 			return true
 		}

+ 19 - 8
line.go

@@ -147,7 +147,7 @@ func (c *Line) readPump() {
 // 检查管道并处理不同的消息,新go程调用
 // 为了防止多线程的冲突,主要的处理都在这里进行
 func (c *Line) writePump() {
-	pingTicker := time.NewTicker(time.Duration(c.cf.PingInterval) * time.Millisecond)
+	pingTicker := time.NewTicker(time.Duration(c.cf.PingInterval * int(time.Millisecond)))
 	// 定义恢复函数
 	defer func() {
 		pingTicker.Stop()
@@ -171,19 +171,25 @@ func (c *Line) writePump() {
 		case request := <-c.sendRequest: // 发送请求包
 			err := c.conn.WriteRequest(request.Id, request.Cmd, request.Data)
 			if err != nil {
-				log.Println(err)
+				log.Println("[writePump WriteRequest]", err)
 				return
 			}
 		case response := <-c.sendResponse: // 接收到的响应包
 			// 发送响应数据
 			err := c.conn.WriteResponse(response.Id, response.State, response.Data)
 			if err != nil {
-				log.Println(err)
+				log.Println("[writePump WriteResponse]", err)
 				return
 			}
 		case ping := <-c.pingRequest: // 发送ping包到网络
+			if c.cf.PrintPing {
+				log.Println("[PING]<-", c.channel, ping.Id)
+			}
 			// 只有服务器端需要回应ping包
 			if c.host == nil {
+				if c.cf.PrintPing {
+					log.Println("[RESP PING]->", c.channel, ping.Id)
+				}
 				err := c.conn.WritePing(ping.Id)
 				if err != nil {
 					log.Println("[ping ERROR]", err)
@@ -205,9 +211,9 @@ func (c *Line) writePump() {
 		case <-pingTicker.C:
 			// 检查是否已经很久时间没有使用连接了
 			dr := time.Since(c.lastRead)
-			if dr > time.Duration(c.cf.PingInterval*3)*time.Millisecond {
+			if dr > time.Duration(c.cf.PingInterval*3*int(time.Millisecond)) {
 				// 超时关闭当前的连接
-				log.Println("Connect timeout and stop it", c.channel)
+				log.Println("[Connect timeout and stop it]", c.channel)
 				// 有可能连接出现问题,断开并重新连接
 				c.Close(false)
 				return
@@ -215,11 +221,15 @@ func (c *Line) writePump() {
 			// 只需要客户端发送
 			if c.host != nil {
 				// 发送ping包
-				if dr > time.Duration(c.cf.PingInterval) {
+				if dr >= time.Duration(c.cf.PingInterval*int(time.Millisecond)) {
+					id := c.getPingID()
 					// 发送ping数据包
-					err := c.conn.WritePing(c.getPingID())
+					if c.cf.PrintPing {
+						log.Println("[SEND PING]->", c.channel, id)
+					}
+					err := c.conn.WritePing(id)
 					if err != nil {
-						log.Println(err)
+						log.Println("[writePump WritePing]", err)
 						return
 					}
 				}
@@ -267,6 +277,7 @@ func (c *Line) cleanClose() {
 // 连接开始运行
 func (c *Line) Start(conn conn.Connect, host *HostInfo) {
 	c.updated = time.Now()
+	c.lastRead = time.Now() // 避免默认为0时被清理
 	c.conn = conn
 	c.host = host
 	go c.readPump()