-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor : SSE 리펙터링 및 Test 환경 Scheduler에 의한 테스트 지연 및 예외처리 상황 개선 (#121)
* refactor : sseRepo에 code가 존재하지 않을 시 빈 배열 반환 * refactor : 마지막 개행 추가 * refactor : sse repo 내부 구현 변경 Map<String, SseEmitter> -> Map<String, Map<String, SseEmitter>> * refactor : sse 구독 aspect 분리 * feat : 게임 삭제시 sse delete 추가 * feat : CORS origin dev 서버 추가 * refactor : 코드 컨벤션 적용 및 정리 * refactor : test 환경에서 scheduler 분리 * refactor: 코드 리뷰 반영 (이중 for문 처리 및 cors 추가)
- Loading branch information
1 parent
14ec567
commit 3b18d17
Showing
21 changed files
with
240 additions
and
143 deletions.
There are no files selected for viewing
Submodule backend-submodule
updated
from 79b966 to 7ebcff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/mafia/mafiatogether/common/config/SchedulerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mafia.mafiatogether.common.config; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
@ConditionalOnProperty(value = "application.scheduling-enable", havingValue = "true", matchIfMissing = false) | ||
public class SchedulerConfig { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/mafia/mafiatogether/game/annotation/SseSubscribe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mafia.mafiatogether.game.annotation; | ||
import java.lang.annotation.*; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SseSubscribe { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/mafia/mafiatogether/game/aspect/SseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package mafia.mafiatogether.game.aspect; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mafia.mafiatogether.common.annotation.PlayerInfo; | ||
import mafia.mafiatogether.common.exception.AuthException; | ||
import mafia.mafiatogether.common.exception.ExceptionCode; | ||
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse; | ||
import mafia.mafiatogether.game.domain.Game; | ||
import mafia.mafiatogether.game.domain.GameRepository; | ||
import mafia.mafiatogether.game.domain.SseEmitterRepository; | ||
import mafia.mafiatogether.game.domain.status.StatusType; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SseService { | ||
|
||
private static final String SSE_STATUS = "gameStatus"; | ||
public static final long HOURS_12 = 43200_000L; | ||
public static final long SECOND_30 = 30_000L; | ||
private final SseEmitterRepository sseEmitterRepository; | ||
private final GameRepository gameRepository; | ||
|
||
@Around("@annotation(mafia.mafiatogether.game.annotation.SseSubscribe)") | ||
public ResponseEntity<SseEmitter> subscribe(final ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = methodSignature.getMethod(); | ||
|
||
Annotation[][] parameterAnnotations = method.getParameterAnnotations(); | ||
Object[] args = joinPoint.getArgs(); | ||
|
||
String[] codeAndName = new String[2]; | ||
for (int i = 0; i < parameterAnnotations.length; i++) { | ||
if (hasPlayerInfo(parameterAnnotations[i])) { | ||
codeAndName[0] = args[i].toString(); | ||
codeAndName[1] = args[i].toString(); | ||
break; | ||
} | ||
} | ||
|
||
if (codeAndName[0] == null || codeAndName[1] == null) { | ||
throw new AuthException(ExceptionCode.INVALID_AUTHENTICATION_FORM); | ||
} | ||
|
||
SseEmitter sseEmitter = createSseEmitter(codeAndName[0], codeAndName[1]); | ||
sseEmitterRepository.save(codeAndName[0], codeAndName[1], sseEmitter); | ||
return ResponseEntity.ok(sseEmitter); | ||
} | ||
|
||
private boolean hasPlayerInfo(Annotation[] annotations) { | ||
return Arrays.stream(annotations).anyMatch(PlayerInfo.class::isInstance); | ||
} | ||
|
||
private SseEmitter createSseEmitter(String code, String name) throws IOException { | ||
SseEmitter sseEmitter = new SseEmitter(HOURS_12); | ||
sseEmitter.send(getSseEvent(code)); | ||
sseEmitter.onCompletion(() -> sseEmitterRepository.deleteByCodeAndEmitter(code, name)); | ||
sseEmitter.onTimeout(sseEmitter::complete); | ||
return sseEmitter; | ||
} | ||
|
||
private SseEmitter.SseEventBuilder getSseEvent(final String code) { | ||
Optional<Game> game = gameRepository.findById(code); | ||
return game.map(value -> getSseEventBuilder(value.getStatus().getType())) | ||
.orElseGet(() -> getSseEventBuilder(StatusType.WAIT)); | ||
} | ||
|
||
private static SseEmitter.SseEventBuilder getSseEventBuilder(final StatusType statusType) { | ||
return SseEmitter.event() | ||
.name(SSE_STATUS) | ||
.data(new GameStatusResponse(statusType)) | ||
.reconnectTime(SECOND_30); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.