From 8af64c44a7a68c543f21fd51dd51431fda6010bd Mon Sep 17 00:00:00 2001 From: Ferhat BOSTANCI Date: Thu, 10 Jun 2021 05:11:00 +0300 Subject: [PATCH] room stream mutex --- handler/handler.go | 10 ++++++---- handler/room.go | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 96a17d8..83e3fb1 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -5,14 +5,16 @@ import ( "github.com/PulseyTeam/game-server/jwt" pb "github.com/PulseyTeam/game-server/proto" "go.mongodb.org/mongo-driver/mongo" + "sync" ) type MultiplayerHandler struct { pb.UnimplementedMultiplayerServiceServer - rooms map[string]map[string]*pb.Player - jwtManager *jwt.Manager - mongoDB *mongo.Client - cfg *config.Config + rooms map[string]map[string]*pb.Player + roomsMapMutex sync.RWMutex + jwtManager *jwt.Manager + mongoDB *mongo.Client + cfg *config.Config } type AuthHandler struct { diff --git a/handler/room.go b/handler/room.go index e33b3dd..64c161e 100644 --- a/handler/room.go +++ b/handler/room.go @@ -73,7 +73,9 @@ func (h *MultiplayerHandler) RoomStream(stream pb.MultiplayerService_RoomStreamS log.Trace().Msg(request.String()) } + h.roomsMapMutex.Lock() h.rooms[request.GetRoomId()][request.GetPlayer().GetId()] = request.GetPlayer() + h.roomsMapMutex.Unlock() currentPlayers := make([]*pb.Player, 0, len(h.rooms[request.GetRoomId()])) if len(h.rooms[request.GetRoomId()]) > 2 {