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

@grapelaw/token-bucket

v1.0.2

Published

This package provides a simple and efficient implementation of the token bucket algorithm for rate limiting. It helps you control the frequency of actions (such as API requests) within a specified time frame.

Downloads

892

Readme

Token Bucket

This package provides a simple and efficient implementation of the token bucket algorithm for rate limiting. It helps you control the frequency of actions (such as API requests) within a specified time frame.

The token bucket algorithm provides a simple mechanism to control how many actions (e.g., API requests) can be performed over a given time window. The bucket accumulates tokens at a steady rate which you can fine-tune and stop it for a duration, and each action consumes a token. If no tokens are available, the action can either fail or wait for tokens to refill.

Features

  • Asynchronous Consumption: Supports waiting for tokens asynchronously when they are unavailable.
  • Customizable Refill Rates: Allows fine-tuning of bucket capacity, refill rate, and time windows.
  • Force Stop and Refill: Can force a bucket to wait and stop refilling for a specified duration.
  • Verbose Mode: Logs the state of the token bucket for debugging purposes.

Getting Started

Simple Usage

import { TokenBucket } from 'token-bucket';

const tokenBucket = new TokenBucket({
  capacity: 10,
  fillPerWindow: 1,
  windowInMs: 1000,
});
// Simple Usage: Returns true only when a token is available right away
if (tokenBucket.consume()) {
  // Your logic IF the required tokens are available right away.
}

// Async Usage: Wait until a token is available
if (await tokenBucket.consumeAsync()) {
  // Your logic WHEN required tokens are consumed successfully
}

// Async Usage 2: Wait until a token is available
while (!await tokenBucket.consumeAsync()); // Don't forget to add a semicolon after the while loop
// Your logic here, which will run AFTER the token is available and consumed. 

Usage with Verbose Logging

import { TokenBucket } from 'token-bucket';

const tokenBucket = new TokenBucket({
  capacity: 20,
  fillPerWindow: 5,
  windowInMs: 2000,
}, true);

if (await tokenBucket.consumeAsync()) {
  // Your async logic when a token is consumed successfully
}
// Logs the following
// * When there are not enough tokens, it will log a message, which also tells when will the tokens be available
// * Every 10 seconds, the remaining tokens
// * Forced Stop started-ended

Singleton Recommendation

import { TokenBucket } from 'token-bucket';

export const tokenBucket = new TokenBucket({
  capacity: 20,
  fillPerWindow: 5,
  windowInMs: 2000,
}, true);

// For utilizing the same api / resource, using a single instance of tokenBucket is recommended throughout the application is recommended.

Configuration Options

TokenBucketOptions

  • capacity: The maximum number of tokens in the bucket.
  • fillPerWindow: The number of tokens added to the bucket per window.
  • windowInMs: The size of the window in milliseconds.
  • initialTokens: The initial number of tokens in the bucket. Defaults to the capacity if not provided.
import { TokenBucket } from 'token-bucket';

const tokenBucket = new TokenBucket({
  capacity: 20,
  fillPerWindow: 2,
  windowInMs: 1500,
  initialTokens: 40,
});

Methods

consume(amount: number = 1): boolean

Consumes the specified number of tokens from the bucket. Returns true if the tokens were successfully consumed, otherwise false.

consumeAsync(amount: number = 1): Promise<boolean>

Asynchronously consumes the specified number of tokens from the bucket. Returns a promise that resolves to true if the tokens were successfully consumed, otherwise false.

start(): TokenBucket

Starts the internal timer for refilling tokens. Returns the TokenBucket instance to allow chaining.

stop(): void

Stops the internal timer for refilling tokens.

forceWaitUntilMilisecondsPassed(delayInMs: number): void

Forces the bucket to wait until the specified amount of milliseconds have passed before refilling. The intended use of this method is aligning with the rate limit of the API in case your TokenBucket is providing more tokens than it should (i.e: if you are using shared rate limit within different places).

Authors

Nurbaki Kasikci - GitHub - Twitter

Contribution

We welcome contributions to improve this package and encourage users to submit bug reports, feature requests, or any other contributions that can enhance the project. Please follow the guidelines below to contribute:

  1. Report Issues: If you encounter any issues or have suggestions for improvements, please open an issue on GitHub
  2. Pull Requests: You are welcome to submit Pull Requests (PRs) for bug fixes or new features. Make sure to follow the established coding conventions and explain the purpose of your changes.