Skip to content

Commit

Permalink
πŸš‘ !HOTFIX: μ‹€ν–‰ μ‹œμ μ—μ„œ connection μΆ”κ°€ 둜직 μž‘μ„±
Browse files Browse the repository at this point in the history
  • Loading branch information
sieunie committed Dec 4, 2024
1 parent 4f7258d commit 7f55d61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
29 changes: 25 additions & 4 deletions BE/src/common/redis/redis.domain-service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Injectable, Inject } from '@nestjs/common';
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisDomainService {
export class RedisDomainService implements OnModuleInit {
constructor(
@Inject('REDIS_CLIENT') private readonly redis: Redis,
@Inject('REDIS_PUBLISHER') private readonly publisher: Redis,
@Inject('REDIS_SUBSCRIBER') private readonly subscriber: Redis,
) {}

async onModuleInit() {
const keys = await this.redis.keys('connections:*');
if (keys.length > 0) await this.redis.del(keys);
}

async exists(key: string): Promise<number> {
return this.redis.exists(key);
}
Expand Down Expand Up @@ -82,11 +87,27 @@ export class RedisDomainService {
return this.subscriber.unsubscribe(channel);
}

async setConnection(key: string, value: number) {
return this.redis.set(`connections:${key}`, value);
}

async getConnection(key: string): Promise<number> {
return Number(await this.redis.get(`connections:${key}`));
}

async delConnection(key: string) {
return this.redis.del(`connections:${key}`);
}

async existsConnection(key: string) {
return this.redis.exists(`connections:${key}`);
}

async increment(key: string) {
return this.redis.incr(key);
return this.redis.incr(`connections:${key}`);
}

async decrement(key: string) {
return this.redis.decr(key);
return this.redis.decr(`connections:${key}`);
}
}
10 changes: 5 additions & 5 deletions BE/src/stockSocket/stock-price-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export class StockPriceSocketService extends BaseStockSocketDomainService {

async subscribeByCode(trKey: string) {
// 아무 μ„œλ²„λ„ ν•œνˆ¬μ™€ ꡬ독 쀑이지 μ•Šμ„λ•Œ
if (!(await this.redisDomainService.exists(trKey))) {
if (!(await this.redisDomainService.existsConnection(trKey))) {
this.baseSocketDomainService.registerCode(this.TR_ID, trKey);
await this.redisDomainService.subscribe(`stock/${trKey}`);
this.register.push(trKey);
await this.redisDomainService.set(trKey, 1);
await this.redisDomainService.setConnection(trKey, 1);
this.connection[trKey] = 1;

return;
Expand Down Expand Up @@ -113,16 +113,16 @@ export class StockPriceSocketService extends BaseStockSocketDomainService {
}

// λ ˆλ””μŠ€ λ‚΄μ—μ„œ λͺ¨λ“  연결이 μ’…λ£Œλμ„ 경우
if ((await this.redisDomainService.get(trKey)) === 0) {
await this.redisDomainService.del(trKey);
if ((await this.redisDomainService.getConnection(trKey)) === 0) {
await this.redisDomainService.delConnection(trKey);
}
}
}

@Cron('*/5 * * * *')
async checkConnection() {
for (const trKey of this.register) {
if (!(await this.redisDomainService.exists(trKey))) {
if (!(await this.redisDomainService.existsConnection(trKey))) {
this.baseSocketDomainService.unregisterCode(this.TR_ID, trKey);
const idx = this.register.indexOf(trKey);
if (idx) this.register.splice(idx, 1);
Expand Down

0 comments on commit 7f55d61

Please sign in to comment.