Skip to content

Commit d137f14

Browse files
Merge pull request #3095 from phxgg/websocket-throttler-patch
docs: update websockets throttler guard example
2 parents d940f58 + d9b6c6f commit d137f14

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

content/security/rate-limiting.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,29 @@ 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, throttler: ThrottlerOptions): Promise<boolean> {
136-
const client = context.switchToWs().getClient();
137-
const ip = client._socket.remoteAddress;
138-
const key = this.generateKey(context, ip, throttler.name);
139-
const { totalHits } = await this.storageService.increment(key, ttl);
135+
async handleRequest(requestProps: ThrottlerRequest): Promise<boolean> {
136+
const { context, limit, ttl, throttler, blockDuration, getTracker, generateKey } = requestProps;
140137

141-
if (totalHits > limit) {
142-
throw new ThrottlerException();
138+
const client = context.switchToWs().getClient();
139+
const tracker = client._socket.remoteAddress;
140+
const key = generateKey(context, tracker, throttler.name);
141+
const { totalHits, timeToExpire, isBlocked, timeToBlockExpire } =
142+
await this.storageService.increment(key, ttl, limit, blockDuration, throttler.name);
143+
144+
const getThrottlerSuffix = (name: string) => (name === 'default' ? '' : `-${name}`);
145+
146+
// Throw an error when the user reached their limit.
147+
if (isBlocked) {
148+
await this.throwThrottlingException(context, {
149+
limit,
150+
ttl,
151+
key,
152+
tracker,
153+
totalHits,
154+
timeToExpire,
155+
isBlocked,
156+
timeToBlockExpire,
157+
});
143158
}
144159

145160
return true;
@@ -188,6 +203,10 @@ The following options are valid for the object passed to the array of the `Throt
188203
<td><code>limit</code></td>
189204
<td>the maximum number of requests within the TTL limit</td>
190205
</tr>
206+
<tr>
207+
<td><code>blockDuration</code></td>
208+
<td>the number of milliseconds that request will be blocked for that time</td>
209+
</tr>
191210
<tr>
192211
<td><code>ignoreUserAgents</code></td>
193212
<td>an array of regular expressions of user-agents to ignore when it comes to throttling requests</td>
@@ -217,6 +236,18 @@ If you need to set up storage instead, or want to use some of the above options
217236
<td><code>throttlers</code></td>
218237
<td>an array of throttler sets, defined using the table above</td>
219238
</tr>
239+
<tr>
240+
<td><code>errorMessage</code></td>
241+
<td>a <code>string</code> OR a function that takes in the <code>ExecutionContext</code> and the <code>ThrottlerLimitDetail</code> and returns a <code>string</code> which overrides the default throttler error message</td>
242+
</tr>
243+
<tr>
244+
<td><code>getTracker</code></td>
245+
<td>a function that takes in the <code>Request</code> and returns a <code>string</code> to override the default logic of the <code>getTracker</code> method</td>
246+
</tr>
247+
<tr>
248+
<td><code>generateKey</code></td>
249+
<td>a function that takes in the <code>ExecutionContext</code>, the tacker <code>string</code> and the throttler name as a <code>string</code> and returns a <code>string</code> to override the final key which will be used to store the rate limit value. This overrides the default logic of the <code>generateKey</code> method</td>
250+
</tr>
220251
</table>
221252

222253
#### Async Configuration

0 commit comments

Comments
 (0)