Skip to content

Commit

Permalink
add throttling guide
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Oct 5, 2024
1 parent 1c5b422 commit 53c31af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
5 changes: 4 additions & 1 deletion malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
},
{
"title": "Rate limiting",
"pages": [["Token bucket", "/rate-limit/token-bucket"]]
"pages": [
["Token bucket", "/rate-limit/token-bucket"],
["Throttling", "/rate-limit/throttling"]
]
},
{
"title": "Community",
Expand Down
60 changes: 60 additions & 0 deletions pages/rate-limit/throttling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: "Throttling"
---

# Throttling

After each failed attempt, the user has to wait longer before their next attempt.

## Memory storage

```ts
export class Throttler<_Key> {
public timeoutSeconds: number[];

private storage = new Map<_Key, ThrottlingCounter>();

constructor(timeoutSeconds: number[]) {
this.timeoutSeconds = timeoutSeconds;
}

public consume(key: _Key): boolean {
let counter = this.storage.get(key) ?? null;
const now = Date.now();
if (counter === null) {
counter = {
timeout: 0,
updatedAt: now
};
this.storage.set(key, counter);
return true;
}
const allowed = now - counter.updatedAt >= this.timeoutSeconds[counter.timeout] * 1000;
if (!allowed) {
return false;
}
counter.updatedAt = now;
counter.timeout = Math.min(counter.timeout + 1, this.timeoutSeconds.length - 1);
this.storage.set(key, counter);
return true;
}

public reset(key: _Key): void {
this.storage.delete(key);
}
}
```

Here, on each failed sign in attempt, the lockout gets extended with a max of 5 minutes.

```ts
const throttler = new Throttler([0, 1, 2, 4, 8, 16, 30, 60, 180, 300]);

if (!throttler.consume(userId)) {
throw new Error("Too many requests");
}
if (!verifyPassword(password)) {
throw new Error("Invalid password");
}
throttler.reset(user.id);
```

0 comments on commit 53c31af

Please sign in to comment.