npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@qima.mkt/rate-limiter

v1.0.0

Published

A client-side rate limiter.

Downloads

4

Readme

Rate Limiter

A client-side rate limiter that implements the fixed window algorithm. (Other algorithms soon to be supported)

Table of Contents

Problem

Naturally, we aim to speed up our processes. When some tasks have the ability to execute concurrently or in-parallel, we wish to take advantage of it. However, in some cases, we are limited to how many of these tasks we can execute concurrently. E.g.: The API docs for an API that we are integrating states "up to 10 requests per second" are supported. How will we ensure this while benefiting from concurrent task execution?

Solution

A client-side rate limiter solves this problem. We define its limits and feed it our tasks. It can then handle concurrent task execution while abiding by the limits we set in place. Other open source rate limiters, such as limiter either do not support both max concurrency control and time-based rate limiting, or they do not support all rate limiting algorithms. The goal of this library is to eventually support all rate limiting algorithms, along with max concurrency control. (E.g. 10 tasks per second, but only 2 tasks at any given time). Some APIs place limits on both tasks per interval and concurrent tasks, which is why this is necessary. One example is the SmartRecruiters API:

For most endpoints, SmartAPIs allow up to 10 requests per second... [and] up to 8 concurrent requests.

How to Use

import RateLimiter from 'rate-limiter';

const rateLimiter = new RateLimiter({
  interval: 'second',
  maxPerInterval: 10,
  maxConcurrency: 10
});

// fake async task that takes 1 second
const makeAsyncTask = () => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000);
  });
};

// add 10 tasks to the rate limiter
const tasks = [];
for (let i = 0; i < 10; i++) {
  tasks.push(rateLimiter.add(makeAsyncTask));
}

// check results of task execution
const results = await Promise.allSettled(tasks);
console.log(results);
// [
//   { status: 'fulfilled', value: 'success' },
//   { status: 'fulfilled', value: 'success' },
//   { status: 'fulfilled', value: 'success' },
//   { status: 'rejected', reason: Error: some error message }
// ]