forked from jvimedia/AppDotNetAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter.js
54 lines (51 loc) · 1.51 KB
/
ratelimiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
rateLimit(function() {
request.get(function(err, res, body) {
logRequest();
});
});
*/
// minutely status report
setInterval(function () {
var ts=new Date().getTime();
process.stdout.write("\n");
var ref=module.exports;
console.log('rate.limit report, resetting in', ts-ref.resetAt, 'ms');
// just need a redis info call to pull memory and keys stats
}, 60*1000);
// pass in proxy settings or just conf it?
module.exports = {
resetAt: Date.now(),
rateCounters: {},
logRequest: function(isAuth, isWrite) {
var now=Date.now();
if (now>=this.resetAt) {
console.log('resetting rate limits');
// how often do we want to reset?
this.resetAt=now+60000; // 50 calls per 1 minute is the strickest
this.rateCounters[0]={};
this.rateCounters[0][0]=50;
this.rateCounters[0][1]=50;
this.rateCounters[1]={};
this.rateCounters[1][0]=83;
this.rateCounters[1][1]=20;
}
console.log('watching rate limit');
this.rateCounters[isAuth][isWrite]--;
},
rateLimit: function(isAuth, isWrite, callback) {
if (this.rateCounters[isAuth]===undefined) this.rateCounters[isAuth]={};
var callsleft=this.rateCounters[isAuth][isWrite];
console.log('callsleft for', isAuth, isWrite, 'are', callsleft);
if (callsleft<1) {
var delay=this.resetAt-Date.now();
console.log('delaying call by', delay);
setTimeout(function() {
callback();
}, delay);
return true;
}
callback();
}
}
module.exports.logRequest(0, 0);