Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
fix: simplify code by removing cluster service; fix open handle in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Dec 22, 2022
1 parent 0f8ad1e commit 13681b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
48 changes: 13 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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(),
}),
}),
],
Expand Down
10 changes: 0 additions & 10 deletions src/throttler-storage-redis-cluster.service.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/app/controllers/cluster-controller.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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],
Expand Down
14 changes: 7 additions & 7 deletions test/controller.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`
Expand All @@ -42,7 +40,7 @@ describe.each`
beforeAll(async () => {
await flushdb(redisOrCluster);
const config = {
imports: [ControllerModule],
imports: [],
providers: [
{
provide: APP_GUARD,
Expand All @@ -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();
Expand Down

0 comments on commit 13681b3

Please sign in to comment.