-
Notifications
You must be signed in to change notification settings - Fork 4
[Fix] 메세지 예외 처리 #380
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
[Fix] 메세지 예외 처리 #380
Conversation
- 친구 생성시 올바른 알림 생성
- 토큰이 없는 경우 알림 중지
- 토큰이 없는 경우 알림 중지, 로깅
WalkthroughFcmService에서 단일 사용자 토큰 조회 방식을 배치 처리로 변경했습니다. 모든 사용자 ID의 토큰을 일괄 조회한 후 토큰 보유 여부로 필터링하고, 토큰이 있는 사용자들만 기존 sendToUsers 메서드로 위임하는 방식으로 개선되었습니다. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/main/java/ku_rum/backend/domain/alarm/application/FcmService.java (2)
40-41: 데이터베이스 중복 조회를 제거하세요.현재 구현에서는 사용자와 토큰 데이터를 이 메서드에서 조회한 후(40-41번 줄), 필터링된 DTO로
sendToUsers를 호출할 때 동일한 데이터를 다시 조회합니다(75, 77번 줄). 이는 불필요한 데이터베이스 부하를 발생시킵니다.이미 조회한 토큰 데이터를
sendToUsers에 전달하거나, 필터링된 토큰으로 직접 메시지를 전송하는 방식을 고려하세요.🔎 중복 조회를 제거하는 리팩토링 제안
방안 1: 토큰을 직접 사용하여 메시지 전송 (권장)
if (userIdsWithToken.isEmpty()) { log.info( "FCM 전송 가능한 유저가 없습니다. 요청 userIds={}", fcmDirectDto.userIds() ); return; } - FcmDirectDto filteredDto = FcmDirectDto.builder() - .title(fcmDirectDto.title()) - .body(fcmDirectDto.body()) - .userIds(userIdsWithToken) - .build(); - sendToUsers(filteredDto); + List<String> tokens = userFcmTokens.stream() + .map(UserFcmToken::getToken) + .toList(); + + MulticastMessage message = MulticastMessage.builder() + .addAllTokens(tokens) + .setNotification( + Notification.builder() + .setTitle(fcmDirectDto.title()) + .setBody(fcmDirectDto.body()) + .build() + ) + .putData("title", fcmDirectDto.title()) + .putData("body", fcmDirectDto.body()) + .build(); + + try { + firebaseMessaging.sendEachForMulticast(message); + } catch (FirebaseMessagingException e) { + log.error("FCM error code: {}", e.getErrorCode()); + log.error("FCM message: {}", e.getMessage(), e); + e.printStackTrace(); + throw new GlobalException(FCM_SEND_ERROR); + } }방안 2: sendToUsers 메서드를 오버로드하여 이미 조회한 데이터를 전달
sendToUsers 메서드에 토큰 리스트를 받는 오버로드 버전을 추가하세요.
Also applies to: 66-71
43-49: Set을 사용하여 필터링 성능을 개선하세요.48번 줄에서
userIdsWithToken.contains(userId)를 사용하여 리스트에서 멤버십을 확인하고 있습니다. 이는 O(n*m) 시간 복잡도를 가지며, 사용자 수가 많을 경우 성능 저하를 일으킬 수 있습니다.
userIdsWithToken을Set으로 변환하면 O(1) 조회가 가능하여 전체 필터링 시간 복잡도를 O(n+m)으로 개선할 수 있습니다.🔎 Set을 사용한 성능 개선 제안
- List<Long> userIdsWithToken = userFcmTokens.stream() + Set<Long> userIdsWithToken = userFcmTokens.stream() .map(token -> token.getUser().getId()) - .toList(); + .collect(Collectors.toSet()); List<Long> userIdsWithoutToken = fcmDirectDto.userIds().stream() .filter(userId -> !userIdsWithToken.contains(userId)) .toList();파일 상단에 import 추가:
+import java.util.Set; +import java.util.stream.Collectors;66번 줄에서 FcmDirectDto를 생성할 때는 List로 변환:
FcmDirectDto filteredDto = FcmDirectDto.builder() .title(fcmDirectDto.title()) .body(fcmDirectDto.body()) - .userIds(userIdsWithToken) + .userIds(new ArrayList<>(userIdsWithToken)) .build();
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/main/java/ku_rum/backend/domain/alarm/application/FcmService.java
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
🔇 Additional comments (2)
src/main/java/ku_rum/backend/domain/alarm/application/FcmService.java (2)
51-56: 좋은 로깅 추가입니다!토큰이 없는 사용자를 정보 로그로 기록하는 것은 디버깅과 모니터링에 유용합니다. 로그 레벨과 메시지 내용이 적절합니다.
58-64: 조기 반환 처리가 적절합니다.전송 가능한 사용자가 없을 때 조기 반환하여 불필요한 처리를 방지하고, 이를 로그로 기록하는 것은 좋은 방어적 프로그래밍입니다.
Test Results 38 files 38 suites 11s ⏱️ Results for commit a27a9d9. |
kmw10693
left a comment
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.
고생하셨습니다!
#️⃣ 연관된 이슈
closes #이슈번호 입력해주세요.
📝작업 내용
작업 상세 내용
FCM 토큰을 보유하지 않은 사용자의 경우, FCM Push 처리 중지
💬리뷰 요구사항
Summary by CodeRabbit
출시 노트
✏️ Tip: You can customize this high-level summary in your review settings.