Skip to content

SSE 연결 관리 로직 개선 - #38

Merged
cfcromn merged 3 commits into
developfrom
feat/sse-connection-management
Jun 7, 2026
Merged

cfcromn merged 3 commits into
developfrom
feat/sse-connection-management

Conversation

@cfcromn

@cfcromn cfcromn commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

✨ 작업 내용

SSE 연결 관리의 여러 문제를 개선했습니다. 새로고침 반복 시 죽은 연결이 누적되는 현상을 해결하고, 초기 구독 이벤트 처리 방식을 의미에 맞게 교체했습니다. 또한 cleanup() 공통 함수를 도입해 모든 종료 경로에서 일관되게 연결이 정리되도록 했습니다.


🔍 리뷰 시 참고사항

  • 초기 구독 전송 방식 변경: 기존에는 @TransactionalEventListener(AFTER_COMMIT)로 초기 데이터를 전송했는데, 이는 DB 변경 트랜잭션과 무관한 구독 요청에 트랜잭션 이벤트를 오용하는 구조였습니다. @EventListener + @Async + @Transactional(readOnly = true)로 교체해 의미를 명확히 했습니다.
  • AtomicInteger activeConnections 제거: emitters.size를 단일 소스로 사용해 카운터 불일치 가능성을 원천 차단했습니다. 최대 연결 수 검사는 emitters.size >= maxConnections로 처리합니다.
  • cleanup(emitter, reason) 공통 함수: onCompletion, onTimeout, onError, send 실패, heartbeat 실패가 모두 이 함수를 통해 정리됩니다. CopyOnWriteArrayList.remove()의 원자적 boolean 반환으로 이중 정리를 방지합니다.
  • heartbeat 실패 시 synchronized 블록 밖에서 cleanup 호출: 락 점유를 최소화하고 cleanup 로직을 send 실패 경로와 동일한 패턴으로 통일했습니다.
  • handleAvatarListChanged에 fallbackExecution = true 추가: DeleteAllAvatarsServiceImpl이 TransactionSynchronization.afterCommit() 내부에서 이벤트를 발행해 활성 트랜잭션이 없는 경우에도 SSE 브로드캐스트가 동작하도록 수정했습니다.
  • heartbeat 주기 30초 → 10초: 브라우저 새로고침 후 죽은 연결이 최대 10초 내에 감지·제거됩니다.

✅ 체크리스트

  • 문서(README, .env.example 등) 변경이 필요한 경우 작성 또는 수정했나요?
  • 작업한 코드가 정상적으로 동작하는 것을 직접 확인했나요?
  • 필요한 경우 테스트 코드를 작성하거나 수정했나요?
  • Merge 대상 브랜치를 올바르게 설정했나요?
  • PR에 관련 없는 작업이 포함되지 않았나요?
  • 적절한 라벨과 리뷰어를 설정했나요?

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cfcromn cfcromn added ✅ Test Test관련사항 ♻️ Refactor 코드 리팩토링 ⚡️performance 성능 개선 labels Jun 7, 2026
@cfcromn cfcromn self-assigned this Jun 7, 2026
@cfcromn
cfcromn requested a review from jyx-07 June 7, 2026 14:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the SSE subscription logic by introducing a dedicated AvatarSseSubscribedEvent to handle initial data pushes, simplifying connection tracking by using the emitters list size instead of an AtomicInteger, and unifying connection cleanup into a single helper method. Additionally, the heartbeat interval has been reduced to 10 seconds. The review feedback highlights a potential resource leak where SseEmitter.complete() is not explicitly called when manually cleaning up failed emitters during heartbeat or send failures. It is recommended to update the cleanup function to support an optional explicit completion flag to ensure the servlet container properly releases associated resources.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@cfcromn

cfcromn commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the SSE subscription mechanism for avatar changes, introducing a dedicated AvatarSseSubscribedEvent, simplifying connection cleanup, and reducing the heartbeat interval to 10 seconds. Feedback on these changes highlights a potential race condition when checking and adding emitters concurrently, the risk of unexpected exceptions interrupting heartbeat and broadcast loops, and unnecessary database queries when no active emitters exist.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@cfcromn
cfcromn merged commit b0cea1f into develop Jun 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️performance 성능 개선 ♻️ Refactor 코드 리팩토링 ✅ Test Test관련사항

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant