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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more