Skip to content

Commit

Permalink
feat: added throttling of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Dec 2, 2024
1 parent 63553d2 commit 1be2935
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@nestjs/platform-express": "^9.1.6",
"@nestjs/swagger": "^6.1.2",
"@nestjs/terminus": "^9.1.4",
"@nestjs/throttler": "^6.2.1",
"cache-manager": "^5.7.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
Expand Down
18 changes: 18 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { ConfigService } from './config/config/config.service';
import { LoggerModule } from 'nestjs-pino';
import { v4 as uuidv4 } from 'uuid';
import { CachingModule } from './caching/caching.module';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';

@Module({
imports: [
Expand Down Expand Up @@ -43,6 +45,22 @@ import { CachingModule } from './caching/caching.module';
},
}),
CachingModule,
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => [
{
ttl: config.get('throttle.ttl'),
limit: config.get('throttle.limit'),
},
],
}),
],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule implements NestModule {
Expand Down
18 changes: 17 additions & 1 deletion src/config/schema/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const schema: convict.Schema<AppConfig> = {
},
cache: {
ttl: {
doc: 'TTL in miliseconds for caching the results',
doc: 'TTL in milliseconds for caching the results',
format: 'Number',
default: 5000,
env: 'CACHE_TTL',
Expand All @@ -100,4 +100,20 @@ export const schema: convict.Schema<AppConfig> = {
arg: 'cache_max_entries',
},
},
throttle: {
ttl: {
doc: 'TTL in milliseconds for throttling the requests',
format: 'Number',
default: 60000,
env: 'THROTTLE_TTL',
arg: 'throttle_ttl',
},
limit: {
doc: 'Limit for throttling the requests',
format: 'Number',
default: 1000,
env: 'THROTTLE_LIMIT',
arg: 'throttle_limit',
},
},
};
1 change: 1 addition & 0 deletions src/jobs/controllers/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ApiBody, ApiOperation } from '@nestjs/swagger';
import { Job, PatchJob } from '../../models/job.dto';
import { DatabaseService } from '../../services/database/database.service';
import { CachingService } from '../../../caching/services/cache.service';
import { Throttle } from '@nestjs/throttler';

@Controller('jobs')
export class JobsController {
Expand Down
1 change: 1 addition & 0 deletions src/jobs/jobs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConfigModule } from '../config/config.module';
import { ConfigService } from '../config/config/config.service';
import { DatabaseService } from './services/database/database.service';
import { CachingModule } from '../caching/caching.module';
import { ThrottlerModule } from '@nestjs/throttler';

@Module({
controllers: [JobsController],
Expand Down

0 comments on commit 1be2935

Please sign in to comment.