EdgeRateLimiter

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

Provides Edge Rate Limiting by combining a RateCounter and a PenaltyBox.

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

Constructor

new EdgeRateLimiter(rateCounter: RateCounter, penaltyBox: PenaltyBox)

Open an EdgeRateLimiter with the given rate counter and penalty box.

Parameters

rateCounter: RateCounter

The RateCounter instance to associate with this EdgeRateLimiter.

penaltyBox: PenaltyBox

The PenaltyBox instance to associate with this EdgeRateLimiter.

Throws

  • TypeError If rateCounter is not an instance of RateCounter. or if penaltyBox is not an instance of PenaltyBox.

Methods

checkRate

View source
checkRate(entry: string, delta: number, window: 1 | 10 | 60, limit: number, timeToLive: number): boolean

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. If the entry is over the RPS limit for the window, add it to the penalty box for the given timeToLive.

Parameters

entry: string

The name of the entry to increment and check.

delta: number

The amount to increment the entry by.

window: 1 | 10 | 60

The time period in seconds to check across.

limit: number

The requests-per-second limit.

timeToLive: number

How long in minutes (1–60) the entry should be added into the penalty box. Value is truncated to the nearest minute.

Returns

boolean

true if the entry has exceeded the average RPS for the window, otherwise false.

Throws

  • TypeError If delta is not a non-negative finite number, if window is not 1, 10, or 60, if limit is not a non-negative finite number, or if timeToLive is not a number between 1 and 60.