Skip to content

Commit

Permalink
feat: Add a RateCounter JavaScript Class which can be used standalone…
Browse files Browse the repository at this point in the history
… for counting and rate calculations (#730)

Co-authored-by: Trevor Elliott <telliott@fastly.com>
  • Loading branch information
JakeChampion and elliottt committed Mar 1, 2024
1 parent 94f4038 commit 0f6036f
Show file tree
Hide file tree
Showing 24 changed files with 1,584 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,7 +10,7 @@ defaults:
run:
shell: bash
env:
viceroy_version: 0.8.1
viceroy_version: 0.9.4
wasm-tools_version: 1.0.28
fastly-cli_version: 10.4.0

Expand Down Expand Up @@ -133,7 +133,7 @@ jobs:
matrix:
include:
- crate: viceroy
version: 0.8.1 # Note: workflow-level env vars can't be used in matrix definitions
version: 0.9.4 # Note: workflow-level env vars can't be used in matrix definitions
options: ""
- crate: wasm-tools
version: 1.0.28 # Note: workflow-level env vars can't be used in matrix definitions
Expand Down
@@ -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

@@ -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.
@@ -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.
@@ -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.

0 comments on commit 0f6036f

Please sign in to comment.