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

call-rate-limiter

v0.1.0

Published

Limit the execution rate of async function

Downloads

14

Readme

Call Rate Limiter

Limit the execution rate of any function by wrapping it with a function that returns Promise. Module uses sliding window rate limiter under the hood. For implementation details check out "How it works" section.

Install

npm install --save call-rate-limiter

Usage

Rate Limited Function

Te basic use case is when you ninteracting with REST api and want to repspect rate limits upfront to prevent be banned by the server.

rateLimit takes limitCount, limitInterval and fn as arguments and returns rate limited function which should be called instead of the function passed as fn.

This means if you call rateLimitedFunc 150 times and only 100 can be called in time, the next 50 calls will be postponed and executed later to respect given rate limits.


import {rateLimit} from 'call-rate-limiter'

const rateLimitedFunc = rateLimit(
  1200,
  60 * 1000,
  id => fetch('https://swapi.co/api/starships/${id}/').then(res => res.json())
)

/*
  fetch Death Star specs, rateLimitedFunc
  transparently passes args to wrapped function
*/
const deathStar = await rateLimitedFunc('9')

Rate Limited API

Suppose, you have a bunch of api calls specified somewhere that you want to call under the same rate limits. It can be easily done with objectRateLimit:

const basePath = 'https://swapi.co/api/'

function character(id) {
  return fetch(`${basePath}/people/${id}`).then(res => res.json())
}

function planet(id) {
  return fetch(`${basePath}/planet/${id}`).then(res => res.json())
}

module.exports = {
  character: character,
  planet: planet
}

objectRateLimit takes object as last argument and return new object with rate-limited functions assigned for same keys:

import {objectRateLimit} from 'call-rate-limiter'
import api from './api'

const rateLimitedApi = objectRateLimit(1200, 60 * 1000, api)

// trying to enumerate all planets in a galaxy far away
let i = 0
while(i < 100000) {
  console.log(await rateLimitedApi.planet(i))
  i++
}

Preventing bursting

Quite often, it is necessary to check not only compliance with the rate limits but also to avoid bursting. We want the function to be called no more than once each N milliseconds. To obtain this wrap your function with throttle than with rateLimit.

import {yourFunction} from 'your-module'

import {rateLimit} from 'call-rate-limiter'
import throttle from 'lodash.throttle'

const waitBetweenCalls = 100 // 100ms
const limitCount = 30 // max 30 calls per minute
const limitInterval = 60 * 1000 // one minute

const rateLimitedNoBurstingFunc = rateLimit(
  limitCount,
  limitInterval,
  throttle(yourFunction, waitBetweenCalls)
)

How it works

TODO: implement