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

dyna-throttle

v1.1.0

Published

Module for dynamically throttling Promise execution based on latency

Downloads

11

Readme

dyna-throttle

About

dyna-throttle is a simple module for dynamically throttling Promises in node.js.

Install

npm install dyna-throttle

Usage

Include dyna-throttle with require('dyna-throttle'). dyna-throttle is returned as a singleton.

Pass dyna-throttle a function that returns a Promise, along with a data object (to pass to the Promise), onFulfill function and onReject function, like so:

throttle(myPromiseFunc, data, fulfill, reject);

dyna-throttle will add your Promise to its queue and automatically delay execution of the next promise in the queue based on execution time and factor.

Queues are namespaced based on the name of the function passed in.

Example

Inspiration for this module was making regular HTTP requests without bogging down the web server, even if that server is under heavy load.

With a request function such as this:

function makeRequest (port) {
    return new Promise(function (fulfill, reject) {
        http.get('http://localhost:' + port, function (res) {
            var body = '';
            res.on('error', reject);
            res.on('data', function (data) {
                body += data;
            });
            res.on('end', function () {
                fulfill(body);
            })
        })
    });
}

We can schedule 1,000 requests immediately with:

for (let i=0; i<1000; i++) {
    throttle(makeRequest, 8888, onFulfill, onReject);
}

As a result, the requests are executed as quickly or as slowly as the server responds and as you decide (via factor), without having to pick an arbitrary delay interval.

Methods

All methods return a Promise. namespace can be a string or the function you are referring to.

setFactor(namespace, factor) - Sets multiplier of average execution time.

setMaxSamples(namespace, maxSamples) - Sets number of timings to keep in the log to be used for averaging.

setTimeout(namespace, timeout) - Sets max length (ms) a timer will be kept active

getDelay(namespace) - Gets the average delay - including factor - for the given namespace.

Note: If a namespace has not yet been registered when calling any of the above methods, it will be created.

Defaults

  • Factor: 10
  • maxSamples: 50
  • timeout: 60000

TODO

  • custom namespaces
  • plain function support
  • Promise chaining

License

Copyright (c) 2016, Kyle Anderson [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.