Skip to content

Commit 1be2935

Browse files
committed
feat: added throttling of requests
1 parent 63553d2 commit 1be2935

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@nestjs/platform-express": "^9.1.6",
3535
"@nestjs/swagger": "^6.1.2",
3636
"@nestjs/terminus": "^9.1.4",
37+
"@nestjs/throttler": "^6.2.1",
3738
"cache-manager": "^5.7.6",
3839
"class-transformer": "^0.5.1",
3940
"class-validator": "^0.13.2",

src/app.module.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { ConfigService } from './config/config/config.service';
99
import { LoggerModule } from 'nestjs-pino';
1010
import { v4 as uuidv4 } from 'uuid';
1111
import { CachingModule } from './caching/caching.module';
12+
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
13+
import { APP_GUARD } from '@nestjs/core';
1214

1315
@Module({
1416
imports: [
@@ -43,6 +45,22 @@ import { CachingModule } from './caching/caching.module';
4345
},
4446
}),
4547
CachingModule,
48+
ThrottlerModule.forRootAsync({
49+
imports: [ConfigModule],
50+
inject: [ConfigService],
51+
useFactory: (config: ConfigService) => [
52+
{
53+
ttl: config.get('throttle.ttl'),
54+
limit: config.get('throttle.limit'),
55+
},
56+
],
57+
}),
58+
],
59+
providers: [
60+
{
61+
provide: APP_GUARD,
62+
useClass: ThrottlerGuard,
63+
},
4664
],
4765
})
4866
export class AppModule implements NestModule {

src/config/schema/config.schema.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const schema: convict.Schema<AppConfig> = {
8686
},
8787
cache: {
8888
ttl: {
89-
doc: 'TTL in miliseconds for caching the results',
89+
doc: 'TTL in milliseconds for caching the results',
9090
format: 'Number',
9191
default: 5000,
9292
env: 'CACHE_TTL',
@@ -100,4 +100,20 @@ export const schema: convict.Schema<AppConfig> = {
100100
arg: 'cache_max_entries',
101101
},
102102
},
103+
throttle: {
104+
ttl: {
105+
doc: 'TTL in milliseconds for throttling the requests',
106+
format: 'Number',
107+
default: 60000,
108+
env: 'THROTTLE_TTL',
109+
arg: 'throttle_ttl',
110+
},
111+
limit: {
112+
doc: 'Limit for throttling the requests',
113+
format: 'Number',
114+
default: 1000,
115+
env: 'THROTTLE_LIMIT',
116+
arg: 'throttle_limit',
117+
},
118+
},
103119
};

src/jobs/controllers/jobs/jobs.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ApiBody, ApiOperation } from '@nestjs/swagger';
1414
import { Job, PatchJob } from '../../models/job.dto';
1515
import { DatabaseService } from '../../services/database/database.service';
1616
import { CachingService } from '../../../caching/services/cache.service';
17+
import { Throttle } from '@nestjs/throttler';
1718

1819
@Controller('jobs')
1920
export class JobsController {

src/jobs/jobs.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ConfigModule } from '../config/config.module';
55
import { ConfigService } from '../config/config/config.service';
66
import { DatabaseService } from './services/database/database.service';
77
import { CachingModule } from '../caching/caching.module';
8+
import { ThrottlerModule } from '@nestjs/throttler';
89

910
@Module({
1011
controllers: [JobsController],

0 commit comments

Comments
 (0)