All Projects → natesilva → p-ratelimit

natesilva / p-ratelimit

Licence: MIT license
Promise-based utility to make sure you don’t call rate-limited APIs too quickly.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to p-ratelimit

YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+293.88%)
Mutual labels:  promise, concurrency
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (-36.73%)
Mutual labels:  promise, concurrency
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+112.24%)
Mutual labels:  promise, concurrency
ratelimiter
A concurrent rate limiter library for Golang based on Sliding-Window rate limiter algorithm.
Stars: ✭ 218 (+344.9%)
Mutual labels:  concurrency, rate-limiting
Throat
Throttle a collection of promise returning functions
Stars: ✭ 419 (+755.1%)
Mutual labels:  promise, concurrency
conquerant
lightweight async/await for Clojure
Stars: ✭ 31 (-36.73%)
Mutual labels:  promise, concurrency
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-59.18%)
Mutual labels:  promise, concurrency
wise-river
Object streaming the way it should be.
Stars: ✭ 33 (-32.65%)
Mutual labels:  promise, concurrency
Post Me
📩 Use web Workers and other Windows through a simple Promise API
Stars: ✭ 398 (+712.24%)
Mutual labels:  promise, concurrency
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+7612.24%)
Mutual labels:  promise, concurrency
P Map
Map over promises concurrently
Stars: ✭ 639 (+1204.08%)
Mutual labels:  promise, concurrency
Start
🔴 Functional task runner for Node.js
Stars: ✭ 478 (+875.51%)
Mutual labels:  promise, concurrency
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+193.88%)
Mutual labels:  promise, concurrency
promise-waterfall
promise-waterfall extends promise utilities with sequential flow control function like async.waterfall
Stars: ✭ 28 (-42.86%)
Mutual labels:  promise
vue-geolocation
ask to your users their coordinates, and wrap them into a Promise
Stars: ✭ 88 (+79.59%)
Mutual labels:  promise
zlimiter
A toolkit for rate limite,support memory and redis
Stars: ✭ 17 (-65.31%)
Mutual labels:  rate-limiting
simpledbm
SimpleDBM is an Open Source Multi-Threaded Embeddable Transactional Database Engine in Java.
Stars: ✭ 51 (+4.08%)
Mutual labels:  concurrency
concurrent-ll
concurrent linked list implementation
Stars: ✭ 66 (+34.69%)
Mutual labels:  concurrency
promise-yes
Slide about how to use Promise in JavaScript.
Stars: ✭ 13 (-73.47%)
Mutual labels:  promise
parley
Flow control harness for implementors. Builds a Deferred object that supports async/await, promise chaining, and conventional Node callbacks.
Stars: ✭ 23 (-53.06%)
Mutual labels:  promise

p-ratelimit npm license node

Makes sure you don’t call rate-limited APIs too quickly.

This is an easy-to-use utility for calling rate-limited APIs. It will prevent you from exceeding rate limits by queueing requests that would go over your rate limit quota.

Rate-limits can be applied across multiple servers if you use Redis.

It works with any API function that returns a Promise.

Install

$ npm i p-ratelimit

What’s different

  • True rate limiting
    • Utilities like p-limit control how many functions are running concurrently. That won’t prevent you from exceeding limits on APIs that use token-bucket throttling.
    • p-ratelimit supports both concurrency and rate limits.
  • Works across API families
    • Utilities like Lodash throttle create separate quotas for each API function.
    • p-ratelimit can enforce a single shared quota for all functions in an API family.
  • Minimal implementation
    • Utilities like limiter provide low-level tooling that requires you to manage tokens and provide your own queue.
    • p-ratelimit requires minimal modification to your existing code.
  • Distributed rate limits
    • If you use Redis, p-ratelimit supports efficient rate limiting across multiple hosts. The quota is divided among your pool of servers. As servers are added or removed, the shared quota is recaclulated.
  • Made for Promises and TypeScript friendly
    • A rate-limited function returns the same Promise type as the original function.

Example

const { pRateLimit } = require('p-ratelimit');
// import { pRateLimit } from 'p-ratelimit';       // TypeScript

// create a rate limiter that allows up to 30 API calls per second,
// with max concurrency of 10
const limit = pRateLimit({
    interval: 1000,             // 1000 ms == 1 second
    rate: 30,                   // 30 API calls per interval
    concurrency: 10,            // no more than 10 running at once
    maxDelay: 2000              // an API call delayed > 2 sec is rejected
});

async function main() {
  // original WITHOUT rate limiter:
  result = await someFunction(42);
  // with rate limiter:
  result = await limit(() => someFunction(42));
}

main();

Configuration

The Quota configuration object passed to pRateLimit offers the following configuration settings:

If you care about rate limiting

Set both of these:

  • interval: the interval over which to apply the rate limit, in milliseconds
  • rate: how many API calls to allow over the interval period

If you care about limiting concurrency

  • concurrency: how many concurrent API calls to allow

If you care about both rate limiting and concurrency

If you want both rate limiting and concurrency, use all three of the above settings (interval, rate, concurrency).

Other options

  • maxDelay: the maximum amount of time to wait (in milliseconds) before rejecting an API request with RateLimitTimeoutError (default: 0, no timeout)
  • fastStart (Redis only): if true, immediately begin processing requests using the full quota, instead of waiting several seconds to discover other servers (default: false)

If you make an API request that would exceed rate limits, it’s queued and delayed until it can run within the rate limits. Setting maxDelay will cause the API request to fail if it’s delayed too long.

See the Using Redis section for a discussion of the fastStart option.

Distributed rate limits

See Using Redis for a detailed discussion.

You can use Redis to coordinate a rate limit among a pool of servers.

const { pRateLimit, RedisQuotaManager } = require('p-ratelimit');

// These must be the same across all servers that share this quota:
const channelName = 'my-api-family';
const quota = { rate: 100, interval: 1000, concurrency: 50 };

// Create a RedisQuotaManager
const qm = new RedisQuotaManager(
    quota,
    channelName,
    redisClient
);

// Create a rate limiter that uses the RedisQuotaManager
const limit = pRateLimit(qm);

// now use limit(…) as usual

Each server that registers with a given channelName will be allotted 1/(number of servers) of the available quota. For example, if the pool consists of four servers, each will receive 1/4 the available quota.

When a new server joins the pool, the quota is dynamically adjusted. If a server goes away, its quota is reallocated among the remaining servers within a few minutes.

License

MIT © Nate Silva

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].