EdgeRateLimiter
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
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
TypeErrorIfrateCounteris not an instance of RateCounter. or ifpenaltyBoxis not an instance of PenaltyBox.
Methods
checkRate
View sourcecheckRate(entry: string, delta: number, window: 1 | 10 | 60, limit: number, timeToLive: number): booleanIncrement 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
true if the entry has exceeded the average RPS for the window,
otherwise false.
Throws
TypeErrorIfdeltais not a non-negative finite number, ifwindowis not 1, 10, or 60, iflimitis not a non-negative finite number, or iftimeToLiveis not a number between 1 and 60.