Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a RateCounter JavaScript Class which can be used standalone…
… for counting and rate calculations (#730) Co-authored-by: Trevor Elliott <telliott@fastly.com>
- Loading branch information
1 parent
94f4038
commit 0f6036f
Showing
24 changed files
with
1,584 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
documentation/docs/fastly:edge-rate-limiter/RateCounter/RateCounter.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
hide_title: false | ||
hide_table_of_contents: false | ||
pagination_next: null | ||
pagination_prev: null | ||
--- | ||
# `RateCounter()` | ||
|
||
The **`RateCounter` constructor** can be used with a [Edge Rate Limiter](../EdgeRateLimiter/EdgeRateLimiter.mdx) or standalone for counting and rate calculations. | ||
|
||
>**Note**: Can only be used when processing requests, not during build-time initialization. | ||
## Syntax | ||
|
||
```js | ||
new RateCounter(name) | ||
``` | ||
|
||
> **Note:** `RateCounter()` can only be constructed with `new`. Attempting to call it without `new` throws a [`TypeError`](../../globals/TypeError/TypeError.mdx). | ||
### Parameters | ||
|
||
- `name` _: string_ | ||
- Open a RateCounter with the given name | ||
|
||
|
||
### Return value | ||
|
||
A new `RateCounter` object instance. | ||
|
||
### Exceptions | ||
|
||
- `TypeError` | ||
- Thrown if the provided `name` value can not be coerced into a string | ||
|
32 changes: 32 additions & 0 deletions
32
documentation/docs/fastly:edge-rate-limiter/RateCounter/prototype/increment.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
hide_title: false | ||
hide_table_of_contents: false | ||
pagination_next: null | ||
pagination_prev: null | ||
--- | ||
# RateCounter.prototype.increment | ||
|
||
Increment the given `entry` in the RateCounter instance with the given `delta` value. | ||
|
||
## Syntax | ||
```js | ||
increment(entry, delta) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `entry` _: string_ | ||
- The name of the entry to look up | ||
- `delta` _: number_ | ||
- The amount to increment the entry by | ||
|
||
|
||
### Return value | ||
|
||
Returns `undefined`. | ||
|
||
### Exceptions | ||
|
||
- `TypeError` | ||
- Thrown if the provided `entry` value can not be coerced into a string | ||
- Thrown if the provided `delta` value is not a positive, finite number. |
32 changes: 32 additions & 0 deletions
32
documentation/docs/fastly:edge-rate-limiter/RateCounter/prototype/lookupCount.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
hide_title: false | ||
hide_table_of_contents: false | ||
pagination_next: null | ||
pagination_prev: null | ||
--- | ||
# RateCounter.prototype.lookupCount | ||
|
||
Look up the current rate for the given `entry` and the given `duration`. | ||
|
||
## Syntax | ||
```js | ||
lookupCount(entry, duration) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `entry` _: string_ | ||
- The name of the entry to look up | ||
- `duration` _: number_ | ||
- The duration to lookup alongside the entry, has to be either, 10, 20, 30, 40, 50, or 60 seconds. | ||
|
||
|
||
### Return value | ||
|
||
Returns a number which is the count for the given `entry` and `duration` in this `RateCounter` instance. | ||
|
||
### Exceptions | ||
|
||
- `TypeError` | ||
- Thrown if the provided `entry` value can not be coerced into a string | ||
- Thrown if the provided `duration` value is not either, 10, 20, 30, 40, 50 or 60. |
32 changes: 32 additions & 0 deletions
32
documentation/docs/fastly:edge-rate-limiter/RateCounter/prototype/lookupRate.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
hide_title: false | ||
hide_table_of_contents: false | ||
pagination_next: null | ||
pagination_prev: null | ||
--- | ||
# RateCounter.prototype.lookupRate | ||
|
||
Look up the current rate for the given `entry` and the given `window`. | ||
|
||
## Syntax | ||
```js | ||
lookupRate(entry, window) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `entry` _: string_ | ||
- The name of the entry to look up | ||
- `window` _: number_ | ||
- The window to look up alongside the entry, has to be either 1 second, 10 seconds, or 60 seconds | ||
|
||
|
||
### Return value | ||
|
||
Returns a number which is the rate for the given `entry` and `window` in this `RateCounter` instance. | ||
|
||
### Exceptions | ||
|
||
- `TypeError` | ||
- Thrown if the provided `entry` value can not be coerced into a string | ||
- Thrown if the provided `window` value is not either, 1, 10, or 60. |
Oops, something went wrong.