5req/s universal rate limit

This commit is contained in:
2026-03-19 21:53:53 +08:00
parent 96135493a6
commit 16a32e82d6
3 changed files with 13 additions and 17 deletions

View File

@@ -1,7 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_GUARD } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter'; import { EventEmitterModule } from '@nestjs/event-emitter';
import { ThrottlerModule } from '@nestjs/throttler'; import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { UsersModule } from './users/users.module'; import { UsersModule } from './users/users.module';
@@ -76,8 +77,8 @@ function validateOptionalProvider(
inject: [ConfigService], inject: [ConfigService],
useFactory: (config: ConfigService) => [ useFactory: (config: ConfigService) => [
{ {
ttl: config.get('THROTTLE_TTL', 60000), ttl: config.get('THROTTLE_TTL', 1000),
limit: config.get('THROTTLE_LIMIT', 10), limit: config.get('THROTTLE_LIMIT', 5),
}, },
], ],
}), }),
@@ -91,6 +92,12 @@ function validateOptionalProvider(
DollsModule, DollsModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [
AppService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
}) })
export class AppModule {} export class AppModule {}

View File

@@ -1,5 +1,4 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ThrottlerModule } from '@nestjs/throttler';
import { FriendsController } from './friends.controller'; import { FriendsController } from './friends.controller';
import { FriendsService } from './friends.service'; import { FriendsService } from './friends.service';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
@@ -18,6 +17,7 @@ describe('FriendsController', () => {
userId: 'user-1', userId: 'user-1',
email: 'user1@example.com', email: 'user1@example.com',
roles: [], roles: [],
tokenType: 'access' as const,
}; };
const mockUser1 = { const mockUser1 = {
@@ -83,14 +83,6 @@ describe('FriendsController', () => {
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
imports: [
ThrottlerModule.forRoot([
{
ttl: 60000,
limit: 10,
},
]),
],
controllers: [FriendsController], controllers: [FriendsController],
providers: [ providers: [
{ provide: FriendsService, useValue: mockFriendsService }, { provide: FriendsService, useValue: mockFriendsService },

View File

@@ -7,8 +7,8 @@ import {
Body, Body,
Query, Query,
HttpCode, HttpCode,
UseGuards,
Logger, Logger,
UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { import {
ApiTags, ApiTags,
@@ -19,7 +19,6 @@ import {
ApiUnauthorizedResponse, ApiUnauthorizedResponse,
ApiQuery, ApiQuery,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { ThrottlerGuard, Throttle } from '@nestjs/throttler';
import { User, FriendRequest, Prisma } from '@prisma/client'; import { User, FriendRequest, Prisma } from '@prisma/client';
import { FriendsService } from './friends.service'; import { FriendsService } from './friends.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
@@ -62,8 +61,6 @@ export class FriendsController {
) {} ) {}
@Get('search') @Get('search')
@UseGuards(ThrottlerGuard)
@Throttle({ default: { limit: 10, ttl: 60000 } })
@ApiOperation({ @ApiOperation({
summary: 'Search users by username', summary: 'Search users by username',
description: 'Search for users by username to send friend requests', description: 'Search for users by username to send friend requests',