Skip to content

Commit

Permalink
fix: disconnect redis to avoid open handles
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelmesdocteurs committed Nov 5, 2021
1 parent 26c09ab commit 52470fa
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/throttler-storage-redis.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Injectable } from '@nestjs/common';
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import * as Redis from 'ioredis';
import { ThrottlerStorageRedis } from './throttler-storage-redis.interface';

@Injectable()
export class ThrottlerStorageRedisService implements ThrottlerStorageRedis {
export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnModuleDestroy {
redis: Redis.Redis;
disconnectRequired?: boolean;
scanCount: number;

constructor(redis?: Redis.Redis, scanCount?: number);
Expand All @@ -17,8 +18,10 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis {
this.redis = redisOrOptions;
} else if (typeof redisOrOptions === 'string') {
this.redis = new Redis(redisOrOptions as string);
this.disconnectRequired = true;
} else {
this.redis = new Redis(redisOrOptions);
this.disconnectRequired = true;
}
}

Expand All @@ -30,4 +33,10 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis {
async addRecord(key: string, ttl: number): Promise<void> {
await this.redis.set(`${key}:${Date.now() + ttl * 1000}`, ttl, 'EX', ttl);
}

onModuleDestroy() {
if (this.disconnectRequired) {
this.redis?.disconnect(false);
}
}
}

0 comments on commit 52470fa

Please sign in to comment.