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

backoff-pool

v1.0.3

Published

A helper library to manage expotential backoff intervals of different resources.

Downloads

5

Readme

backoff-pool

A helper library to manage expotential backoff intervals of different resources.

This can be used to manage retry interval of login attempts per IP address and apply rate limit on abnormal request / rare API calls.

npm Package Version Minified Package Size Minified and Gzipped Package Size

Installation

npm install backoff-pool

Importing this package

import from typescript:

import { BackoffPool } from 'backoff-pool'

Or import from javascript:

const { BackoffPool } = require('backoff-pool')

Typescript Types

export class BackoffPool<Key = string | number> {
  constructor(options?: BackoffPoolOptions)

  success(key: Key): void
  fail(key: Key): void
  applyRandomBackoff(key: Key, range?: number): void

  getInterval(key: Key): number
  getUnlockTime(key: Key): number | undefined

  isLocked(key: Key): boolean
  isAvailable(key: Key): boolean
}

export interface BackoffPoolOptions {
  defaultInterval?: number // default 0
  initialBackoffInterval?: number // default 1 second
  maxBackoffInterval?: number // default unlimited
  backoffFactor?: number // default 2
  randomBackoffRatio?: number // default 0.2 (20%)
}

export let defaultBackoffPoolOptions: Required<BackoffPoolOptions>

Usage Example

let backoffPool = new BackoffPool({
  defaultInterval: 0,
  initialBackoffInterval: 2000,
  backoffFactor: 2,
})

let clientIP = 'stub'

console.log('0', backoffPool.isAvailable(clientIP)) // true: is available
console.log('0', backoffPool.isLocked(clientIP)) // false: not locked
console.log('0', backoffPool.getUnlockTime(clientIP)) // undefined: not locked

backoffPool.fail(clientIP) // lock for 2 seconds
console.log('1', backoffPool.isAvailable(clientIP)) // false: not available
console.log('1', backoffPool.getUnlockTime(clientIP)) // Date.now() + 2000

backoffPool.fail(clientIP) // failed again, lock for 4 seconds
backoffPool.fail(clientIP) // failed again, lock for 8 seconds
console.log('2', backoffPool.getInterval(clientIP)) // 8000: 8 seconds

await sleep(7 * 1000)
console.log('7', backoffPool.isAvailable(clientIP)) // false: still locked

await sleep(1 * 1000)
console.log('8', backoffPool.isAvailable(clientIP)) // true: unlocked now

backoffPool.success(clientIP) // reset the interval
console.log('9', backoffPool.getInterval(clientIP)) // 0: same as defaultInterval

backoffPool.fail(clientIP) // lock for 2 seconds instead of 16 seconds

await sleep(2 * 1000)
console.log('10', backoffPool.isLocked(clientIP)) // false: unlocked now

More examples see test/example.ts and test/core.test.ts

License

This project is licensed with BSD-2-Clause

This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:

  • The freedom to run the program as you wish, for any purpose
  • The freedom to study how the program works, and change it so it does your computing as you wish
  • The freedom to redistribute copies so you can help others
  • The freedom to distribute copies of your modified versions to others