Skip to content

Commit 0f6036f

Browse files
feat: Add a RateCounter JavaScript Class which can be used standalone for counting and rate calculations (#730)
Co-authored-by: Trevor Elliott <[email protected]>
1 parent 94f4038 commit 0f6036f

File tree

24 files changed

+1584
-3
lines changed

24 files changed

+1584
-3
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defaults:
1010
run:
1111
shell: bash
1212
env:
13-
viceroy_version: 0.8.1
13+
viceroy_version: 0.9.4
1414
wasm-tools_version: 1.0.28
1515
fastly-cli_version: 10.4.0
1616

@@ -133,7 +133,7 @@ jobs:
133133
matrix:
134134
include:
135135
- crate: viceroy
136-
version: 0.8.1 # Note: workflow-level env vars can't be used in matrix definitions
136+
version: 0.9.4 # Note: workflow-level env vars can't be used in matrix definitions
137137
options: ""
138138
- crate: wasm-tools
139139
version: 1.0.28 # Note: workflow-level env vars can't be used in matrix definitions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# `RateCounter()`
8+
9+
The **`RateCounter` constructor** can be used with a [Edge Rate Limiter](../EdgeRateLimiter/EdgeRateLimiter.mdx) or standalone for counting and rate calculations.
10+
11+
>**Note**: Can only be used when processing requests, not during build-time initialization.
12+
13+
## Syntax
14+
15+
```js
16+
new RateCounter(name)
17+
```
18+
19+
> **Note:** `RateCounter()` can only be constructed with `new`. Attempting to call it without `new` throws a [`TypeError`](../../globals/TypeError/TypeError.mdx).
20+
21+
### Parameters
22+
23+
- `name` _: string_
24+
- Open a RateCounter with the given name
25+
26+
27+
### Return value
28+
29+
A new `RateCounter` object instance.
30+
31+
### Exceptions
32+
33+
- `TypeError`
34+
- Thrown if the provided `name` value can not be coerced into a string
35+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# RateCounter.prototype.increment
8+
9+
Increment the given `entry` in the RateCounter instance with the given `delta` value.
10+
11+
## Syntax
12+
```js
13+
increment(entry, delta)
14+
```
15+
16+
### Parameters
17+
18+
- `entry` _: string_
19+
- The name of the entry to look up
20+
- `delta` _: number_
21+
- The amount to increment the entry by
22+
23+
24+
### Return value
25+
26+
Returns `undefined`.
27+
28+
### Exceptions
29+
30+
- `TypeError`
31+
- Thrown if the provided `entry` value can not be coerced into a string
32+
- Thrown if the provided `delta` value is not a positive, finite number.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# RateCounter.prototype.lookupCount
8+
9+
Look up the current rate for the given `entry` and the given `duration`.
10+
11+
## Syntax
12+
```js
13+
lookupCount(entry, duration)
14+
```
15+
16+
### Parameters
17+
18+
- `entry` _: string_
19+
- The name of the entry to look up
20+
- `duration` _: number_
21+
- The duration to lookup alongside the entry, has to be either, 10, 20, 30, 40, 50, or 60 seconds.
22+
23+
24+
### Return value
25+
26+
Returns a number which is the count for the given `entry` and `duration` in this `RateCounter` instance.
27+
28+
### Exceptions
29+
30+
- `TypeError`
31+
- Thrown if the provided `entry` value can not be coerced into a string
32+
- Thrown if the provided `duration` value is not either, 10, 20, 30, 40, 50 or 60.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# RateCounter.prototype.lookupRate
8+
9+
Look up the current rate for the given `entry` and the given `window`.
10+
11+
## Syntax
12+
```js
13+
lookupRate(entry, window)
14+
```
15+
16+
### Parameters
17+
18+
- `entry` _: string_
19+
- The name of the entry to look up
20+
- `window` _: number_
21+
- The window to look up alongside the entry, has to be either 1 second, 10 seconds, or 60 seconds
22+
23+
24+
### Return value
25+
26+
Returns a number which is the rate for the given `entry` and `window` in this `RateCounter` instance.
27+
28+
### Exceptions
29+
30+
- `TypeError`
31+
- Thrown if the provided `entry` value can not be coerced into a string
32+
- Thrown if the provided `window` value is not either, 1, 10, or 60.

0 commit comments

Comments
 (0)