apache / dubbo-getty

a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Apache License 2.0
220 stars 69 forks source link

fix:add read mutex in gettyWSConn(websocket) struct to prevent data race in ReadMessage() #123

Closed No-SilverBullet closed 4 months ago

No-SilverBullet commented 4 months ago

What this PR does:

  1. rename "lock" to "writeLock" and add "readLock" in gettyWSConn
  2. use the "readLock" to wrap the websocket'sReadMessage function to be concurrency safe( similar to WriteMessage())

Which issue(s) this PR fixes:

NONE Special notes for your reviewer: NONE Does this PR introduce a user-facing change?:

NONE
No-SilverBullet commented 4 months ago

The concurrent writing problem mentioned in this issue(https://github.com/apache/dubbo-getty/issues/119%EF%BC%89 )was fixed in the previous version(v1.4.12), this PR fixes the concurrent reading problem.All based on the belowing description:

"Connections support one concurrent reader and one concurrent writer.

Applications are responsible for ensuring that no more than one goroutine calls the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and that no more than one goroutine calls the read methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler) concurrently.

The Close and WriteControl methods can be called concurrently with all other methods."

-- from https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency