npm.io
3.0.0 • Published 1 week ago

@sapphire/ratelimits

Licence
MIT
Version
3.0.0
Deps
0
Size
41 kB
Vulns
0
Weekly
4.4K
Stars
192

Sapphire Logo

@sapphire/ratelimits

Bucket implementation for Ratelimits.

GitHub npm bundle size npm

Table of Contents

Description

There is often a need to apply ratelimits to protect a network from excessive traffic levels on connections routed through it, or limit bot command usages in your Discord bot, or similar things. This package offers two different techniques in the same implementation: the Token Bucket, and the Leaky Bucket.

Features

  • Implements token-bucket and leaky-bucket rate-limiting strategies
  • Maintains independent rate-limit buckets by caller-provided key
  • Exposes whether a bucket is limited and when it can be used again

Installation

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install @sapphire/ratelimits

Usage

Token RateLimitManager
// Import the Bucket class
import { RateLimitManager } from '@sapphire/ratelimits';

// Define a bucket with 1 usage every 5 seconds
const rateLimitManager = new RateLimitManager(5000);

// Acquire the rate limit. The ID can be something like `'global'`, a Discord channel/server/user ID, or anything else.
const ratelimit = rateLimitManager.acquire('some-unique-id-here');

// Check if there is a rate limit right now
if (ratelimit.limited) {
	// Do something when limited, such as throwing an error
}

// We're not rate limited so we drip the bucket. After consuming once, the second run through we'll be rate limited.
ratelimit.consume();

// And now we can finish the flow by returning some form of "success" state.
Leaky RateLimitManager
// Import the Bucket class
import { RateLimitManager } from '@sapphire/ratelimits';

// Define a bucket with 2 usages every 5 seconds
const rateLimitManager = new RateLimitManager(5000, 2);

// Acquire the rate limit. The ID can be something like `'global'`, a Discord channel/server/user ID, or anything else.
const ratelimit = rateLimitManager.acquire('some-unique-id-here');

// Check if there is a rate limit right now
if (ratelimit.limited) {
	// Do something when limited, such as throwing an error
}

// We're not rate limited so we drip the bucket. After consuming twice, the third run through we'll be rate limited.
ratelimit.consume();

// And now we can finish the flow by returning some form of "success" state.

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate With Address
Open Collective Click Here
Ko-fi Click Here
Patreon Click Here
PayPal Click Here

Contributors

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to Sapphire!

Keywords