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

[BE] fix-room-clean #298

Merged

Conversation

DongHoonYu96
Copy link
Collaborator

@DongHoonYu96 DongHoonYu96 commented Dec 2, 2024

๐Ÿ”Ž ์ž‘์—… ๋‚ด์šฉ

์ด Pull Request๋Š” Redis ๊ธฐ๋ฐ˜ ๊ฒŒ์ž„ ์„œ๋น„์Šค์—์„œ ๋น„ํ™œ์„ฑ ๋ฐฉ ์ฒ˜๋ฆฌ์™€ ์ •๋ฆฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ์—…๋ฐ์ดํŠธ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ๋น„ํ™œ์„ฑ ๋ฐฉ ํ™•์ธ์„ ์œ„ํ•œ ์ƒˆ๋กœ์šด ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€, ๋ฐฉ ์ •๋ฆฌ ํ”„๋กœ์„ธ์Šค ์ˆ˜์ •, ๊ทธ๋ฆฌ๊ณ  ์ค‘๋ณต ์ฝ”๋“œ ์ œ๊ฑฐ์ž…๋‹ˆ๋‹ค.

  • ๋น„ํ™œ์„ฑ ๋ฐฉ ์ฒ˜๋ฆฌ ๊ฐœ์„ :
    BE/src/game/redis/game-redis-memory.service.ts: cron ์ž‘์—…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋น„ํ™œ์„ฑ ๋ฐฉ์„ ํ™•์ธํ•˜๊ณ  ์ •๋ฆฌํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฉ”์†Œ๋“œ checkInactiveRooms๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” Redis SCAN์„ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ ๋ฐฉ ์„ธํŠธ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฉ ์ •๋ฆฌ ํ”„๋กœ์„ธ์Šค ๊ฐ•ํ™”:
    BE/src/game/redis/subscribers/room.cleanup.subscriber.ts: ๋ฐฉ ์ •๋ฆฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ๋ฐฉ ๋ฐ์ดํ„ฐ ์ž์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ๋ฐฉ์˜ ํ”Œ๋ ˆ์ด์–ด ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€์„œ ๊ทธ๋“ค์˜ ๋ฐ์ดํ„ฐ๋„ ์‚ญ์ œํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์ฝ”๋“œ ์ •๋ฆฌ ๋ฐ ๋ฆฌํŒฉํ† ๋ง:
    BE/src/game/service/game.room.service.ts: GameRedisMemoryService์˜ ์ƒˆ๋กœ์šด ๊ตฌํ˜„์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์˜ checkInactiveRooms ๋ฉ”์†Œ๋“œ์™€ ๊ด€๋ จ cron ์ž‘์—…์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์‚ฌ์†Œํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ:
    BE/src/game/redis/game-redis-memory.service.ts: ์ผ๊ด€๋œ ๊ตฌ์กฐ๋ฅผ ๋”ฐ๋ฅด๋„๋ก imports ์ˆœ์„œ๋ฅผ ์žฌ์ •๋ ฌํ–ˆ์Šต๋‹ˆ๋‹ค.
    BE/src/game/service/game.room.service.ts: ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” import ๊ตฌ๋ฌธ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ–ผ ์ฐธ๊ณ  ์ด๋ฏธ์ง€

  • ํ‡ด์žฅ์ „
    image
  • ๋งˆ์ง€๋ง‰ ํ”Œ๋ ˆ์ด์–ด ํ‡ด์žฅํ›„
    image

๐ŸŽฏ ๋ฆฌ๋ทฐ ์š”๊ตฌ์‚ฌํ•ญ (์„ ํƒ)

  • ํŠน๋ณ„ํžˆ ๋ด์คฌ์œผ๋ฉด ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ ์–ด์ฃผ์„ธ์š”

โœ… Check List

  • mergeํ•  ๋ธŒ๋žœ์น˜์˜ ์œ„์น˜๋ฅผ ํ™•์ธํ–ˆ๋‚˜์š”?
  • Label์„ ์ง€์ •ํ–ˆ๋‚˜์š”?

@DongHoonYu96 DongHoonYu96 self-assigned this Dec 2, 2024
@DongHoonYu96 DongHoonYu96 requested a review from Copilot December 2, 2024 09:42
@DongHoonYu96 DongHoonYu96 added the bug Something isn't working label Dec 2, 2024
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 suggestion.

Comments skipped due to low confidence (1)

BE/src/game/redis/game-redis-memory.service.ts:25

  • There is a redundant comment for the checkInactiveRooms method. The comment on line 25 can be removed.
/**

@@ -37,6 +37,15 @@ export class RoomCleanupSubscriber extends RedisSubscriber {
try {
const pipeline = this.redis.pipeline();

// 1. ๋ฐฉ์— ์†ํ•œ ํ”Œ๋ ˆ์ด์–ด ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ, 200๋ช…๋ฏธ๋งŒ -> smembers ์‚ฌ์šฉ!
Copy link
Preview

Copilot AI Dec 2, 2024

Choose a reason for hiding this comment

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

The comment has a minor spelling issue. It should be '200๋ช… ๋ฏธ๋งŒ -> smembers ์‚ฌ์šฉ!' instead of '200๋ช…๋ฏธ๋งŒ -> smembers ์‚ฌ์šฉ!'.

Suggested change
// 1. ๋ฐฉ์— ์†ํ•œ ํ”Œ๋ ˆ์ด์–ด ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ, 200๋ช…๋ฏธ๋งŒ -> smembers ์‚ฌ์šฉ!
// 1. ๋ฐฉ์— ์†ํ•œ ํ”Œ๋ ˆ์ด์–ด ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ, 200๋ช… ๋ฏธ๋งŒ -> smembers ์‚ฌ์šฉ!

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Collaborator

@NewCodes7 NewCodes7 left a comment

Choose a reason for hiding this comment

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

๐Ÿ‘๐Ÿ‘

Comment on lines +29 to +69
@Cron(CronExpression.EVERY_10_MINUTES)
async checkInactiveRooms(): Promise<void> {
this.logger.verbose('๋น„ํ™œ์„ฑ ๋ฐฉ ์ฒดํฌ ์‹œ์ž‘');
try {
const now = Date.now();
let cursor = '0';
let processedCount = 0;

do {
// SCAN์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ
const [nextCursor, rooms] = await this.redis.sscan(
REDIS_KEY.ACTIVE_ROOMS,
cursor,
'COUNT',
this.BATCH_SIZE
);
cursor = nextCursor;

// ๋ณ‘๋ ฌ๋กœ ๋ฐฉ ์ƒํƒœ ์ฒดํฌ ๋ฐ ์ฒ˜๋ฆฌ
await Promise.all(
rooms.map(async (roomId) => {
try {
const lastActivity = await this.redis.hget(REDIS_KEY.ROOM(roomId), 'lastActivityAt');

if (lastActivity && now - parseInt(lastActivity) > this.INACTIVE_THRESHOLD) {
await this.redis.publish('room:cleanup', roomId);
this.logger.verbose(`๋น„ํ™œ์„ฑ์œผ๋กœ ์ธํ•ด ๋ฐฉ ${roomId} ์ •๋ฆฌ ์‹œ์ž‘`);
processedCount++;
}
} catch (error) {
this.logger.error(`๋ฐฉ ${roomId} ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${error.message}`);
}
})
);
} while (cursor !== '0');

this.logger.verbose(`๋น„ํ™œ์„ฑ ๋ฐฉ ์ฒดํฌ ์™„๋ฃŒ: ${processedCount}๊ฐœ ๋ฐฉ ์ •๋ฆฌ๋จ`);
} catch (error) {
this.logger.error(`๋น„ํ™œ์„ฑ ๋ฐฉ ์ฒดํฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${error.message}`);
}
}
Copy link
Collaborator

@NewCodes7 NewCodes7 Dec 2, 2024

Choose a reason for hiding this comment

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

P3: try/catch ๋ฐ”๊นฅ์— ํ•˜๋‚˜๋งŒ ๋‘ฌ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
(์ทจ์†Œ)

Copy link
Collaborator

@NewCodes7 NewCodes7 Dec 2, 2024

Choose a reason for hiding this comment

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

์•„ ๊ทธ๋Ÿฐ๋ฐ ๋ฐ”๊นฅ์— ํ•˜๋‚˜๋งŒ ๋‘๋ฉด rooms.map ๋Œ๋‹ค๊ฐ€ ์—๋Ÿฌ ๋‚ฌ์„ ๋•Œ ๋‹ค์Œ ์ปค์„œ๋กœ ๋ชป ๊ฐ€๊ฒ ๋„ค์š”.

๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๋Œ€์‹ ์— ์•„๋ž˜ ๋ถ€๋ถ„์„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? depth๊ฐ€ ๋„ˆ๋ฌด ๊นŠ์–ด๋ณด์—ฌ์„œ์š”!

async (roomId) => {
  try {
    const lastActivity = await this.redis.hget(REDIS_KEY.ROOM(roomId), 'lastActivityAt');

    if (lastActivity && now - parseInt(lastActivity) > this.INACTIVE_THRESHOLD) {
      await this.redis.publish('room:cleanup', roomId);
      this.logger.verbose(`๋น„ํ™œ์„ฑ์œผ๋กœ ์ธํ•ด ๋ฐฉ ${roomId} ์ •๋ฆฌ ์‹œ์ž‘`);
      processedCount++;
    }
  } catch (error) {
    this.logger.error(`๋ฐฉ ${roomId} ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${error.message}`);
  }
})

Copy link
Collaborator

@songbuild00 songbuild00 left a comment

Choose a reason for hiding this comment

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

๐Ÿ‘

Copy link
Collaborator

@ijun17 ijun17 left a comment

Choose a reason for hiding this comment

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

LGTM!

@DongHoonYu96 DongHoonYu96 merged commit 51c0ce2 into boostcampwm-2024:dev-be Dec 2, 2024
@DongHoonYu96 DongHoonYu96 deleted the fix-be-room-clean branch December 3, 2024 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants