Gathering detailed insights and metrics for dynamic-throttled-queue
Gathering detailed insights and metrics for dynamic-throttled-queue
Gathering detailed insights and metrics for dynamic-throttled-queue
Gathering detailed insights and metrics for dynamic-throttled-queue
Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.
npm install dynamic-throttled-queue
Typescript
Module System
Node Version
NPM Version
JavaScript (95.33%)
HTML (4.67%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1 Stars
23 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Aug 28, 2024
Latest Version
1.1.3
Package Id
dynamic-throttled-queue@1.1.3
Unpacked Size
29.68 kB
Size
7.74 kB
File Count
17
NPM Version
10.7.0
Node Version
20.15.1
Published on
Aug 28, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This project was forked from shaunpersad/throttled-queue
Dynamically throttles arbitrary code to execute between a minuimum and maximum number of times per interval. Best for making throttled API requests.
For example, making network calls to popular APIs such as Twitter is subject to rate limits. By wrapping all of your API calls in a throttle, it will automatically adjust your requests to be within the acceptable rate limits.
Unlike the throttle
functions of popular libraries like lodash and underscore, dynamic-throttled-queue
will not prevent any executions. Instead, every execution is placed into a queue, which will be drained at the desired rate limit.
v1.1.1 - Add default for option object
v1.1.0 - Adding Retry ability, if returning false, function will be added back to the master queue to be retired.
v1.0.0 - Initial Release
Can be used in a Node.js environment, or directly in the browser.
npm install dynamic-throttled-queue
<script src="dynamic-throttled-queue.min.js"></script>
##Options
Param | Type | Description |
---|---|---|
min_rpi | {number} | Minimum requests per interval |
max_rpi | [number=min_rpi] | Maximum requests per interval |
interval | {number} | Number of milliseconds between each batch of requests |
evenly_spaced | [boolean=true] | If true requests will be distributed throughout the interval time |
errors_per_second | [number=5] | Number of errors per second before deciding to either increase or decrease the current rpi |
back_off | [boolean=true] | If true and we hit the errors_per_interval watermark, we will back off for 1 interval |
retry | [number=0] | If greater than 0, any failed callbacks, will be put back onto the queue to retry upto X times |
require
the factory function:1var throttledQueue = require('dynamic-throttled-queue');
Else, include it in a script tag in your browser and throttledQueue
will be globally available.
1const throttle = throttledQueue({min_rpi:5, interval:1000}); // at most 5 requests per second.
throttle
instance as a function to enqueue actions:1throttle(function() { 2 // perform some type of activity in here. 3});
Rapidly assigning network calls to be run, but they will be limited to 1 request per second.
1var throttledQueue = require('dynamic-throttled-queue'); 2var throttle = throttledQueue({min_rpi:1, interval:1000}); // at most make 1 request every second. 3 4for (let i = 0; i < 100; i++) { 5 6 throttle(function() { 7 // make a network request. 8 fetch('https://api.github.com/search/users?q=adrianbrowning').then(console.log); 9 }); 10}
Wherever the throttle
instance is used, your action will be placed into the same queue,
and be subject to the same rate limits.
1const throttledQueue = require('dynamic-throttled-queue'); 2const throttle = throttledQueue({min_rpi:1, interval:60 * 1000}); // at most make 1 request every minute. 3 4for (let x = 0; x < 50; x++) { 5 6 throttle(function() { 7 // make a network request. 8 fetch('https://api.github.com/search/users?q=adrianbrowning').then(console.log); 9 }); 10} 11for (let y = 0; y < 50; y++) { 12 13 throttle(function() { 14 // make another type of network request. 15 fetch('https://api.github.com/search/repositories?q=throttled-queue+user:adrianbrowning').then(console.log); 16 }); 17}
By specifying a number higher than 1 for the min_rpi, and setting evenly_spaced: false
you can dequeue multiple actions within the given interval:
1var throttledQueue = require('dynamic-throttled-queue'); 2var throttle = throttledQueue({min_rpi:10, interval:1000, evenly_spaced: true}); // at most make 10 requests every second. 3 4for (let x = 0; x < 100; x++) { 5 6 throttle(function() { 7 // This will fire at most 10 a second, as rapidly as possible. 8 fetch('https://api.github.com/search/users?q=adrianbrowning').then(console.log); 9 }); 10}
By default your actions are evenly distributed over the interval evenly_spaced: true
:
1const throttledQueue = require('dynamic-throttled-queue'); 2const throttle = throttledQueue({min_rpi: 10, interval: 1000, evenly_spaced:true}); // at most make 10 requests every second, but evenly spaced. 3 4for (let x = 0; x < 100; x++) { 5 6 throttle(function() { 7 // This will fire at most 10 requests a second, spacing them out instead of in a burst. 8 fetch('https://api.github.com/search/users?q=adrianbrowning').then(console.log); 9 }); 10}
By suppling a min_rpi
& max_rpi
value to the options object, you will be able to have a dynamically adjusting queue. This works by the function passed to throttle
returning false
if there was an issue. The starting requests per interval is as close to halfway bewteen the min_rpi
and max_rpi
, rounded to the nearest whole number.
The second part of this is the errors_per_second
option. This is set by default to 5 errors per second. Every X seconds, a check is made to see how many errors we have seen (through the use of return false
) and if we see X or more, then the current requests per interval will decrease until we hit the min_rpi
value. If between 0 - X errors are seen then we keep with the current requests per interval as is. Finally if there are 0 errors in the last check period then we will increase the current requests per interval until we reach max_rpi
.
1const throttledQueue = require('dynamic-throttled-queue'); 2const throttle = throttledQueue({min_rpi: 1, max_rpi:5, interval: 1000}); // at most make 5 requests every second. 3 4for (let x = 0; x < 100; x++) { 5 6 throttle(function() { 7 return !(Date.now() % 2); 8 }); 9}
By suppling backoff:true
in the options, every time we hit the errors_per_second
mark, we will backoff from the next batch of calls for 1 inteveral
1const throttledQueue = require('dynamic-throttled-queue'); 2const throttle = throttledQueue({min_rpi: 10, interval: 1000, backoff:true, errors_per_second:2}); 3// at most make 10 requests every second, if more than 2 errors per second, then back off for 1 full interval of 1 second. 4 5for (let x = 0; x < 100; x++) { 6 7 throttle(function() { 8 return !(Date.now() % 2); 9 }); 10}
Note: The tests take a few minutes to run. Watch the console to see how closely the actual rate limit gets to the maximum.
Run npm test
.
Open test/index.html
in your browser.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
38 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More