-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] 방 입/퇴장 동시성 구현 및 로직 변경 #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
41864be
f1b1308
9157f7c
0f52704
754a82b
3fe0628
39738fc
cae5121
4ffea60
cfc104a
ed66c38
54b8458
6624b79
2e8cbbc
a669fb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,15 +33,19 @@ | |
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import org.hibernate.boot.model.naming.IllegalIdentifierException; | ||
| import org.springframework.context.ApplicationEventPublisher; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RoomService { | ||
|
|
@@ -50,6 +54,8 @@ public class RoomService { | |
| private final RoomRepository roomRepository; | ||
| private final AtomicLong roomIdGenerator = new AtomicLong(0); | ||
| private final ApplicationEventPublisher eventPublisher; | ||
| private final Map<Long, Object> roomLocks = new ConcurrentHashMap<>(); | ||
| private static final String PENDING_SESSION_ID = "PENDING_SESSION_ID"; | ||
|
|
||
| public RoomCreateResponse saveRoom(RoomCreateRequest request) { | ||
|
|
||
|
|
@@ -66,48 +72,58 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) { | |
|
|
||
| Room room = new Room(newId, roomSetting, gameSetting, host); | ||
|
|
||
| room.getUserIdSessionMap().put(host.id, PENDING_SESSION_ID); | ||
|
|
||
| roomRepository.saveRoom(room); | ||
|
|
||
| eventPublisher.publishEvent(new RoomCreatedEvent(room, quiz)); | ||
|
|
||
| return new RoomCreateResponse(newId); | ||
| } | ||
|
|
||
| public void validateRoom(RoomValidationRequest request) { | ||
| public void enterRoom(RoomValidationRequest request) { | ||
|
|
||
| Room room = | ||
| roomRepository | ||
| .findRoom(request.roomId()) | ||
| .orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다.-1")); | ||
| Long roomId = request.roomId(); | ||
|
|
||
| if (room.getState().equals(RoomState.PLAYING)) { | ||
| throw new IllegalArgumentException("403 게임이 진행중입니다."); | ||
| } | ||
| Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object()); | ||
|
|
||
| int maxUserCnt = room.getRoomSetting().maxUserCount(); | ||
| int currentCnt = room.getPlayerSessionMap().size(); | ||
| if (maxUserCnt == currentCnt) { | ||
| throw new IllegalArgumentException("403 정원이 모두 찼습니다."); | ||
| } | ||
| synchronized (lock) { | ||
| Room room = findRoom(request.roomId()); | ||
|
|
||
| if (room.getState().equals(RoomState.PLAYING)) { | ||
| throw new IllegalArgumentException("403 게임이 진행중입니다."); | ||
| } | ||
|
|
||
| int maxUserCnt = room.getRoomSetting().maxUserCount(); | ||
| int currentCnt = room.getUserIdSessionMap().size(); | ||
| if (maxUserCnt == currentCnt) { | ||
| throw new IllegalArgumentException("403 정원이 모두 찼습니다."); | ||
| } | ||
|
|
||
| if (room.getRoomSetting().locked() | ||
| && !room.getRoomSetting().password().equals(request.password())) { | ||
| throw new IllegalArgumentException("401 비밀번호가 일치하지 않습니다."); | ||
| } | ||
|
|
||
| if (room.getRoomSetting().locked() | ||
| && !room.getRoomSetting().password().equals(request.password())) { | ||
| throw new IllegalArgumentException("401 비밀번호가 일치하지 않습니다."); | ||
| room.getUserIdSessionMap().put(getCurrentUserId(), PENDING_SESSION_ID); | ||
| } | ||
| } | ||
|
|
||
| public RoomInitialData enterRoom(Long roomId, String sessionId) { | ||
| public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) { | ||
|
|
||
| Room room = | ||
| roomRepository | ||
| .findRoom(roomId) | ||
| .orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다.")); | ||
| Room room = findRoom(roomId); | ||
|
|
||
| Player player = createPlayer(); | ||
|
|
||
| Map<String, Player> playerSessionMap = room.getPlayerSessionMap(); | ||
| Map<Long, String> userIdSessionMap = room.getUserIdSessionMap(); | ||
|
|
||
| playerSessionMap.put(sessionId, player); | ||
| String existingSession = userIdSessionMap.get(player.getId()); | ||
| /* 정상 흐름 or 재연결 */ | ||
| if (existingSession.equals(PENDING_SESSION_ID) || !existingSession.equals(sessionId)) { | ||
| userIdSessionMap.put(player.getId(), sessionId); | ||
| } | ||
|
|
||
| RoomSettingResponse roomSettingResponse = toRoomSettingResponse(room); | ||
|
|
||
|
|
@@ -130,42 +146,60 @@ public RoomInitialData enterRoom(Long roomId, String sessionId) { | |
| } | ||
|
|
||
| public RoomExitData exitRoom(Long roomId, String sessionId) { | ||
| Room room = | ||
| roomRepository | ||
| .findRoom(roomId) | ||
| .orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다.")); | ||
|
|
||
| Map<String, Player> playerSessionMap = room.getPlayerSessionMap(); | ||
| Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object()); | ||
|
|
||
| String destination = getDestination(roomId); | ||
| synchronized (lock) { | ||
| Room room = findRoom(roomId); | ||
|
|
||
| if (playerSessionMap.size() == 1 && playerSessionMap.get(sessionId) != null) { | ||
| roomRepository.removeRoom(roomId); | ||
| return RoomExitData.builder().destination(destination).removedRoom(true).build(); | ||
| } | ||
| Map<String, Player> playerSessionMap = room.getPlayerSessionMap(); | ||
|
|
||
| Player removedPlayer = playerSessionMap.remove(sessionId); | ||
| if (removedPlayer == null) { | ||
| throw new IllegalArgumentException("퇴장 처리 불가 - 404 해당 세션 플레이어는 존재하지않습니다."); | ||
| } | ||
| String destination = getDestination(roomId); | ||
|
|
||
| if (room.getHost().getId().equals(removedPlayer.getId())) { | ||
| Optional<String> nextHostSessionId = playerSessionMap.keySet().stream().findFirst(); | ||
| Player nextHost = | ||
| playerSessionMap.get( | ||
| nextHostSessionId.orElseThrow( | ||
| () -> | ||
| new IllegalArgumentException( | ||
| "방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다."))); | ||
| room.updateHost(nextHost); | ||
| } | ||
| Player removePlayer = playerSessionMap.get(sessionId); | ||
|
|
||
| SystemNoticeResponse systemNoticeResponse = | ||
| ofPlayerEvent(removedPlayer, RoomEventType.EXIT); | ||
| if (removePlayer == null) { | ||
| room.getUserIdSessionMap().remove(getCurrentUserId()); | ||
|
||
| throw new IllegalIdentifierException("404 세션 없음 비정상적인 퇴장 요청"); | ||
| } | ||
|
|
||
| PlayerListResponse playerListResponse = toPlayerListResponse(room); | ||
| /* 방 삭제 */ | ||
| if (playerSessionMap.size() == 1 && playerSessionMap.containsKey(sessionId)) { | ||
|
||
| roomRepository.removeRoom(roomId); | ||
| roomLocks.remove(roomId); | ||
| log.info("{}번 방 삭제", roomId); | ||
| return RoomExitData.builder().destination(destination).removedRoom(true).build(); | ||
| } | ||
|
|
||
| /* 방장 변경 */ | ||
| if (room.getHost().getId().equals(removePlayer.getId())) { | ||
|
||
|
|
||
| Optional<String> nextHostSessionId = | ||
| playerSessionMap.keySet().stream() | ||
| .filter(key -> !key.equals(sessionId)) | ||
| .findFirst(); | ||
|
|
||
| Player nextHost = | ||
| playerSessionMap.get( | ||
| nextHostSessionId.orElseThrow( | ||
| () -> | ||
| new IllegalArgumentException( | ||
| "방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다."))); | ||
|
|
||
| room.updateHost(nextHost); | ||
| log.info("user_id:{} 방장 변경 완료 ", nextHost.getId()); | ||
| } | ||
|
|
||
| room.getUserIdSessionMap().remove(removePlayer.getId()); | ||
| playerSessionMap.remove(sessionId); | ||
|
|
||
| return new RoomExitData(destination, playerListResponse, systemNoticeResponse, false); | ||
| SystemNoticeResponse systemNoticeResponse = | ||
| ofPlayerEvent(removePlayer, RoomEventType.EXIT); | ||
|
|
||
| PlayerListResponse playerListResponse = toPlayerListResponse(room); | ||
|
|
||
| return new RoomExitData(destination, playerListResponse, systemNoticeResponse, false); | ||
| } | ||
| } | ||
|
|
||
| public RoomListResponse getAllRooms() { | ||
|
|
@@ -187,7 +221,13 @@ private static String getDestination(Long roomId) { | |
| return "/sub/room/" + roomId; | ||
| } | ||
|
|
||
| private static Player createPlayer() { | ||
| private Player createPlayer() { | ||
| return new Player(getCurrentUserId(), getCurrentUserNickname()); | ||
| } | ||
|
|
||
| private Room findRoom(Long roomId) { | ||
| return roomRepository | ||
| .findRoom(roomId) | ||
| .orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다.")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[L4-변경제안]
입장과 validation 검사를 한 번에 해주는 건데, 뭔가 컨트롤러는 메서드 네이밍이
validateRoom으로 남아있고, 다른 엔드포인트나 서비스 네이밍은enterRoom이라 컨트롤러 네이밍도enterRoom이면 좋겠다는 의견입니다 !There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
헉. 메소드 네임은 안바꾸고 경로만 바꿨네요 .. 메서드 네임 변경해서 puh했습니다. 감사합니다!