Skip to content

Commit

Permalink
feat(app): improve global throttler
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed May 9, 2024
1 parent bd8f8e9 commit 2307c14
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author Surmon <https://github.com/surmon-china>
*/

import type { Request } from 'express'
import { APP_INTERCEPTOR, APP_GUARD, APP_PIPE } from '@nestjs/core'
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common'
import { ThrottlerGuard, ThrottlerModule, minutes } from '@nestjs/throttler'
Expand Down Expand Up @@ -44,8 +45,14 @@ import { VoteModule } from '@app/modules/vote/vote.module'
ThrottlerModule.forRoot([
{
ttl: minutes(5), // 5 minutes = 300s
limit: 800, // 800 limit
ignoreUserAgents: [/googlebot/gi, /bingbot/gi, /baidubot/gi]
limit: 600, // 600 limit
ignoreUserAgents: [/googlebot/gi, /bingbot/gi, /baidubot/gi],
skipIf: (context) => {
// Skip throttle for the front-end Server
const request = context.switchToHttp().getRequest<Request>()
// Work only for front-end applications running on the same host machine
return request.hostname === 'localhost' || ['127.0.0.1', '::1'].includes(request.ip)
}
}
]),
HelperModule,
Expand Down

0 comments on commit 2307c14

Please sign in to comment.