Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bottleneck

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bottleneck

Distributed task scheduler and rate limiter

  • 2.19.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3M
increased by8.36%
Maintainers
1
Weekly downloads
 
Created

What is bottleneck?

The bottleneck npm package is a rate limiter that allows you to control the rate at which tasks are executed. It is useful for managing resource usage and ensuring that your application does not exceed the rate limits imposed by APIs or other systems. It can be used to throttle function calls, manage task execution concurrency, and queue tasks.

What are bottleneck's main functionalities?

Task Scheduling

This feature allows you to schedule tasks to be executed with a minimum time interval between them and a maximum number of concurrent tasks. The code sample demonstrates how to create a limiter that only allows one task to run at a time with a minimum of 1 second between task starts.

const Bottleneck = require('bottleneck');
const limiter = new Bottleneck({ maxConcurrent: 1, minTime: 1000 });

async function myFunction(id) {
  console.log(`Working on task ${id}`);
}

for (let i = 0; i < 5; i++) {
  limiter.schedule(() => myFunction(i));
}

Priority Queueing

This feature allows you to assign priorities to tasks. Tasks with higher priority (lower priority number) will be executed before those with lower priority. The code sample shows how to schedule tasks with different priorities.

const Bottleneck = require('bottleneck');
const limiter = new Bottleneck({ maxConcurrent: 2 });

async function myFunction(priority, id) {
  console.log(`Working on task ${id} with priority ${priority}`);
}

limiter.schedule({ priority: 1 }, myFunction, 1, 'A');
limiter.schedule({ priority: 5 }, myFunction, 5, 'B');
limiter.schedule({ priority: 3 }, myFunction, 3, 'C');

Rate Limiting

This feature allows you to limit the rate at which functions are executed. It can be used to avoid hitting rate limits of external APIs. The code sample demonstrates how to set up a limiter with a reservoir that refreshes over time, and how to handle the 'depleted' event when the rate limit is exceeded.

const Bottleneck = require('bottleneck');
const limiter = new Bottleneck({ reservoir: 100, // initial value
  reservoirRefreshAmount: 100,
  reservoirRefreshInterval: 60 * 1000 // must be divisible by 250
});

limiter.on('depleted', () => console.log('Rate limit exceeded'));

// Function to be rate-limited
async function myFunction() {
  console.log('Doing something.');
}

setInterval(() => {
  limiter.submit(myFunction);
}, 10);

Other packages similar to bottleneck

Keywords

FAQs

Package last updated on 03 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc