Explorar el Código

fix: connection.go

lijian hace 2 años
padre
commit
3d4d40d1ad
Se han modificado 1 ficheros con 4 adiciones y 0 borrados
  1. 4 0
      pkg/mqtt/connection.go

+ 4 - 0
pkg/mqtt/connection.go

@@ -7,6 +7,7 @@ import (
 	"sparrow/pkg/models"
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
+	"sync"
 	"time"
 )
 
@@ -37,6 +38,7 @@ type Connection struct {
 	VendorId        string
 	closeChan       chan struct{}
 	DeviceCode      string
+	mLock           sync.Mutex
 }
 
 // NewConnection create a connection
@@ -120,6 +122,8 @@ func (c *Connection) ValidateToken(token []byte) error {
 
 // Close close
 func (c *Connection) Close() {
+	c.mLock.Lock()
+	defer c.mLock.Unlock()
 	DeviceID := c.DeviceID
 	server.Log.Infof("closing connection of device %v", DeviceID)
 	if c.Conn != nil {