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

simple-token-bucket

v2.0.0

Published

A straightforward token bucket implementation with no entanglements

Downloads

2,452

Readme

simple-token-bucket

Typescript token bucket implementation with no dependencies.

$ npm install --save simple-token-bucket
import { TokenBucket } from 'simple-token-bucket';

const bucket = new TokenBucket({
    capacity: 10,
    fillQuantity: 1,
    fillTime: 1000, // in milliseconds
    initialCapacity: 0
});

const timeToWait = bucket.take(3);

Options

  • capacity: the capacity of the token bucket, aka burstiness
  • fillQuantity: how many tokens to add when filling
  • fillTime: how much time it takes to add fillQuantity tokens
  • initialCapacity: the bucket initializes to max capacity by default, but you can optionally change it here

fillQuantity and fillTime combined create a rate which is used to calculate both how many tokens to add at any given moment and how much time remains before a request can be fulfilled. I chose this approach since most of the time it's desirable to specify a rate limit in "X's per Y".

Methods

#take(N)

Attempts to take N tokens from the bucket. Throws a RangeError if N exceeds the bucket's capacity. Returns 0 if successful, else returns the minimum number of milliseconds you must wait before the call will be successful.

Note: This isn't a guarantee that the call will be successful, since other things might take from the bucket in the meantime!

What

A token bucket is a rate-limiting construct that requires no timers and uses a fixed amount of memory. Conceptually, every fillTime ms, fillQuantity "tokens" are added to an imaginary "bucket". When you want to do a thing, you attempt to remove one (or more) tokens from the bucket; if the bucket contains enough tokens, you may proceed, else you must fail.

Token buckets can be used to reject a request from a source that is behaving too aggressively or as a building block for holding your own requests to some fixed rate, though in the latter case you may want to investigate the "leaky bucket" algorithm instead.

Note: In this implementation, tokens are not locked to being added in groups of fillQuantity; instead, before each call to take, as many tokens are added to the bucket as possible given the time elapsed since the last attempt and the rate calculated by the two fill parameters.

Why

The token bucket implementations I found on NPM are either wrapped up in magic for applying rate limiting to things or not very robust. I wanted something to just "run the numbers" and give me the results, similar to what I did with simple-backoff.