From 13681b339c61f6967a35e47d7d4345b7d475efd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Thu, 22 Dec 2022 20:34:47 +0100 Subject: [PATCH] fix: simplify code by removing cluster service; fix open handle in tests --- README.md | 48 +++++-------------- ...throttler-storage-redis-cluster.service.ts | 10 ---- .../controllers/cluster-controller.module.ts | 6 +-- test/controller.e2e-spec.ts | 14 +++--- 4 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 src/throttler-storage-redis-cluster.service.ts diff --git a/README.md b/README.md index 18e28939..dcb8c057 100644 --- a/README.md +++ b/README.md @@ -27,53 +27,31 @@ import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis'; ThrottlerModule.forRoot({ ttl: 60, limit: 5, + + // Below are possible options on how to configure the storage service. + + // default config (host = localhost, port = 6379) storage: new ThrottlerStorageRedisService(), - }), - ], -}) -export class AppModule {} -``` -Inject another config module and service: + // connection url + storage: new ThrottlerStorageRedisService('redis://'), -```ts -import { ThrottlerModule } from '@nestjs/throttler'; -import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis'; + // redis object + storage: new ThrottlerStorageRedisService(new Redis()), -@Module({ - imports: [ - ThrottlerModule.forRootAsync({ - imports: [ConfigModule], - inject: [ConfigService], - useFactory: (config: ConfigService) => ({ - ttl: config.get('THROTTLE_TTL'), - limit: config.get('THROTTLE_LIMIT'), - storage: new ThrottlerStorageRedisService(), - }), + // redis clusters + storage: new ThrottlerStorageRedisService(new Redis.Cluster(nodes, options)), }), ], }) export class AppModule {} ``` -Using redis clusters: +Inject another config module and service: ```ts import { ThrottlerModule } from '@nestjs/throttler'; -import { ThrottlerStorageRedisClusterService } from 'nestjs-throttler-storage-redis'; - -const nodes = [ - { host: '127.0.0.1', port: 7000 }, - { host: '127.0.0.1', port: 7001 }, - ... - { host: '127.0.0.1', port: 7005 }, -]; - -const options = { - redisOptions: { - password: 'your-redis-password' - } -}; +import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis'; @Module({ imports: [ @@ -83,7 +61,7 @@ const options = { useFactory: (config: ConfigService) => ({ ttl: config.get('THROTTLE_TTL'), limit: config.get('THROTTLE_LIMIT'), - storage: new ThrottlerStorageRedisClusterService(nodes, options), + storage: new ThrottlerStorageRedisService(), }), }), ], diff --git a/src/throttler-storage-redis-cluster.service.ts b/src/throttler-storage-redis-cluster.service.ts deleted file mode 100644 index 641b7da8..00000000 --- a/src/throttler-storage-redis-cluster.service.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import Redis, { ClusterNode, ClusterOptions } from 'ioredis'; -import { ThrottlerStorageRedisService } from './throttler-storage-redis.service'; - -@Injectable() -export class ThrottlerStorageRedisClusterService extends ThrottlerStorageRedisService { - constructor(nodes: ClusterNode[], options?: ClusterOptions) { - super(new Redis.Cluster(nodes, options)); - } -} diff --git a/test/app/controllers/cluster-controller.module.ts b/test/app/controllers/cluster-controller.module.ts index 4edfbfd2..5cf66a62 100644 --- a/test/app/controllers/cluster-controller.module.ts +++ b/test/app/controllers/cluster-controller.module.ts @@ -1,7 +1,7 @@ import { Module } from '@nestjs/common'; import { ThrottlerModule } from '@nestjs/throttler'; -import { ThrottlerStorageRedisClusterService } from '../../../src/throttler-storage-redis-cluster.service'; -import { clusterNodes } from '../../utility/redis'; +import { ThrottlerStorageRedisService } from '../../../src'; +import { cluster } from '../../utility/redis'; import { AppService } from '../app.service'; import { AppController } from './app.controller'; import { DefaultController } from './default.controller'; @@ -13,7 +13,7 @@ import { LimitController } from './limit.controller'; limit: 5, ttl: 60, ignoreUserAgents: [/throttler-test/g], - storage: new ThrottlerStorageRedisClusterService(clusterNodes), + storage: new ThrottlerStorageRedisService(cluster), }), ], controllers: [AppController, DefaultController, LimitController], diff --git a/test/controller.e2e-spec.ts b/test/controller.e2e-spec.ts index f109f4d5..e87e383c 100644 --- a/test/controller.e2e-spec.ts +++ b/test/controller.e2e-spec.ts @@ -8,7 +8,7 @@ import Redis, { Cluster } from 'ioredis'; import { ClusterControllerModule } from './app/controllers/cluster-controller.module'; import { ControllerModule } from './app/controllers/controller.module'; import { httPromise } from './utility/httpromise'; -import { redis, cluster } from './utility/redis'; +import { cluster, redis } from './utility/redis'; async function flushdb(redisOrCluster: Redis | Cluster) { if (redisOrCluster instanceof Redis) { @@ -23,13 +23,11 @@ async function flushdb(redisOrCluster: Redis | Cluster) { describe.each` instance | instanceType ${redis} | ${'single'} - ${cluster} | ${'cluster'} + ${cluster} | ${'single'} `('Redis $instanceType instance', ({ instance: redisOrCluster }: { instance: Redis | Cluster }) => { afterAll(async () => { - if (redisOrCluster instanceof Cluster) { - redisOrCluster.disconnect(); - } + await redisOrCluster.quit(); }); describe.each` @@ -42,7 +40,7 @@ describe.each` beforeAll(async () => { await flushdb(redisOrCluster); const config = { - imports: [ControllerModule], + imports: [], providers: [ { provide: APP_GUARD, @@ -52,7 +50,9 @@ describe.each` }; if (redisOrCluster instanceof Cluster) { - config.imports = [ClusterControllerModule]; + config.imports.push(ClusterControllerModule); + } else { + config.imports.push(ControllerModule); } const moduleFixture: TestingModule = await Test.createTestingModule(config).compile();