Skip to content
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

refactor: 게임 엔진 서비스의 Busy Waiting 문제 개선 #83

Conversation

Jeongjjuna
Copy link
Contributor

#️⃣ 관련 이슈

📝 작업한 내용

  • 게임 엔진 서비스의 Busy Waiting 문제 개선

💬 논의하고 싶은 내용

  • Blocking Pop 연산을 활용해서 데이터가 들어올 때까지 대기
  • 기본적으로 1초 동안 대기하고, 없으면 넘어갑니다
  • 애플리케이션 로직상 계속해서 Blocking으로 대기하고 있으면, 퀴즈 한문제당 대시시간을 지났는지 아닌지를 판단할 수 없기 때문에, 1초마다 Qeueu를 확인하도록 했습니다.
  • 대기시간 1초를 지정해줄 때 infrastructure에 매직넘버로 적어주었는데, 서비스 계층에서 값을 전달해줄 수 있게 개선해보면 좋을 것 같습니다.

- Blocking Pop 연산을 활용해서 데이터가 들어올 때까지 대기
- 기본적으로 1초 동안 대기하고, 없으면 넘어갑니다
- 애플리케이션 로직상 계속해서 Blocking으로 대기하고 있으면, 퀴즈 한문제당 대시시간을 지났는지 아닌지를 판단할 수 없기 때문에, 1초마다 Qeueu를 확인하도록 했습니다.
- 대기시간 1초를 지정해줄 때 infrastructure에 매직넘버로 적어주었는데, 서비스 계층에서 값을 전달해줄 수 있게 개선해보면 좋을 것 같습니다.
@Jeongjjuna Jeongjjuna added 🚀 BE 우리는 백엔드 개발자! 🛠 Refactor 리팩토링을 해봐요! labels Sep 9, 2024
@Jeongjjuna Jeongjjuna self-assigned this Sep 9, 2024
@Jeongjjuna Jeongjjuna linked an issue Sep 9, 2024 that may be closed by this pull request
1 task
Copy link
Member

@youjungHwang youjungHwang left a comment

Choose a reason for hiding this comment

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

수고하셨습니다!

@@ -23,7 +23,7 @@ class QueueRepositoryAdapter(

override fun poll(roomId: Long, quizId: Long): AnswerResult? {
val key = "roomId : " + roomId.toString() + ", " + "quizId : " + quizId.toString()
Copy link
Member

Choose a reason for hiding this comment

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

만약 key가 여러 곳에서 쓰인다면 분리해보는 것도 좋을 것 같습니다.😀😀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맞아요 레디스에서 다양한 키를 사용할 수 있고, 키 명명 규칙도 존재하기 때문에 별도의 파일에서 관리해주면 좋을 것 같습니다!!

Copy link
Member

Choose a reason for hiding this comment

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

좋습니다! 이렇게 이슈가 늘어나는 거죠! ㅎㅎ 여기저기 쓰이면서 명명 규칙이 혼재해서 사용될 수 있기 때문에 분리해서 관리하는 게 좋을 것 같습니다.

@@ -23,7 +23,7 @@ class QueueRepositoryAdapter(

override fun poll(roomId: Long, quizId: Long): AnswerResult? {
val key = "roomId : " + roomId.toString() + ", " + "quizId : " + quizId.toString()
return redisQueueRepository.poll(key)?.let {
return redisQueueRepository.poll(key, 1)?.let {
Copy link
Member

Choose a reason for hiding this comment

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

이 부분이 1초를 대기하고 poll한다는 의미일까요?🤗🤗

Copy link
Contributor Author

@Jeongjjuna Jeongjjuna Sep 10, 2024

Choose a reason for hiding this comment

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

1초동안 블럭킹으로 대기를하고, 없으면 null을 반환하도록 했습니다!
무한으로 blocking 해버리면 대기시간 10초를 카운트해서 다음 문제로 넘어가야하는데 그부분에서 문제가 생길 거같아서 일단 1초로 지정했습니다
이부분 한번 같이 이야기해보면 좋을거같아요!

Copy link
Member

Choose a reason for hiding this comment

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

네네 이해했습니다. 그리고 직접 게임 해보니까 10초가 참 짧게 느껴졌습니다. 대기시간 10초를 없애는 것도 같이 고려해봐도 좋을 것 같아요😀😀

@@ -12,8 +13,8 @@ class RedisQueueRepository(
return redisTemplate.opsForList().rightPush(key, value)
}

fun poll(key: String): String? {
return redisTemplate.opsForList().leftPop(key)
fun poll(key: String, timeout: Long = 60, timeUnit: TimeUnit = TimeUnit.SECONDS): String? {
Copy link
Member

Choose a reason for hiding this comment

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

timeout, timeunit이 어떤 차이가 있을까요? 🤗🤗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

timeout은 몇초동안 blocking을 할지 지정하는 파라미터이고, TimeUnit은 timout에 적어준 값의 기준값을 의미합니다.
예를들어 위의 코드에서 SECONDS로 설정되어있기 때문에, timout값이 10이라면 10초로 설정되는 것입니다!! 😀😀

Copy link
Member

Choose a reason for hiding this comment

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

친절하게 설명해주셔서 감사해요! 이해가 쏙쏙!! 되었습니다!

Comment on lines +40 to +42
private val redis: GenericContainer<*> = GenericContainer(DockerImageName.parse("redis:latest"))
.withExposedPorts(6379)
.withReuse(true)
Copy link
Member

Choose a reason for hiding this comment

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

여기서 GenericContainer는 어떤 역할을 하는지 여쭤봐도 될까요?😆😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

해당 구문은 컨테이너를 활용하기 위해 사용하였는데요, 특정 버전의 redis 이미지를 가져오고 컨테이너화 시킬 때 사용되는 클래스입니다! 😀😀

Copy link
Member

Choose a reason for hiding this comment

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

컨테이너화... 오 좋은 구문이 많네요! 이렇게 또 배워갑니다!😀😀

gameAnswerQueueService.add(answerResult)
}
} catch (e: Exception) {
println("Error: ${e.message}")
Copy link
Member

Choose a reason for hiding this comment

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

편의를 위해서 print문을 쓰신 걸까요? 그렇다면 남겨두는 것도 좋을 것 같습니다. 😁😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 편의를 위해서 사용하였는데, 혹시 나중에 성능적으로 문제가 될 것 같으면 로그를 찍는 방식으로 수정해봐도 좋을 것 같아요!!

Copy link
Member

Choose a reason for hiding this comment

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

네네 알겠습니다. 나중에 print문이 있는 파트를 다 찾아서 로그로 바꿔봐도 좋을 것 같아요!

@Jeongjjuna Jeongjjuna merged commit 170cb8e into develop Sep 10, 2024
1 check passed
@Jeongjjuna Jeongjjuna deleted the refactor/#76-게임엔진서비스의BusyWaiting문제개선 branch September 10, 2024 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 BE 우리는 백엔드 개발자! 🛠 Refactor 리팩토링을 해봐요!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor: 게임 엔진 서비스의 busy waiting 문제 개선
2 participants