-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (42 loc) · 1.1 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
const sleep = time => {
return new Promise(cb => setTimeout(cb, time))
}
class Limiter {
constructor (amount, period, prefix, client) {
this.amount = amount
this.period = period
this.prefix = prefix
this.client = client
}
hit () {
const commands = ['set', 'incr', 'pttl']
return this.client.multi([
[commands[0], `${this.prefix}:${this.amount}:${this.period}`, 0, 'px', this.period, 'nx'],
[commands[1], `${this.prefix}:${this.amount}:${this.period}`],
[commands[2], `${this.prefix}:${this.amount}:${this.period}`]
]).exec().then(results => {
return {
allow: results[1][1] <= this.amount,
count: results[1][1],
reset: results[2][1]
}
})
}
execute () {
return this.hit().then(results => {
if (results.allow === true) return results
if (results.allow !== true) {
return sleep(results.reset).then(_ => this.execute())
}
})
}
static create (amount, period, prefix, client) {
return new Limiter(
amount,
period,
prefix,
client
)
}
}
module.exports = Limiter