From 1a0a5e0432e403469c99399bccbe3465d81401ee Mon Sep 17 00:00:00 2001 From: Ferhat BOSTANCI Date: Thu, 10 Jun 2021 05:26:57 +0300 Subject: [PATCH] room stream mutex current player --- handler/handler.go | 11 ++++++----- handler/room.go | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 83e3fb1..cdfc9f0 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -10,11 +10,12 @@ import ( type MultiplayerHandler struct { pb.UnimplementedMultiplayerServiceServer - rooms map[string]map[string]*pb.Player - roomsMapMutex sync.RWMutex - jwtManager *jwt.Manager - mongoDB *mongo.Client - cfg *config.Config + rooms map[string]map[string]*pb.Player + roomsMapMutex sync.RWMutex + currentPlayersMutex 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 64c161e..72be58f 100644 --- a/handler/room.go +++ b/handler/room.go @@ -79,9 +79,11 @@ func (h *MultiplayerHandler) RoomStream(stream pb.MultiplayerService_RoomStreamS currentPlayers := make([]*pb.Player, 0, len(h.rooms[request.GetRoomId()])) if len(h.rooms[request.GetRoomId()]) > 2 { + h.currentPlayersMutex.Lock() for _, player := range h.rooms[request.GetRoomId()] { currentPlayers = append(currentPlayers, player) } + h.currentPlayersMutex.Unlock() } err = stream.Send(&pb.RoomStreamResponse{Players: currentPlayers})