Giter Club home page Giter Club logo

p-key-limit's Introduction

p-key-limit Build Status

Run multiple promise-returning & async functions with limited concurrency by key. The package maintains a mapping of p-limit object per key, so when the limit is called with the same key the promises' execution will be limited (following the concurrency value) but when used with different keys the promises' execution will not limit each other. The package automatically deletes the p-limit objects when they are no longer needed.

Install

$ npm install p-key-limit

Usage

const myDbWrapper = require('./my-db-wrapper'); // assumes to have an async function called getInfo.
const pKeyLimit = require('p-key-limit');

const limit = pKeyLimit(5);

// Each user gets its own "p-limit" queue and can execute 5 parallel info requests.
function getInfoForUser(userId, infoKey) {
    if (limit.getSize(userId) > 20) {
        // user is overflowing with requests!
        throw new Error("That's too much, man!");
    }
    return limit(userId, () => myDbWrapper.getInfo(infoKey))
}

API

pKeyLimit(concurrency)

Returns a limit function.

concurrency

Type: number
Minimum: 1

Concurrency limit.

limit(key, fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

...args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

limit.getSize(key)

Returns the number of promises that are currently running for the given key plus the number of promises that are currently queued for the given key. Useful to detect overflows.

Related

  • p-limit - Run multiple promise-returning & async functions with limited concurrency

License

MIT © Oren Zomer

p-key-limit's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.