New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

exponential-rate-limit

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exponential-rate-limit

A small library which handles decaying exponential backoff. This is useful if you want to start throttling something whilst it is going wrong, but recover once things start working again.

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm version

Exponential Rate Limit

A small library which handles decaying exponential backoff. This is useful if you want to start throttling something whilst it is going wrong, but recover once things start working again.

Demo

The demo source is in "demo.ts".

To run the demo locally run npm run start.

It is also hosted here (thanks to Netify).

Usage

import { JobQueue, defaultDelayFunction } from './exponential-rate-limit';

const queue = new JobQueue({
  /**
   * The maximum time (seconds) between jobs executing.
   */
  maxInterval: 5
  /**
   * The delay function.
   */
  delayFunction: defaultDelayFunction
});

const { remove } = queue.enqueue(({ enqueueTime } => {
  console.log('Executing', enqueueTime);
});

Example

The following example implements throttledFetch, which will start delaying future executions exponentially (up to the default 5 seconds) every time a request fails or does not respond with status 200.

Every time there is a 200 response, the delays will also start getting shorter again. As time passes without any jobs being executed, the delay the next job would incur also decreases.

import { JobQueue } from './exponential-rate-limit';

const jobQueue = new JobQueue(); // default options

function throttledFetch(...fetchArgs): Promise<Response> {
  return new Promise((resolve) => {
    jobQueue.enqueue(() => {
      const fetchPromise = fetch(...fetchArgs);
      resolve(fetchPromise);
      return fetchPromise.then((response) => response.status === 200);
    });
  });
}

Browser

This can also be used in the browser thanks to jsDelivr:

<head>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/exponential-rate-limit@1"></script>
  <script type="text/javascript">
    var queue = new ExponentialRateLimit.JobQueue();
  </script>
</head>

Keywords

FAQs

Package last updated on 13 Jul 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