From 7d02ab063b1fab1eca53f7214980be91f4f23f09 Mon Sep 17 00:00:00 2001 From: luoyy Date: Wed, 23 Oct 2024 18:12:13 +0800 Subject: [PATCH] fix: resolve packet sequence errors when sending binary and non-binary data across multiple threads - Ensures proper sequencing of packets to avoid misordering when handling concurrent transmission of binary and regular data. - Fixes zishang520/socket.io#70. --- go.mod | 2 +- go.sum | 6 ++++-- socket/client.go | 6 ++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 44f47ac..830fcc9 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/zishang520/socket.io/v2 go 1.23.1 require ( - github.com/andybalholm/brotli v1.1.0 + github.com/andybalholm/brotli v1.1.1 github.com/zishang520/engine.io-go-parser v1.2.7 github.com/zishang520/engine.io/v2 v2.2.4 github.com/zishang520/socket.io-go-parser/v2 v2.2.2 diff --git a/go.sum b/go.sum index 0e3729e..aaa58e7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= -github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= +github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -41,6 +41,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= github.com/zishang520/engine.io-go-parser v1.2.7 h1:pnJr/9kOmOLBJcUQpOnRfR1q3UJAQudkeF4wLyqbtnM= github.com/zishang520/engine.io-go-parser v1.2.7/go.mod h1:WRsjNz1Oi04dqGcvjpW0t6/B2KIuDSrTBvCZDs7r3XY= github.com/zishang520/engine.io/v2 v2.2.4 h1:0tfKfjnHGDSmKJCYtuxk4SItjIeqJYi4+qBitJuWQqM= diff --git a/socket/client.go b/socket/client.go index 3a0becf..c42bd74 100644 --- a/socket/client.go +++ b/socket/client.go @@ -2,6 +2,7 @@ package socket import ( "net/url" + "sync" "sync/atomic" _types "github.com/zishang520/engine.io-go-parser/types" @@ -24,6 +25,8 @@ type Client struct { sockets *types.Map[SocketId, *Socket] nsps *types.Map[string, *Socket] connectTimeout atomic.Pointer[utils.Timer] + + mu sync.Mutex } func MakeClient() *Client { @@ -171,6 +174,9 @@ func (c *Client) _packet(packet *parser.Packet, opts *WriteOptions) { } func (c *Client) WriteToEngine(encodedPackets []_types.BufferInterface, opts *WriteOptions) { + c.mu.Lock() + defer c.mu.Unlock() + if opts.Volatile && !c.conn.Transport().Writable() { client_log.Debug("volatile packet is discarded since the transport is not currently writable") return