Skip to content

Commit

Permalink
Improve thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
liaohongxing committed Aug 27, 2024
1 parent 27e4c8d commit dadd0c8
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions socketio/socketio.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ type ws interface {
}

type Websocket struct {
mu sync.RWMutex
once sync.Once
mu sync.RWMutex
// The Fiber.Websocket connection
Conn *websocket.Conn
// Define if the connection is alive or not
Expand Down Expand Up @@ -564,13 +565,14 @@ func (kws *Websocket) read(ctx context.Context) {

// When the connection closes, disconnected method
func (kws *Websocket) disconnected(err error) {
kws.fireEvent(EventDisconnect, nil, err)

// may be called multiple times from different go routines
if kws.IsAlive() {
kws.setAlive(false)
if !kws.IsAlive() {
kws.once.Do(func() {
kws.setAlive(false)
close(kws.done)
}
kws.fireEvent(EventDisconnect, nil, err)
})
}

// Fire error event if the connection is
Expand Down

0 comments on commit dadd0c8

Please sign in to comment.