Skip to content

RateLimiterUnion

Roman edited this page Oct 17, 2022 · 3 revisions

RateLimiterUnion

Combine two or more rate limiters to act as single. The minimum number of limiters combined is one.

Note: consume method is implemented only.

Any rate limiters from this rate-limiter-flexible can be united

Useful for authorization, which must be protected from password brute force

For example, not more than once per second and only 5 points per minute

keyPrefix is necessary as resolved and rejected results depend on it

const limiter1 = new RateLimiterMemory({
  keyPrefix: 'limit1',
  points: 1,
  duration: 1,
});
const limiter2 = new RateLimiterMemory({
  keyPrefix: 'limit2',
  points: 5,
  duration: 60,
});
const rateLimiterUnion = new RateLimiterUnion(limiter1, limiter2);

rateLimiterUnion.consume(remoteAddress)
  .then((res) => {
    // Returns object with 2 RateLimiterRes objects
    res['limit1'].remainingPoints;
    res['limit2'].remainingPoints;
  })
  .catch((rej) => {
    /* Returns object with RateLimiterRes objects only for rejected limiters
    * For example:
    * { limit1: RateLimiterRes { ... } }
    * 
    * It may be Error if you use any limiter without insurance except Memory 
    * { limit2: Error }
    */
  });
Clone this wiki locally