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

priority-limiter

v1.2.3

Published

super-simple promise based Limiter

Downloads

136

Readme

Minimal async/await Rate Limiter

Super simple promise-based Rate Limiter:

  • comes in at 9.058 bytes and zero dependencies
  • fully typed
  • maintains in-order execution and supports priorities
  • constant O(1) runtime, no matter the queue length or priorities

Installation

npm install priority-limiter --save

Usage

Simply import, then create a new Limiter Instance. The first parameter describes how many request are allowed within the timeframe of the second paramter (in seconds).

For example, allow 5 requests per 10 seconds:

import Limiter from "priority-limiter";

const limiter = new Limiter(5, 10);

(async () => {
	while (true) {
		await limiter.awaitTurn();

		//do requests...
	}
})();

The default priority is 0. Higher priorities will execute before lower ones. Requests with the same priority are resolved in-order of function calling.

await limiter.awaitTurn(1); //priority 1, will execute before default priority 0.

Lastly, you can specify the maximum duration a request can wait for their turn. Promise will reject when waiting longer:

try {
	await limiter.awaitTurn(0, 30); //timeout if waiting for longer than 30 seconds.
} catch (err) {
	console.log(err); //on timeout: "Limiter timed out."
}

Documentation

new Limiter(request_number, per_seconds)

  • request_number {Number} How many elements will resolve per seconds_number seconds.
  • per_seconds {Number} How long the timeframe for request_number is in seconds. Defaults to 60.

Creates a new Limiter Instance.

awaitTurn([priority, timeout])

  • priority {Number} Elements with higher priority will resolve before elements with lower priority. Defaults to 0.
  • timeout {Number} Reject if element waits for longer than this many seconds. Defaults to 0 (never rejects).

Resolves once ready. Use await to wait until you can continue.

getLength()

Gets the current queue length, i.e. how many calls to awaitTurn() have not been resolved yet.

isEmpty()

Checks whether the queue is empty, i.e. if there are no calls to awaitTurn() that have not been resolved yet.

getUsedResolves()

Get how many calls to awaitTurn() have already been resolved within the last per_seconds seconds (thus another request_number - getUsedResolves() calls can be made right now).

getTimeTillNextResolve()

Get how long it will be until the next call to awaitTurn() will resolve in seconds.

License

MIT