I've written a REST API for a simple game including creating, joining and submitting game's matches. I've encountered a problem while testing server. By sending join requests by a single player (lots of them at once) the server would join the player to the lobby multiply times.
My design looks like this:
- in players' table check if player's field 'playing' is set to true, if yes discard the request
- if player is not currently playing create new player entry for the lobby and set player's playing status to true
I know that the problem is when checking if player is playing at the moment. Every request is run by separate goroutine in Go so it can happen that every goroutine gets the information from database that player's 'playing' field equals false. Then every gouroutine would add the same player to the lobby multiply times which I want to avoid.
Is there a way to avoid this problem, or the problems is in my design?