From 7334896307797b43c119eab5cfefc19a04d66317 Mon Sep 17 00:00:00 2001 From: Amir Reza Mehbakhsh Date: Sat, 24 Feb 2024 21:09:32 +0100 Subject: [PATCH] Use mutext for player model --- server/models/player.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server/models/player.go b/server/models/player.go index 9df5afd..1025e76 100644 --- a/server/models/player.go +++ b/server/models/player.go @@ -6,6 +6,7 @@ import ( "log" "skullking/constants" "strconv" + "sync" ) type Player struct { @@ -22,17 +23,28 @@ type Player struct { IsConnected bool // To keep track of closed channel IsClosed bool + mutex sync.Mutex } // Different scenarios for 'close of closed channel' // 1) If user opens duplicate tab and close the first one func (player *Player) Kick() { + + // We are using mutex to make sure IsClosed value is evaluated correctly + // when reading its value at the same time. + // https://go101.org/article/channel-closing.html + player.mutex.Lock() + + defer player.mutex.Unlock() + if !player.IsClosed { close(player.Message) player.IsClosed = true } + // First we need to check if it's nil or not + // we call kick method in game_handler, and player may has no connection if player.Connection != nil { _ = player.Connection.Close() }