Ver Fonte

fix leak

lijian há 4 anos atrás
pai
commit
03a20f3d9b
1 ficheiros alterados com 4 adições e 6 exclusões
  1. 4 6
      pkg/mqtt/connection.go

+ 4 - 6
pkg/mqtt/connection.go

@@ -3,7 +3,6 @@ package mqtt
 import (
 	"encoding/hex"
 	"errors"
-	"fmt"
 	"net"
 	"sparrow/pkg/models"
 	"sparrow/pkg/rpcs"
@@ -309,17 +308,16 @@ func (c *Connection) RcvMsgFromClient() {
 func (c *Connection) SendMsgToClient() {
 	host := c.Conn.RemoteAddr()
 	for {
+		c.lock.Lock()
+		if c.SendChan == nil {
+			return
+		}
 		msg, ok := <-c.SendChan
 		if !ok {
 			server.Log.Errorf("%s is end now", host)
 			return
 		}
-		c.lock.Lock()
-
 		server.Log.Debugf("send msg to %s=======\n%v\n=========", host, msg)
-		if c.Conn == nil {
-			fmt.Printf("客户端实例:%v, 消息内容:%v\r\n", c.Conn, msg)
-		}
 		err := msg.Encode(c.Conn)
 		if err != nil {
 			server.Log.Errorf("send msg err: %s=====\n%v\n=====", err, msg)