Skip to content

Commit 4e81fc7

Browse files
authored
feat: Add a EdgeRateLimiter JavaScript Class which enables edge-rate-limiting by utilising a RateCounter and a PenaltyBox instance (#732)
1 parent bfe1e15 commit 4e81fc7

File tree

12 files changed

+914
-1
lines changed

12 files changed

+914
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# `EdgeRateLimiter()`
8+
9+
The **`EdgeRateLimiter` constructor** lets you open an epen a [ERL](https://docs.fastly.com/products/edge-rate-limiting) with the given ratecounter and penaltybox.
10+
11+
12+
>**Note**: Can only be used when processing requests, not during build-time initialization.
13+
14+
## Syntax
15+
16+
```js
17+
new EdgeRateLimiter(rateCounter, penaltyBox)
18+
```
19+
20+
> **Note:** `EdgeRateLimiter()` can only be constructed with `new`. Attempting to call it without `new` throws a [`TypeError`](../../globals/TypeError/TypeError.mdx).
21+
22+
### Parameters
23+
24+
- `rateCounter` _: RateCounter_
25+
- The RateCounter instance to associate with this EdgeRateLimiter instance
26+
- `penaltyBox` _: PenaltyBox_
27+
- The PenaltyBox instance which should be associated with this EdgeRateLimiter instance
28+
29+
### Return value
30+
31+
A new `EdgeRateLimiter` object.
32+
33+
### Exceptions
34+
35+
- `TypeError`
36+
- Thrown if the provided rateCounter value is not an instance of RateCounter
37+
- Thrown if the provided penaltyBox value is not an instance of PenaltyBox
38+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# EdgeRateLimiter.prototype.checkRate
8+
9+
Increment an entry in the rate counter and check if the entry has exceeded some average number of requests per second (RPS) over the given window.
10+
If the entry is over the RPS limit for the window, add to the penaltybox for the given `timeToLive`.
11+
12+
Valid `timeToLive` is 1 minute to 60 minutes and `timeToLive` value is truncated to the nearest minute.
13+
14+
## Syntax
15+
```js
16+
checkRate(entry, delta, window, limit, timeToLive)
17+
```
18+
19+
### Parameters
20+
21+
- `entry` _: string_
22+
- The name of the entry to increment and check
23+
- `delta` _: number_
24+
- The amount to increment the `entry` by
25+
- `window` _: number_
26+
- The time period to check across, has to be either 1 second, 10 seconds, or 60 seconds
27+
- `limit` _: number_
28+
- The requests-per-second limit
29+
- `timeToLive` _: number_
30+
- In minutes, how long the entry should be added into the penalty-box
31+
- Valid `timeToLive` is 1 minute to 60 minutes and `timeToLive` value is truncated to the nearest minute.
32+
33+
34+
### Return value
35+
36+
Returns `true` if the entry has exceeded the average RPS for the window, otherwise returns `false`.
37+
38+
### Exceptions
39+
40+
- `TypeError`
41+
- Thrown if the provided `name` value can not be coerced into a string
42+
- Thrown if the provided `delta` value is not a positive finite number.
43+
- Thrown if the provided `window` value is not either, 1, 10, or 60.
44+
- Thrown if the provided `limit` value is not a positive finite number.
45+
- Thrown if the provided `timeToLive` value is not either, a number between 1 and 60 inclusively.
46+

0 commit comments

Comments
 (0)