Skip to content

Commit

Permalink
fix: resolve packet sequence errors when sending binary and non-binar…
Browse files Browse the repository at this point in the history
…y data across multiple threads

- Ensures proper sequencing of packets to avoid misordering when handling concurrent transmission of binary and regular data.
- Fixes #70.
  • Loading branch information
zishang520 committed Oct 23, 2024
1 parent 3965505 commit 7d02ab0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions socket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package socket

import (
"net/url"
"sync"
"sync/atomic"

_types "github.com/zishang520/engine.io-go-parser/types"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d02ab0

Please sign in to comment.