Skip to content

Commit

Permalink
Remove player socket mutex
Browse files Browse the repository at this point in the history
This SHOULD not be require anymore, as it was just due to the lack of
thread-safety in gorilla. gws claims to be doing this properly.

While the player could disconnect, causing us to null the socket after
it has been retrieved, this shouldn't really make a difference to
before, as even with the mutex we could run into a disconnected / invalid
connection.
  • Loading branch information
Bios-Marcel committed Oct 24, 2024
1 parent f74afd5 commit b6be472
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 18 deletions.
6 changes: 0 additions & 6 deletions internal/api/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ func handleIncommingEvent(lobby *game.Lobby, player *game.Player, data []byte) {
}

func WriteObject(player *game.Player, object easyjson.Marshaler) error {
player.GetWebsocketMutex().Lock()
defer player.GetWebsocketMutex().Unlock()

socket := player.GetWebsocket()
if socket == nil || !player.Connected {
return ErrPlayerNotConnected
Expand All @@ -187,9 +184,6 @@ func WriteObject(player *game.Player, object easyjson.Marshaler) error {
}

func WritePreparedMessage(player *game.Player, message *gws.Broadcaster) error {
player.GetWebsocketMutex().Lock()
defer player.GetWebsocketMutex().Unlock()

socket := player.GetWebsocket()
if socket == nil || !player.Connected {
return ErrPlayerNotConnected
Expand Down
10 changes: 0 additions & 10 deletions internal/game/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ func (player *Player) SetWebsocket(socket *gws.Conn) {
player.ws = socket
}

// GetWebsocketMutex returns a mutex for locking the websocket connection.
// Since gorilla websockets shits it self when two calls happen at
// the same time, we need a mutex per player, since each player has their
// own socket. This getter extends to prevent accidentally sending the mutex
// via the network.
func (player *Player) GetWebsocketMutex() *sync.Mutex {
return player.socketMutex
}

// GetUserSession returns the players current user session.
func (player *Player) GetUserSession() uuid.UUID {
return player.userSession
Expand Down Expand Up @@ -180,7 +171,6 @@ func createPlayer(name string) *Player {
ID: uuid.Must(uuid.NewV4()),
userSession: uuid.Must(uuid.NewV4()),
votedForKick: make(map[uuid.UUID]bool),
socketMutex: &sync.Mutex{},
}
}

Expand Down
2 changes: 0 additions & 2 deletions internal/game/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package game

import (
"sync"
"time"

"github.com/gofrs/uuid/v5"
Expand Down Expand Up @@ -200,7 +199,6 @@ type Player struct {
// userSession uniquely identifies the player.
userSession uuid.UUID
ws *gws.Conn
socketMutex *sync.Mutex
// disconnectTime is used to kick a player in case the lobby doesn't have
// space for new players. The player with the oldest disconnect.Time will
// get kicked.
Expand Down

0 comments on commit b6be472

Please sign in to comment.