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

porro

v2.2.0

Published

Fast leaky bucket algorithm implementation without timers

Downloads

440

Readme

npm version ci Coverage Status JavaScript Style Guide

Porro is a simple and fast implementation of the leaky bucket algorithm.

Naming things is hard

During a scene in Cloudy with a Chance of Meatballs 2, the protagonists are moving with a boat. At some point, a wild tribe of living leeks attacks the boat, and the scientist shouts: "There's a leak in the boat!". Porro means leek in Italian. Leaky bucket algorithm -> Leak -> Leek -> Porro.

Features

  • No dependencies: small footprint.
  • No timers: unlike some other implementations, Porro doesn't use any timer.
  • ES modules support: native Node.js import/export.
  • CommonJS support: common runtimes are still supported.
  • ES6: code just uses ES6 features.
  • TypeScript: this package includes its own TypeScript declarations.

Install

Open your favourite shell and run:

npm install porro

API

new Porro(options)

Porro's constructor.

  • options <Object>
    • bucketSize <Number> The size (number of tokens) of the bucket.
    • interval <Number> Time interval in milliseconds to refill the bucket.
    • tokensPerInterval <Number> Number of refilled tokens after the specified interval.
    • [tokens] <Number> Initial number of tokens. Defaults to bucketSize.
  • Returns: <Porro>

Porro::tokens

Getter and setter property. Gets or sets the current number of available tokens.

Porro::request([quantity])

Returns the amount of time that the pending request needs to wait before executing.

  • [quantity] <Number> Number (positive integer) of tokens to burn for the current request. Defaults to 1.
  • Returns: <Number>

Porro::throttle([quantity])

Returns a Promise that will resolve when It's appropriate to execute the pending request.

  • [quantity] <Number> Number (positive integer) of tokens to burn for the current request. Defaults to 1.
  • Returns: <Promise> Resolves with the waited milliseconds.

Porro::reset()

Resets the bucket to its original status.

Example

import { Porro } from 'porro'

// 2 requests per second with a "buffer" of 5 requests
const bucket = new Porro({
  bucketSize: 5,
  interval: 1000,
  tokensPerInterval: 2
})

async function run () {
  // Create 10 "requests"
  const items = new Array(10).fill(null)

  // Execute all requests at the same time
  log('script is starting')
  await Promise.all(items.map(doSomething))
}

async function doSomething (value, index) {
  // Get the waiting time for the currenct request
  const ms = bucket.request()

  // Sleep for the required time
  // You can also use `await bucket.throttle()` for simplicity
  if (ms > 0) {
    log(`request #${index} will wait ${ms}ms`)
    await sleep(ms)
  }

  // Do something!
  log(`run request #${index}`)
}

function log (message) {
  console.log(`[${new Date().toISOString()}]`, message, `(bucket has now ${bucket.tokens} tokens)`)
}

function sleep (ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

run()
// [2023-01-27T14:21:13.776Z] script is starting (bucket has now 5 tokens)
// [2023-01-27T14:21:13.779Z] run request #0 (bucket has now 4 tokens)
// [2023-01-27T14:21:13.779Z] run request #1 (bucket has now 3 tokens)
// [2023-01-27T14:21:13.779Z] run request #2 (bucket has now 2 tokens)
// [2023-01-27T14:21:13.779Z] run request #3 (bucket has now 1 tokens)
// [2023-01-27T14:21:13.779Z] run request #4 (bucket has now 0 tokens)
// [2023-01-27T14:21:13.779Z] request #5 will wait 1000ms (bucket has now -1 tokens)
// [2023-01-27T14:21:13.779Z] request #6 will wait 1000ms (bucket has now -2 tokens)
// [2023-01-27T14:21:13.779Z] request #7 will wait 2000ms (bucket has now -3 tokens)
// [2023-01-27T14:21:13.780Z] request #8 will wait 2000ms (bucket has now -4 tokens)
// [2023-01-27T14:21:13.780Z] request #9 will wait 3000ms (bucket has now -5 tokens)
// [2023-01-27T14:21:14.782Z] run request #5 (bucket has now -3 tokens)
// [2023-01-27T14:21:14.782Z] run request #6 (bucket has now -3 tokens)
// [2023-01-27T14:21:15.779Z] run request #7 (bucket has now -1 tokens)
// [2023-01-27T14:21:15.780Z] run request #8 (bucket has now -1 tokens)
// [2023-01-27T14:21:16.782Z] run request #9 (bucket has now 1 tokens)

Donate

Thank you!

"Buy Me A Coffee"