Fix possible concurrency issues

This commit is contained in:
Claudemiro
2018-11-26 09:15:08 +01:00
parent d412759c16
commit 1a9fb1a706
2 changed files with 30 additions and 5 deletions
+6
View File
@@ -5,6 +5,7 @@
package connection
import (
"sync"
"time"
log "github.com/golang/glog"
@@ -17,6 +18,8 @@ type Socket interface {
// Connection An user connection
type Connection struct {
sync.Mutex
SocketID string
Socket Socket
CreatedAt time.Time
@@ -31,6 +34,9 @@ func New(socketID string, s Socket) *Connection {
// Publish the message to websocket attached to this client
func (conn *Connection) Publish(m interface{}) {
conn.Lock()
defer conn.Unlock()
if err := conn.Socket.WriteJSON(m); err != nil {
log.Errorf("error writing json into Socket, %+v", err)
}