-
Notifications
You must be signed in to change notification settings - Fork 0
✅ [Deploy] v0.5.1 배포 #140
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
✅ [Deploy] v0.5.1 배포 #140
Changes from all commits
4041c38
7650d37
4d6be6f
c7f2999
723cb7e
98efdee
7569673
a63b6d8
8c99c82
0d3e58f
d052a5f
5880e28
edfc10e
670853a
52c7ed2
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,27 +1,107 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package com.example.scoi.domain.websocket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.scoi.domain.websocket.code.WebsocketErrorCode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.scoi.domain.websocket.event.WebsocketConnectionEvent.ConnectedEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.scoi.domain.websocket.event.WebsocketConnectionEvent.DisconnectedEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.scoi.domain.websocket.exception.WebsocketException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.scoi.domain.websocket.handler.UpbitTickerHandler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.annotation.PreDestroy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.extern.slf4j.Slf4j; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.beans.factory.ObjectProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.context.event.EventListener; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.retry.annotation.Backoff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.retry.annotation.Recover; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.retry.annotation.Retryable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.scheduling.annotation.Async; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.socket.client.WebSocketClient; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.Executors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ScheduledExecutorService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ScheduledFuture; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @RequiredArgsConstructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Slf4j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class WebsocketConnect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final WebSocketClient webSocketClient; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final UpbitTickerHandler upbitTickerHandler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ObjectProvider<WebsocketConnect> selfProvider; // AOP 우회 방지를 위한 프록시 의존성 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final String PUBLIC_URL = "wss://api.upbit.com/websocket/v1"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 실시간 가격 변동 체크 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 워치독(Half-Open 헬스체크) 전용 스케줄러 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private ScheduledFuture<?> watchdogTask; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventListener(ApplicationReadyEvent.class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void connect(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 디페깅 알고리즘 구동 시작..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| webSocketClient.execute(upbitTickerHandler, PUBLIC_URL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void init() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 애플리케이션 준비 완료."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selfProvider.getObject().connectWithRetry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Async | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Retryable( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| retryFor = WebsocketException.class, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxAttempts = 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 60000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void connectWithRetry() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 업비트 웹소켓 연결 시도 중... URL: {}", PUBLIC_URL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // execute()의 리턴값인 Future를 대기(get)하여 타임아웃이나 연결 거부 시 예외를 발생시키도록 유도 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| webSocketClient.execute(upbitTickerHandler, PUBLIC_URL).get(5, TimeUnit.SECONDS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.error("[ Websocket ]: 웹소켓 연결 실패: {}", e.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 예외를 던져서 @Retryable이 AOP 기반 백오프 및 재시도를 수행하도록 트리거 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new WebsocketException(WebsocketErrorCode.UPBIT_WEBSOCKET_ERROR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+53
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 타임아웃 시 진행 중인 연결 시도가 취소되지 않음.
🔧 제안: 타임아웃 시 future를 명시적으로 취소 public void connectWithRetry() {
log.info("[ Websocket ]: 업비트 웹소켓 연결 시도 중... URL: {}", PUBLIC_URL);
+ java.util.concurrent.Future<WebSocketSession> future = null;
try {
- // execute()의 리턴값인 Future를 대기(get)하여 타임아웃이나 연결 거부 시 예외를 발생시키도록 유도
- webSocketClient.execute(upbitTickerHandler, PUBLIC_URL).get(5, TimeUnit.SECONDS);
+ future = webSocketClient.execute(upbitTickerHandler, PUBLIC_URL);
+ future.get(5, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("[ Websocket ]: 웹소켓 연결 실패: {}", e.getMessage());
+ if (future != null) {
+ future.cancel(true);
+ }
throw new WebsocketException(WebsocketErrorCode.UPBIT_WEBSOCKET_ERROR);
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Recover | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void recover(Exception e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.error("[ Websocket ]: 웹소켓 재연결 최대 시도 횟수(10회) 초과"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 2시간뒤 다시 시도 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduler.schedule(() -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 시스템 복구를 위해 웹소켓 재연결 사이클을 다시 시작합니다."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selfProvider.getObject().connectWithRetry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, 2, TimeUnit.HOURS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventListener | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void onConnected(ConnectedEvent event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 이벤트 수신 - 웹소켓 연결 활성화 완료."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (watchdogTask != null && !watchdogTask.isCancelled()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| watchdogTask.cancel(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 10초마다 마지막 수신 시간 검사 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| watchdogTask = scheduler.scheduleAtFixedRate(() -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| long idleTime = System.currentTimeMillis() - upbitTickerHandler.getLastMessageTime(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (idleTime > 30_000) { // 30초 초과 시 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.error("[ Websocket ]: 30초간 업비트 응답이 없습니다..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upbitTickerHandler.closeSession(); // DisconnectedEvent 유발 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, 10, 10, TimeUnit.SECONDS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventListener | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void onDisconnected(DisconnectedEvent event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.warn("[ Websocket ]: 이벤트 수신 - 웹소켓 연결 해제. 재연결 시도"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (watchdogTask != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| watchdogTask.cancel(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selfProvider.getObject().connectWithRetry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @PreDestroy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void shutdown() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("[ Websocket ]: 워치독 스케줄러 종료 중..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduler.shutdown(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example.scoi.domain.websocket.code; | ||
|
|
||
| import com.example.scoi.global.apiPayload.code.BaseErrorCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum WebsocketErrorCode implements BaseErrorCode { | ||
|
|
||
| UPBIT_WEBSOCKET_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, | ||
| "WEBSOCKET500_1", | ||
| "웹소켓 연결 도중 에러가 발생했습니다."), | ||
| ; | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.example.scoi.domain.websocket.event; | ||
|
|
||
| public interface WebsocketConnectionEvent { | ||
| record ConnectedEvent() implements WebsocketConnectionEvent {} | ||
| record DisconnectedEvent() implements WebsocketConnectionEvent {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.scoi.domain.websocket.exception; | ||
|
|
||
| import com.example.scoi.global.apiPayload.code.BaseErrorCode; | ||
| import com.example.scoi.global.apiPayload.exception.ScoiException; | ||
|
|
||
| public class WebsocketException extends ScoiException { | ||
| public WebsocketException(BaseErrorCode code) { | ||
| super(code); | ||
| } | ||
| } |
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
watchdogTask필드의 스레드 안전성 문제.watchdogTask는volatile이 아니며,onConnected()/onDisconnected()는 이벤트 발행 스레드(WebSocket 컨테이너 스레드 또는@Async스레드)에서 호출될 수 있어 서로 다른 스레드에서 동시 실행될 가능성이 있습니다. check-then-act 패턴(null체크 →cancel→ 재할당)이 원자적이지 않아, 경쟁 상태에서 워치독 취소가 누락되거나 중복 스케줄될 수 있습니다.최소한
volatile선언과 함께, 가능하면synchronized블록이나AtomicReference로 감싸는 것을 권장합니다.Also applies to: 76-100
🤖 Prompt for AI Agents