diff --git a/src/matchmaking/matchmaking.gateway.ts b/src/matchmaking/matchmaking.gateway.ts index 48b294c0..ced4c31d 100644 --- a/src/matchmaking/matchmaking.gateway.ts +++ b/src/matchmaking/matchmaking.gateway.ts @@ -4,6 +4,7 @@ import { e_match_types_enum } from "generated"; import { MatchmakeService } from "./matchmake.service"; import { MatchmakingLobbyService } from "./matchmaking-lobby.service"; import { RedisManagerService } from "../redis/redis-manager/redis-manager.service"; +import { CacheService } from "src/cache/cache.service"; import { FiveStackWebSocketClient } from "src/sockets/types/FiveStackWebSocketClient"; import { ConnectedSocket, @@ -12,6 +13,7 @@ import { WebSocketGateway, } from "@nestjs/websockets"; import { JoinQueueError } from "./utilities/joinQueueError"; +import { PlayerLobby } from "./types/PlayerLobby"; import { HasuraService } from "src/hasura/hasura.service"; import { isRoleAbove } from "src/utilities/isRoleAbove"; import { e_player_roles_enum } from "generated"; @@ -29,6 +31,7 @@ export class MatchmakingGateway { public readonly redisManager: RedisManagerService, public readonly matchmakeService: MatchmakeService, public readonly matchmakingLobbyService: MatchmakingLobbyService, + private readonly cache: CacheService, ) { this.redis = this.redisManager.getConnection(); } @@ -122,7 +125,7 @@ export class MatchmakingGateway { throw new JoinQueueError("You do not have permission to join this queue"); } - let lobby; + let lobby: PlayerLobby | undefined; const user = client.user; if (!user) { @@ -220,16 +223,24 @@ export class MatchmakingGateway { throw new JoinQueueError("Unable to find Player Lobby"); } - await this.matchmakingLobbyService.verifyLobby(lobby, user, type); - try { - await this.matchmakingLobbyService.setLobbyDetails( - regions, - type, - lobby, + await this.cache.lock( + `matchmaking:verify:${lobby.id}`, + async () => { + await this.matchmakingLobbyService.verifyLobby(lobby, user, type); + await this.matchmakingLobbyService.setLobbyDetails( + regions, + type, + lobby, + ); + await this.matchmakeService.addLobbyToQueue(lobby.id); + return true; + }, ); - await this.matchmakeService.addLobbyToQueue(lobby.id); } catch (error) { + if (error instanceof JoinQueueError) { + throw error; + } this.logger.error(`unable to add lobby to queue`, error); await this.matchmakingLobbyService.removeLobbyFromQueue(lobby.id); await this.matchmakingLobbyService.removeLobbyDetails(lobby.id); diff --git a/src/matchmaking/matchmaking.module.ts b/src/matchmaking/matchmaking.module.ts index 298aa708..1aa8383a 100644 --- a/src/matchmaking/matchmaking.module.ts +++ b/src/matchmaking/matchmaking.module.ts @@ -3,6 +3,7 @@ import { loggerFactory } from "../utilities/LoggerFactory"; import { MatchmakingGateway } from "./matchmaking.gateway"; import { HasuraModule } from "src/hasura/hasura.module"; import { RedisModule } from "src/redis/redis.module"; +import { CacheModule } from "src/cache/cache.module"; import { MatchesModule } from "src/matches/matches.module"; import { MatchmakeService } from "./matchmake.service"; import { MatchmakingLobbyService } from "./matchmaking-lobby.service"; @@ -19,6 +20,7 @@ import { MarkPlayerOffline } from "./jobs/MarkPlayerOffline"; imports: [ RedisModule, HasuraModule, + CacheModule, forwardRef(() => MatchesModule), BullModule.registerQueue({ name: MatchmakingQueues.Matchmaking, diff --git a/src/sockets/sockets.gateway.ts b/src/sockets/sockets.gateway.ts index 037282e5..d4cf8839 100644 --- a/src/sockets/sockets.gateway.ts +++ b/src/sockets/sockets.gateway.ts @@ -1,5 +1,6 @@ import { ConnectedSocket, + OnGatewayConnection, SubscribeMessage, WebSocketGateway, } from "@nestjs/websockets"; @@ -10,7 +11,7 @@ import { SocketsService } from "./sockets.service"; @WebSocketGateway({ path: "/ws/web", }) -export class SocketsGateway { +export class SocketsGateway implements OnGatewayConnection { constructor(private readonly sockets: SocketsService) {} @SubscribeMessage("ping") @@ -22,7 +23,7 @@ export class SocketsGateway { await this.sockets.updateClient(client.user.steam_id, client.id); } - private async handleConnection( + public async handleConnection( @ConnectedSocket() client: FiveStackWebSocketClient, request: Request, ) {