DEV Community

Michael Di Prisco
Michael Di Prisco

Posted on

My Journey in Open Source - rate-limiter-fn

Link to the repo

What is this?

A higher-order function to provide a Rate Limiting mechanism to the given function.

How do I install it?

npm install rate-limiter-fn
Enter fullscreen mode Exit fullscreen mode

How can I use it?

const rateLimiter = require('rate-limiter-fn');

const rateLimitedFn = rateLimiter(fn, {
  limit: 10,
  interval: 1000,
});

rateLimitedFn();
Enter fullscreen mode Exit fullscreen mode

API

The module exports a single function (rateLimit) that takes two arguments:

  • fn - The function to be rate limited.
  • options - An object with the following properties:
    • limit - The number of times the function can be called within the given interval.
    • interval - The interval in milliseconds.
    • onLimitReached - A function that will be called when the limit is reached. It will be called with the following arguments:
    • limit - The limit that was reached.
    • interval - The interval that was reached.
    • fn - The function that was rate limited.
    • args - The arguments that were passed to the function.
    • identifierFn - A function that will be called to get the identifier for the rate limit. It will be called with the following arguments:
    • fn - The function that was rate limited.
    • args - The arguments that were passed to the function.

Tests

You can run the tests by using the following command:

npm test
Enter fullscreen mode Exit fullscreen mode

Top comments (0)