Skip to content

Commit

Permalink
Merge pull request #121 from No-SilverBullet/master
Browse files Browse the repository at this point in the history
fix issue120 and issue103: add mutex to prevent data race in websocket write message and use named return values for WritePkg method
  • Loading branch information
AlexStocks authored May 28, 2024
2 parents 6a6e1d1 + 01362ff commit 2769505
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 15 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ func (u *gettyUDPConn) CloseConn(_ int) {
type gettyWSConn struct {
gettyConn
conn *websocket.Conn
lock sync.Mutex
}

// create websocket connection
Expand Down Expand Up @@ -608,7 +609,7 @@ func (w *gettyWSConn) Send(pkg interface{}) (int, error) {
}

w.updateWriteDeadline()
if err = w.conn.WriteMessage(websocket.BinaryMessage, p); err == nil {
if err = w.threadSafeWriteMessage(websocket.BinaryMessage, p); err == nil {
w.writeBytes.Add((uint32)(len(p)))
w.writePkgNum.Add(1)
}
Expand All @@ -617,18 +618,18 @@ func (w *gettyWSConn) Send(pkg interface{}) (int, error) {

func (w *gettyWSConn) writePing() error {
w.updateWriteDeadline()
return perrors.WithStack(w.conn.WriteMessage(websocket.PingMessage, []byte{}))
return perrors.WithStack(w.threadSafeWriteMessage(websocket.PingMessage, []byte{}))
}

func (w *gettyWSConn) writePong(message []byte) error {
w.updateWriteDeadline()
return perrors.WithStack(w.conn.WriteMessage(websocket.PongMessage, message))
return perrors.WithStack(w.threadSafeWriteMessage(websocket.PongMessage, message))
}

// close websocket connection
func (w *gettyWSConn) CloseConn(waitSec int) {
w.updateWriteDeadline()
w.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "bye-bye!!!"))
w.threadSafeWriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "bye-bye!!!"))
conn := w.conn.UnderlyingConn()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetLinger(waitSec)
Expand All @@ -637,3 +638,13 @@ func (w *gettyWSConn) CloseConn(waitSec int) {
}
w.conn.Close()
}

// uses a mutex to ensure that only one thread can send a message at a time, preventing race conditions.
func (w *gettyWSConn) threadSafeWriteMessage(messageType int, data []byte) error {
w.lock.Lock()
defer w.lock.Unlock()
if err := w.conn.WriteMessage(messageType, data); err != nil {
return err
}
return nil
}
12 changes: 6 additions & 6 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (s *session) sessionToken() string {
s.name, s.EndPoint().EndPointType(), s.ID(), s.LocalAddr(), s.RemoteAddr())
}

func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, error) {
func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (pkgBytesLenth int, successCount int, err error) {
if pkg == nil {
return 0, 0, fmt.Errorf("@pkg is nil")
}
Expand All @@ -381,7 +381,8 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, er
const size = 64 << 10
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
log.Errorf("[session.WritePkg] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf)
err = perrors.WithStack(fmt.Errorf("[session.WritePkg] panic session %s: err=%v\n%s", s.sessionToken(), r, rBuf))
log.Error(err)
}
}()

Expand All @@ -407,13 +408,12 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, int, er
if 0 < timeout {
s.Connection.SetWriteTimeout(timeout)
}
var succssCount int
succssCount, err = s.Connection.Send(pkg)
successCount, err = s.Connection.Send(pkg)
if err != nil {
log.Warnf("%s, [session.WritePkg] @s.Connection.Write(pkg:%#v) = err:%+v", s.Stat(), pkg, err)
return len(pkgBytes), succssCount, perrors.WithStack(err)
return len(pkgBytes), successCount, perrors.WithStack(err)
}
return len(pkgBytes), succssCount, nil
return len(pkgBytes), successCount, nil
}

// WriteBytes for codecs
Expand Down

0 comments on commit 2769505

Please sign in to comment.