GithubHelp home page GithubHelp logo

wyt's Introduction

wyt - time-based rate limiter

Build Status Codebeat badge Codacy Badge Maintainability Coverage Status

NPM

wyt (pronounced wait) is a time-based rate limiter that uses promises.

Use Cases

  • Limiting calls to APIs that only allow a limited number of requests within a certain period. Just call it before your HTTP requests.

  • As an alternative to setInterval or setTimeout in polling loops.

    If you want to run run a function every second and you use setInterval, your function might run multiple times at the same time if your function occasionally takes more than 1 second to run. This might cause problems.

    Alternatively, if you use setTimeout(update, 1000) in a recursive update loop, your update interval will be 1000ms plus the time it takes for your update function to run. You'd have to measure the time and dynamically set the timeout.

    wyt takes care of this for you. It'll make sure that your update function runs every second unless the update function takes more than 1s to run, in which case it will run it again immediately.

Features

  • Promise-based API
  • Full test coverage
  • TypeScript support
  • Easy to use

Installation

npm install --save wyt

Usage

Rate-limiting API calls

Just put it in front of your HTTP requests.

import wyt from 'wyt';

// 5 API calls per second
const rateLimit = wyt(5, 1000);

async function getStuff() {
  await rateLimit();
  const response = await fetch('/stuff');
  return response.json();
}

await getStuff();
await getStuff();
await getStuff();
await getStuff();
await getStuff();
await getStuff(); // has to wait unless the previous 5 calls together took longer than 1000ms

Update loops

import wyt from 'wyt';

// Once per second
const rateLimit = wyt(1, 1000);

async function update() {
  await rateLimit();
  await updateMyStuff();
  update();
}

update();

If, for example, updateMyStuff() takes 900ms then rateLimit() will wait only 100ms.

API

const rateLimit = wyt(requestsPerInterval: number, interval: number)

  • requestsPerInterval (number): The number of requests that can be executed within a certain interval.
  • interval (number): The interval within which requestsPerInterval can be executed.

Returns a function (requests?: number) => Promise<number> that can be called before before requesting a rate-limited resource in order to not exceed the limit.

const timeWaited = await rateLimit(requests?: number)

  • requests (number, optional, default: 1): The number of requests that will be used at the same time. For example, if your code fetches two resources in parallel. You can not use more requests at the same time as requestsPerInterval.

Returns a promise Promise<number> that will resolve with the time waited as soon as another request can be made. If more than requestsPerInterval are requested at the same time the promise will reject.

License

Copyright (c) 2017 - 2020 Max Kueng and contributors

MIT License

wyt's People

Contributors

fredkschott avatar maxkueng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wyt's Issues

es5 compilation?

Hi there, wyt is fantastic. Unfortunately, we just added it to our app and started getting reports of our app breaking in IE11. It seems like your built code is still ES6. If you don't intend to support ES5, could you add a note to your readme indicating that? Otherwise, it'd be great if it compiled down to es5.

Thanks!

Clarification of use

I have similar use in my app as in the example below and I would expect "doing" appear on the logs every second:

var wyt = require("wyt")
const rateLimit = wyt(1, 1000);

async function doStuff(i) {
 await rateLimit();
 console.log('doing ' + i)
  
}

for (let i=0; i<5; i++){
 doStuff(i)
}

What I got instead is immediate execution 5 times.
I am trying to use wyt to rate limit my RPC node calls so it won't deny service, but it does not work as I've imagined.
How can I modify my test to get the expected result?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.