RateCounter

Classin edge-rate-limiter@fastly/js-compute 4.0.0since 3.9.0View source

A rate counter that can be used with EdgeRateLimiter or standalone for counting and rate calculations.

Note: Can only be used when processing requests, not during build-time initialization.

Constructor

new RateCounter(name: string)

Open a RateCounter instance with the given name.

Parameters

name: string

The name of the rate counter.

Methods

increment

View source
increment(entry: string, delta: number): void

Increment the given entry by delta.

Parameters

entry: string

The entry to increment.

delta: number

The amount to increment the entry by.

Returns

void

Throws

  • TypeError if delta is not a non-negative finite number.

lookupCount

View source
lookupCount(entry: string, duration: 10 | 20 | 30 | 40 | 50 | 60): number

Look up the current count for an entry over a given duration.

Parameters

entry: string

The entry to look up.

duration: 10 | 20 | 30 | 40 | 50 | 60

The duration in seconds to look up alongside the entry.

Returns

number

The count for the given entry and duration.

Throws

  • TypeError if duration is not 10, 20, 30, 40, 50, or 60.

lookupRate

View source
lookupRate(entry: string, window: 1 | 10 | 60): number

Look up the current rate for an entry over a given window.

Parameters

entry: string

The entry to look up.

window: 1 | 10 | 60

The time window in seconds to look up alongside the entry.

Returns

number

The rate for the given entry and window.

Throws

  • TypeError if window is not 1, 10, or 60.