Skip to content

Commit

Permalink
added millisecondy limit feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mindactuate committed Jun 16, 2019
1 parent 92a8597 commit 82e0811
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Sometimes there is no other way to be patient and to wait for the API rate limit

## Planned Features

- Add secondly limit feature (e.g. 2 reqs per second).
- If required I can add a "maxConcurrent" feature like in [bottleneck](https://www.npmjs.com/package/bottleneck)
- I'm also trying to figure out how to deal with rate limit headers like ```x-ratelimit-remaining``` or ```retry-after```. At first sight this is not so easy, because the first headers only arrive after the first API call. And until then, any number of API calls could have been fired.
- [x] Add secondly limit feature (e.g. 2 reqs per second).
- [] If required I can add a "maxConcurrent" feature like in [bottleneck](https://www.npmjs.com/package/bottleneck)
- [] I'm also trying to figure out how to deal with rate limit headers like ```x-ratelimit-remaining``` or ```retry-after```. At first sight this is not so easy, because the first headers only arrive after the first API call. And until then, any number of API calls could have been fired.

## Installing

Expand All @@ -42,6 +42,7 @@ Perhaps you already know about the limits (maybe from the API docs).
startWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function if waiting necessary
endWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function after waiting
waitingTickCallback: function(info){console.log(info)}, // default is function(){}, calls a function every tick
msBetweenTwoCalls: 1000, // default is 0 milliseconds (no waiting time between two calls)
minutelyLimit: 50, // default is Infinity (no minutely limit set)
hourlyLimit: 750, // default is Infinity (no hourly limit set)
test: false // default is false (if true, max waiting time is 5 secs)
Expand Down
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const x = Infinity;
* startWaitingCallback: {function}, // default is function(){}, calls a function if waiting necessary
* endWaitingCallback: {function}, // default is function(){}, calls a function after waiting
* waitingTickCallback: {function}, // default is function(){}, calls a function every tick
* msBetweenTwoCalls: {integer}, // default is 0 milliseconds (no waiting time between two calls)
* minutelyLimit: {integer}, // default is Infinity (no minutely limit set)
* hourlyLimit: {integer}, // default is Infinity (no hourly limit set)
* test: {boolean}, // default is false (if true, max waiting time is 5 secs)
Expand All @@ -26,6 +27,7 @@ const x = Infinity;
* @constructor
*/
function LimitWaiter(options) {
this.msWait = options ? options.msBetweenTwoCalls ? Number.isSafeInteger(options.msBetweenTwoCalls) ? options.msBetweenTwoCalls > 0 ? options.msBetweenTwoCalls : 0 : 0 : 0 : 0;
this.mLim = options ? options.minutelyLimit ? Number.isSafeInteger(options.minutelyLimit) ? options.minutelyLimit > 0 ? options.minutelyLimit : x : x : x : x;
this.hLim = options ? options.hourlyLimit ? Number.isSafeInteger(options.hourlyLimit) ? options.hourlyLimit > 0 ? options.hourlyLimit : x : x : x : x;
this.startWaitingCallback = options ? options.startWaitingCallback ? options.startWaitingCallback : function () { } : function () { };
Expand Down Expand Up @@ -66,7 +68,20 @@ let workOnQueueLW = async (ctx) => {
function checkWaitingLW(ctx) {
return new Promise(function (resolve) {
if (ctx.mC < ctx.mLim && ctx.hC < ctx.hLim) {
resolve(); // do not have to wait
if (ctx.msWait > 0) {
ctx.startWaitingCallback({
millisecondsToWait: ctx.msWait
})
waitMilliseconds(ctx.msWait)
.then(() => {
ctx.endWaitingCallback({
millisecondsWaited: ctx.msWait
})
resolve();
})
} else {
resolve(); // do not have to wait
}
} else if (ctx.hC >= ctx.hLim) {
let d = new Date().getMinutes();
let minutes = 60 - d;
Expand Down Expand Up @@ -110,6 +125,15 @@ function checkWaitingLW(ctx) {
})
}

function waitMilliseconds(milliseconds) {
return new Promise(function (resolve) {
let t = setTimeout(function () {
clearTimeout(t);
resolve();
}, milliseconds)
})
}

function waitSeconds(seconds, tickCallback) {
return new Promise(function (resolve) {
let count = 1;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "patiently",
"version": "2.0.0",
"version": "2.1.0",
"description": "API rate limit handler. Make your API client patient. A legal way to wait for API rate limit resets.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let optionsTest1 = {
startWaitingCallback: function (info) { console.log("start waiting", info) },
endWaitingCallback: function (info) { console.log("end waiting", info) },
waitingTickCallback: function (info) { console.log("tick", info) },
msBetweenTwoCalls: 1000,
minutelyLimit: 5,
hourlyLimit: 10,
test: true
Expand Down

0 comments on commit 82e0811

Please sign in to comment.