Skip to content

Commit a9b954c

Browse files
committed
Refactor: 중복 로깅/불필요한 try-catch 제거
1 parent 0d5fd45 commit a9b954c

File tree

3 files changed

+65
-167
lines changed

3 files changed

+65
-167
lines changed

src/main/java/com/back/global/websocket/service/RoomParticipantService.java

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,102 +25,61 @@ public class RoomParticipantService {
2525

2626
// 사용자 방 입장
2727
public void enterRoom(Long userId, Long roomId) {
28-
try {
29-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
30-
31-
if (sessionInfo == null) {
32-
log.warn("세션 정보가 없어 방 입장 실패 - 사용자: {}, 방: {}", userId, roomId);
33-
throw new CustomException(ErrorCode.WS_SESSION_NOT_FOUND);
34-
}
35-
36-
// 기존 방에서 퇴장 처리
37-
if (sessionInfo.currentRoomId() != null) {
38-
exitRoom(userId, sessionInfo.currentRoomId());
39-
log.debug("기존 방에서 퇴장 처리 완료 - 사용자: {}, 이전 방: {}",
40-
userId, sessionInfo.currentRoomId());
41-
}
28+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
4229

43-
// 세션 정보에 방 ID 업데이트
44-
WebSocketSessionInfo updatedSession = sessionInfo.withRoomId(roomId);
45-
redisSessionStore.saveUserSession(userId, updatedSession);
30+
if (sessionInfo == null) {
31+
log.warn("세션 정보가 없어 방 입장 실패 - 사용자: {}, 방: {}", userId, roomId);
32+
throw new CustomException(ErrorCode.WS_SESSION_NOT_FOUND);
33+
}
4634

47-
// 방 참가자 목록에 추가
48-
redisSessionStore.addUserToRoom(roomId, userId);
35+
if (sessionInfo.currentRoomId() != null) {
36+
exitRoom(userId, sessionInfo.currentRoomId());
37+
log.debug("기존 방에서 퇴장 처리 완료 - 사용자: {}, 이전 방: {}",
38+
userId, sessionInfo.currentRoomId());
39+
}
4940

50-
log.info("방 입장 완료 - 사용자: {}, 방: {}", userId, roomId);
41+
WebSocketSessionInfo updatedSession = sessionInfo.withRoomId(roomId);
42+
redisSessionStore.saveUserSession(userId, updatedSession);
43+
redisSessionStore.addUserToRoom(roomId, userId);
5144

52-
} catch (CustomException e) {
53-
throw e;
54-
} catch (Exception e) {
55-
log.error("방 입장 실패 - 사용자: {}, 방: {}", userId, roomId, e);
56-
throw new CustomException(ErrorCode.WS_ROOM_JOIN_FAILED);
57-
}
45+
log.info("방 입장 완료 - 사용자: {}, 방: {}", userId, roomId);
5846
}
5947

6048
// 사용자 방 퇴장
6149
public void exitRoom(Long userId, Long roomId) {
62-
try {
63-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
64-
65-
if (sessionInfo == null) {
66-
log.warn("세션 정보가 없지만 방 퇴장 처리 계속 진행 - 사용자: {}, 방: {}", userId, roomId);
67-
} else {
68-
// 세션 정보에서 방 ID 제거
69-
WebSocketSessionInfo updatedSession = sessionInfo.withoutRoom();
70-
redisSessionStore.saveUserSession(userId, updatedSession);
71-
}
50+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
7251

73-
// 방 참가자 목록에서 제거
74-
redisSessionStore.removeUserFromRoom(roomId, userId);
75-
76-
log.info("방 퇴장 완료 - 사용자: {}, 방: {}", userId, roomId);
77-
78-
} catch (Exception e) {
79-
log.error("방 퇴장 실패 - 사용자: {}, 방: {}", userId, roomId, e);
80-
throw new CustomException(ErrorCode.WS_ROOM_LEAVE_FAILED);
52+
if (sessionInfo == null) {
53+
log.warn("세션 정보가 없지만 방 퇴장 처리 계속 진행 - 사용자: {}, 방: {}", userId, roomId);
54+
} else {
55+
WebSocketSessionInfo updatedSession = sessionInfo.withoutRoom();
56+
redisSessionStore.saveUserSession(userId, updatedSession);
8157
}
58+
59+
redisSessionStore.removeUserFromRoom(roomId, userId);
60+
log.info("방 퇴장 완료 - 사용자: {}, 방: {}", userId, roomId);
8261
}
8362

8463
// 사용자의 현재 방 ID 조회
8564
public Long getCurrentRoomId(Long userId) {
86-
try {
87-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
88-
return sessionInfo != null ? sessionInfo.currentRoomId() : null;
89-
} catch (Exception e) {
90-
log.error("사용자 현재 방 조회 실패 - 사용자: {}", userId, e);
91-
return null;
92-
}
65+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
66+
return sessionInfo != null ? sessionInfo.currentRoomId() : null;
9367
}
9468

9569
// 방의 온라인 참가자 목록 조회
9670
public Set<Long> getParticipants(Long roomId) {
97-
try {
98-
return redisSessionStore.getRoomUsers(roomId);
99-
} catch (Exception e) {
100-
log.error("방 참가자 목록 조회 실패 - 방: {}", roomId, e);
101-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
102-
}
71+
return redisSessionStore.getRoomUsers(roomId);
10372
}
10473

10574
// 방의 온라인 참가자 수 조회
10675
public long getParticipantCount(Long roomId) {
107-
try {
108-
return redisSessionStore.getRoomUserCount(roomId);
109-
} catch (Exception e) {
110-
log.error("방 참가자 수 조회 실패 - 방: {}", roomId, e);
111-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
112-
}
76+
return redisSessionStore.getRoomUserCount(roomId);
11377
}
11478

11579
// 사용자가 특정 방에 참여 중인지 확인
11680
public boolean isUserInRoom(Long userId, Long roomId) {
117-
try {
118-
Long currentRoomId = getCurrentRoomId(userId);
119-
return currentRoomId != null && currentRoomId.equals(roomId);
120-
} catch (Exception e) {
121-
log.error("사용자 방 참여 여부 확인 실패 - 사용자: {}, 방: {}", userId, roomId, e);
122-
return false;
123-
}
81+
Long currentRoomId = getCurrentRoomId(userId);
82+
return currentRoomId != null && currentRoomId.equals(roomId);
12483
}
12584

12685
// 모든 방에서 사용자 퇴장 처리 (세션 종료 시 사용)

src/main/java/com/back/global/websocket/service/UserSessionService.java

Lines changed: 36 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,127 +24,76 @@ public class UserSessionService {
2424

2525
// 세션 등록
2626
public void registerSession(Long userId, String sessionId) {
27-
try {
28-
// 기존 세션 확인 및 제거
29-
WebSocketSessionInfo existingSession = redisSessionStore.getUserSession(userId);
30-
if (existingSession != null) {
31-
terminateSession(existingSession.sessionId());
32-
log.info("기존 세션 제거 후 새 세션 등록 - 사용자: {}", userId);
33-
}
34-
35-
// 새 세션 생성 및 등록
36-
WebSocketSessionInfo newSession = WebSocketSessionInfo.createNewSession(userId, sessionId);
37-
redisSessionStore.saveUserSession(userId, newSession);
38-
redisSessionStore.saveSessionUserMapping(sessionId, userId);
39-
40-
log.info("WebSocket 세션 등록 완료 - 사용자: {}, 세션: {}", userId, sessionId);
41-
42-
} catch (Exception e) {
43-
log.error("WebSocket 세션 등록 실패 - 사용자: {}", userId, e);
44-
throw new CustomException(ErrorCode.WS_CONNECTION_FAILED);
27+
WebSocketSessionInfo existingSession = redisSessionStore.getUserSession(userId);
28+
if (existingSession != null) {
29+
terminateSession(existingSession.sessionId());
30+
log.info("기존 세션 제거 후 새 세션 등록 - 사용자: {}", userId);
4531
}
32+
33+
WebSocketSessionInfo newSession = WebSocketSessionInfo.createNewSession(userId, sessionId);
34+
redisSessionStore.saveUserSession(userId, newSession);
35+
redisSessionStore.saveSessionUserMapping(sessionId, userId);
36+
37+
log.info("WebSocket 세션 등록 완료 - 사용자: {}, 세션: {}", userId, sessionId);
4638
}
4739

4840
// 세션 종료
4941
public void terminateSession(String sessionId) {
50-
try {
51-
Long userId = redisSessionStore.getUserIdBySession(sessionId);
52-
53-
if (userId != null) {
54-
// 세션 데이터 삭제
55-
redisSessionStore.deleteUserSession(userId);
56-
redisSessionStore.deleteSessionUserMapping(sessionId);
57-
58-
log.info("WebSocket 세션 종료 완료 - 세션: {}, 사용자: {}", sessionId, userId);
59-
} else {
60-
log.warn("종료할 세션을 찾을 수 없음 - 세션: {}", sessionId);
61-
}
62-
63-
} catch (Exception e) {
64-
log.error("WebSocket 세션 종료 실패 - 세션: {}", sessionId, e);
65-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
42+
Long userId = redisSessionStore.getUserIdBySession(sessionId);
43+
44+
if (userId != null) {
45+
redisSessionStore.deleteUserSession(userId);
46+
redisSessionStore.deleteSessionUserMapping(sessionId);
47+
log.info("WebSocket 세션 종료 완료 - 세션: {}, 사용자: {}", sessionId, userId);
48+
} else {
49+
log.warn("종료할 세션을 찾을 수 없음 - 세션: {}", sessionId);
6650
}
6751
}
6852

6953
// Heartbeat 처리 (활동 시간 업데이트 및 TTL 연장)
7054
public void processHeartbeat(Long userId) {
71-
try {
72-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
73-
74-
if (sessionInfo == null) {
75-
log.warn("세션 정보가 없어 Heartbeat 처리 실패 - 사용자: {}", userId);
76-
return;
77-
}
78-
79-
// 마지막 활동 시간 업데이트
80-
WebSocketSessionInfo updatedSession = sessionInfo.withUpdatedActivity();
55+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
8156

82-
// TTL 연장하며 저장
83-
redisSessionStore.saveUserSession(userId, updatedSession);
57+
if (sessionInfo == null) {
58+
log.warn("세션 정보가 없어 Heartbeat 처리 실패 - 사용자: {}", userId);
59+
return;
60+
}
8461

85-
log.debug("Heartbeat 처리 완료 - 사용자: {}, TTL 연장", userId);
62+
WebSocketSessionInfo updatedSession = sessionInfo.withUpdatedActivity();
63+
redisSessionStore.saveUserSession(userId, updatedSession);
8664

87-
} catch (Exception e) {
88-
log.error("Heartbeat 처리 실패 - 사용자: {}", userId, e);
89-
throw new CustomException(ErrorCode.WS_ACTIVITY_UPDATE_FAILED);
90-
}
65+
log.debug("Heartbeat 처리 완료 - 사용자: {}, TTL 연장", userId);
9166
}
9267

9368
// 사용자 연결 상태 확인
9469
public boolean isConnected(Long userId) {
95-
try {
96-
return redisSessionStore.existsUserSession(userId);
97-
} catch (Exception e) {
98-
log.error("사용자 연결 상태 확인 실패 - 사용자: {}", userId, e);
99-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
100-
}
70+
return redisSessionStore.existsUserSession(userId);
10171
}
10272

10373
// 사용자 세션 정보 조회
10474
public WebSocketSessionInfo getSessionInfo(Long userId) {
105-
try {
106-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
75+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
10776

108-
if (sessionInfo == null) {
109-
log.debug("세션 정보 없음 - 사용자: {}", userId);
110-
}
111-
112-
return sessionInfo;
113-
114-
} catch (Exception e) {
115-
log.error("세션 정보 조회 실패 - 사용자: {}", userId, e);
116-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
77+
if (sessionInfo == null) {
78+
log.debug("세션 정보 없음 - 사용자: {}", userId);
11779
}
80+
81+
return sessionInfo;
11882
}
11983

12084
// 세션ID로 사용자ID 조회
12185
public Long getUserIdBySessionId(String sessionId) {
122-
try {
123-
return redisSessionStore.getUserIdBySession(sessionId);
124-
} catch (Exception e) {
125-
log.error("세션으로 사용자 조회 실패 - 세션: {}", sessionId, e);
126-
throw new CustomException(ErrorCode.WS_REDIS_ERROR);
127-
}
86+
return redisSessionStore.getUserIdBySession(sessionId);
12887
}
12988

13089
// 사용자의 현재 방 ID 조회
13190
public Long getCurrentRoomId(Long userId) {
132-
try {
133-
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
134-
return sessionInfo != null ? sessionInfo.currentRoomId() : null;
135-
} catch (Exception e) {
136-
log.error("사용자 현재 방 조회 실패 - 사용자: {}", userId, e);
137-
return null;
138-
}
91+
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
92+
return sessionInfo != null ? sessionInfo.currentRoomId() : null;
13993
}
14094

14195
// 전체 온라인 사용자 수 조회
14296
public long getTotalOnlineUserCount() {
143-
try {
144-
return redisSessionStore.getTotalOnlineUserCount();
145-
} catch (Exception e) {
146-
log.error("전체 온라인 사용자 수 조회 실패", e);
147-
return 0;
148-
}
97+
return redisSessionStore.getTotalOnlineUserCount();
14998
}
15099
}

src/main/java/com/back/global/websocket/service/WebSocketSessionManager.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ public class WebSocketSessionManager {
1515
private final UserSessionService userSessionService;
1616
private final RoomParticipantService roomParticipantService;
1717

18-
// ============= 세션 관리 =============
19-
2018
// 사용자 세션 추가 (WebSocket 연결 시 호출)
2119
public void addSession(Long userId, String sessionId) {
2220
userSessionService.registerSession(userId, sessionId);
23-
log.info("WebSocket 연결 완료 - 사용자: {}, 세션: {}", userId, sessionId);
2421
}
2522

2623
// 세션 제거 (WebSocket 연결 종료 시 호출)
@@ -33,8 +30,6 @@ public void removeSession(String sessionId) {
3330

3431
// 2. 세션 종료
3532
userSessionService.terminateSession(sessionId);
36-
37-
log.info("WebSocket 연결 종료 완료 - 세션: {}, 사용자: {}", sessionId, userId);
3833
} else {
3934
log.warn("종료할 세션을 찾을 수 없음 - 세션: {}", sessionId);
4035
}
@@ -53,26 +48,21 @@ public WebSocketSessionInfo getSessionInfo(Long userId) {
5348
// Heartbeat 처리 (활동 시간 업데이트 및 TTL 연장)
5449
public void updateLastActivity(Long userId) {
5550
userSessionService.processHeartbeat(userId);
56-
log.debug("Heartbeat 처리 완료 - 사용자: {}", userId);
5751
}
5852

5953
// 전체 온라인 사용자 수 조회
6054
public long getTotalOnlineUserCount() {
6155
return userSessionService.getTotalOnlineUserCount();
6256
}
6357

64-
// ============= 방 관리 =============
65-
6658
// 사용자가 방에 입장
6759
public void joinRoom(Long userId, Long roomId) {
6860
roomParticipantService.enterRoom(userId, roomId);
69-
log.info("방 입장 처리 완료 - 사용자: {}, 방: {}", userId, roomId);
7061
}
7162

7263
// 사용자가 방에서 퇴장
7364
public void leaveRoom(Long userId, Long roomId) {
7465
roomParticipantService.exitRoom(userId, roomId);
75-
log.info("방 퇴장 처리 완료 - 사용자: {}, 방: {}", userId, roomId);
7666
}
7767

7868
// 방의 온라인 사용자 수 조회

0 commit comments

Comments
 (0)