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

better-limiter

v2.2.0

Published

A simple and smart rate limiter with promises.

Downloads

613

Readme

better-limiter npm downloads

A simple and smart rate limiter with promises.

Install

npm install better-limiter

API

class Limiter {
  /**
   * Creates a limiter.
   * @param {number} [interval=1.01] - The time interval in seconds.
   * @param {number} [rate=10] - The number of operations allowed in the time interval.
   * @param {bool} [evenMode=false] - If to resolve a single task for every (interval/rate) second instead of resolving [rate] tasks for every [interval] second.
   */
  constructor(interval = 1.01, rate = 10, evenMode = false) { ... }

  /**
   * Enqueue into the limiter and return a promise to be resolved automatically at the right time.
   * @async
   * @return {Promise<void>} - a promise that resolves automatically at the right time.
   */
  enter() { ... }

  /**
   * Throttle a function call.
   * @async
   * @param {Function} fn - Function to be called after waiting.
   * @param {...*} args - Arguments for calling fn().
   * @return {Promise<*>} - Promise of the result returned from fn(args).
   */
  throttle(fn, ...args) { ... }

  /**
   * Make a throttled version of function fn.
   * @param {Function} fn - Function to be throttled.
   * @return {Function} - The throttled fn with return type of promise from @see Limiter.throttle
   */
  makeThrottledFn(fn) { ... }

  /**
   * Manually ternimate the interval loop. [This is normally invoked by this.enter() automaticly.]
   * @param {bool} [releaseLeftoverTasks=false] - If to release tasks in the queue or reject them.
   */
  terminate(releaseLeftoverTasks = false) { ... }
}

module.exports = Limiter;

Example

See a full example in example.js

Require and Initiliaze

const Limiter = require(`better-limiter`);

const limt = new Limiter(3.0, 3); // 3 operations allowed for every 3 seconds.
const even_limt = new Limiter(3.0, 3, true); // 1 operation allowed for every (3.0/3)=1.0 second.

Throttle function calles

// Limit log scrolling speed.
for (let i = 0; i < 7; ++i) {
  limt.throttle(console.log, `better-limiter Demo: NormalMode - ${i}`);
  even_limt.throttle(console.error, `better-limiter Demo: EvenMode - ${i}`);
}

Make Throttled Functions and redirect the calls.

const got = require(`got`);
// redirect got.get() to a throttled version get()
const get = limt.makeThrottledFn(got.get);

for (let i = 0; i < 10; ++i)
  get(`http://google.com`).then(res => console.log(`Request#${i} - ${res.statusCode}`));

Changelog

2.1.0 / 2018-02-11

  • (Compatibility) Updated simple-semaphore to be compatible with Node 6! Better JSDoc.

    2.0.1 / 2017-08-10

  • (README) API document update.

    2.0.0 / 2017-08-10

  • (Performance) Performance imporvement with updated simple-semaphore.

  • (New API) throttle(fn, ...args) and makeThrottledFn(fn) added.
  • (Deprecate) Renamed release() into terminate(releaseLeftoverTasks) with minor functionality tweak.

  • (JSDoc) Style improvement.

    1.0.3 / 2017-08-09

  • README revision and bug-fix.

    1.0.0 / Initial Release.

Lisense

Licensed under MIT Copyright (c) 2017-2018 Phoenix Song