Skip to content

Commit

Permalink
✨ feat: flush 할 때 redis를 이용해 락킹
Browse files Browse the repository at this point in the history
  • Loading branch information
gamgyul163 committed Nov 26, 2024
1 parent 1754d97 commit ba75b78
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions Backend/apps/api/src/chats/chats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { firstValueFrom } from 'rxjs';

@Injectable()
export class ChatsService {
private isFlushing = false;
constructor(
@InjectRedis() private readonly redisClient: Redis,
private readonly httpService: HttpService,
Expand Down Expand Up @@ -37,7 +36,7 @@ export class ChatsService {
filteringResult: true,
};
const chatString = JSON.stringify(chat);
await this.redisClient.multi().publish(`${channelId}:chat`, chatString).lpush('chatQueue', chatId).exec();
await this.redisClient.multi().publish(`${channelId}:chat`, chatString).rpush('chatQueue', chatId).exec();
this.clovaFiltering(chat);
}

Expand Down Expand Up @@ -85,19 +84,26 @@ export class ChatsService {
}

async flushChat() {
if (!this.isFlushing) {
this.isFlushing = true;
while (true) {
const frontChatId = await this.redisClient.lindex('chatQueue', 0);
const chatString = await this.redisClient.hget('chatCache', frontChatId);
if (!chatString) {
break;
} else {
const chat = JSON.parse(chatString);
await this.redisClient.multi().rpush(`${chat.channelId}:chats`, chatString).lpop('chatQueue').exec();
const lockKey = 'chat:flush:lock';
const lock = await this.redisClient.set(lockKey, 'lock', 'NX');

try {
if (lockKey) {
while (true) {
const frontChatId = await this.redisClient.lindex('chatQueue', 0);
const chatString = await this.redisClient.hget('chatCache', frontChatId);
if (!chatString) {
break;
} else {
const chat = JSON.parse(chatString);
await this.redisClient.multi().rpush(`${chat.channelId}:chats`, chatString).lpop('chatQueue').exec();
}
}
}
this.isFlushing = false;
} catch (err) {
console.log(err);
} finally {
await this.redisClient.del(lockKey);
}
}
}

0 comments on commit ba75b78

Please sign in to comment.