-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (49 loc) · 1.52 KB
/
index.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
55
56
57
const limiters = new Map();
const pending_calls = new Set();
/**
*
* @param {string} key
* @param {number} max_token_count maximum token count
* @param {number} window time window (in milliseconds)
*/
async function rateLimiter(key, max_token_count, window){
if(!limiters.has(key)){
//init the limiter
limiters.set(key, {
tokens: 0,
next_refill: null,
/** @param {number} current_time process.uptime() */
checkAndRefill(current_time){
// refill the bucket
if(this.next_refill == null || current_time > this.next_refill){
this.tokens = max_token_count;
this.next_refill = current_time + (window / 1000);
}
}
});
limiters.get(key).checkAndRefill(process.uptime());
}
const limiter = limiters.get(key);
return new Promise(resolve => {
function cb(){
// if there are available tokens, allow the function to proceed
if(limiter.tokens > 0){
pending_calls.delete(cb);
limiter.tokens -= 1;
resolve();
return true;
}
return false;
}
if(!cb())
pending_calls.add(cb);
});
}
setInterval(() => {
const current_time = process.uptime();
for(let limiter of limiters)
limiter[1].checkAndRefill(current_time);
for(let cb of pending_calls)
cb();
}, 50);
module.exports = rateLimiter;