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

@good-idea/leaky-bucket

v3.1.2

Published

A fast and efficient leaky bucket implementation

Downloads

223

Readme

leaky-bucket

A fast and efficient leaky bucket.

NOTE: This is a browser-safe typescript version forked from linaGirl/leaky-bucket

Leaky buckets are often used to rate limits calls to APIs. They can be used on the server, to make sure the client does not send too many requests and on the client, to make sure to not to send too many requests to a server rate limiting using a leaky bucket. Leaky buckets are burstable: if a server lets a client send 10 requests per minute, it normally lets the user burst those 10 reuests. after that only one request per 6 seconds may be sent (60 seconds / 10 requests). If the user stops sending requests, the bucket is filled up again so that the user may send a burst of requests again.

ATTENTION: Version 3+ is a rewrite of this library, it makes use of es modules and thus needs to be started using the --experimental-modules flag in node 10 and 12.

installation

npm i leaky-bucket

build status

Build Status

API

Constructor

Sets up the leaky bucket. Accpets three optional options

  • capacity: this is the amount of items that may be processed per interval, if the items cost is 1 (which is the default)
  • interval: this is the interval, in which the capacity may be used
  • timeout: defines, how long it takes until items are rejected due to an overflow. defaults to the value of the interval, so that the overflow occurs at the same time the bucket is empty.
import LeakyBucket from 'leaky-bucket'

// a leaky bucket, that will burst 60 items, then will throttle the items to one per seond
const bucket = new Bucket({
  capacity: 60,
  interval: 60,
})

throttle

The throttle method is used to delay items until the bucket leaks them, thus rate limiting them. If the bucket is overflowing, which is when items cannot be executed within the timeout, the throttle method will reject using an error.

This method accepts two optional paramters:

  • cost: the cost of the item, defaults to 1
  • append: if the ittem should be appended or added at the first position in the queue, defaults to true
/// throttle an individual item
await bucket.throttle()
doThings()

// Throttle aset of items, waiting for each one to complete, before it's added to the bucket
for (const item of set.values()) {
  await bucket.throttle()

  doThings()
}

// throttle items, add them to the bucket in paralle
await Promise.all(
  Array.from(set).map(async (item) => {
    await bucket.throttle()

    doThings()
  }),
)

pause

The pause method can be use to pause the bucket for n seconds until it is allwed to resume.

bucket.pause()