Skip to content

Commit 1c4fbc1

Browse files
committed
docs(rate-limiting): update WsThrottlerGuard example for v5
1 parent 7f393a9 commit 1c4fbc1

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

content/security/rate-limiting.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class AppModule {}
6464

6565
#### Customization
6666

67-
There may be a time where you want to bind the guard to a controller or globally, but want to disable rate limiting for one or more of your endpoints. For that, you can use the `@SkipThrottle()` decorator, to negate the throttler for an entire class or a single route. The `@SkipThrottle()` decorator can also take in an object of string keys with boolean values for if there is a case where you want to exclude _most_ of a controller, but not every route, and configure it per throttler set if you have more than one. If you do not pass an object, the default is to use `{{ '{' }} default: true {{ '}' }}`
67+
There may be a time where you want to bind the guard to a controller or globally, but want to disable rate limiting for one or more of your endpoints. For that, you can use the `@SkipThrottle()` decorator, to negate the throttler for an entire class or a single route. The `@SkipThrottle()` decorator can also take in an object of string keys with boolean values for if there is a case where you want to exclude _most_ of a controller, but not every route, and configure it per throttler set if you have more than one. If you do not pass an object, the default is to use `{{ '{' }} default: true {{ '}' }}`
6868

6969
```typescript
7070
@SkipThrottle()
@@ -132,10 +132,10 @@ This module can work with websockets, but it requires some class extension. You
132132
```typescript
133133
@Injectable()
134134
export class WsThrottlerGuard extends ThrottlerGuard {
135-
async handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean> {
135+
async handleRequest(context: ExecutionContext, limit: number, ttl: number, throttler: ThrottlerOptions): Promise<boolean> {
136136
const client = context.switchToWs().getClient();
137137
const ip = client._socket.remoteAddress;
138-
const key = this.generateKey(context, ip);
138+
const key = this.generateKey(context, ip, throttler.name);
139139
const { totalHits } = await this.storageService.increment(key, ttl);
140140

141141
if (totalHits > limit) {
@@ -231,10 +231,12 @@ One approach would be to use a factory function:
231231
ThrottlerModule.forRootAsync({
232232
imports: [ConfigModule],
233233
inject: [ConfigService],
234-
useFactory: (config: ConfigService) => ([{
235-
ttl: config.get('THROTTLE_TTL'),
236-
limit: config.get('THROTTLE_LIMIT'),
237-
}]),
234+
useFactory: (config: ConfigService) => [
235+
{
236+
ttl: config.get('THROTTLE_TTL'),
237+
limit: config.get('THROTTLE_LIMIT'),
238+
},
239+
],
238240
}),
239241
],
240242
})

0 commit comments

Comments
 (0)