-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRateLimiter.js
More file actions
135 lines (110 loc) · 3.38 KB
/
Copy pathRateLimiter.js
File metadata and controls
135 lines (110 loc) · 3.38 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
function RateLimiter(
iterations,
onExhaustionCb = undefined,
logger = undefined
) {
this.iterations = iterations
this.isFirstIteration = true
this.onExhaustionCb = onExhaustionCb
this.groups = {}
this.limit = 0
this.logger = logger || function (logMsg) {}
this.nextIteration = function () {
let newConfig
if (this.iterations.length > 1) {
this.iterations[0].repeat--
if (this.iterations[0].repeat > 0) {
newConfig = this.iterations[0]
} else {
newConfig = this.iterations.shift()
}
} else {
newConfig = this.iterations[0]
newConfig.repeat = 999
}
this.lastLimit = this.isFirstIteration ? newConfig.limit : this.limit
this.lastPenalty = this.isFirstIteration ? newConfig.penalty : this.penalty
this.limit = newConfig.limit
this.penalty = newConfig.penalty || 0
this.resetGroups(this.lastLimit, this.lastPenalty)
this.isFirstIteration = false
this.timoutHandle = setTimeout(
this.nextIteration.bind(this),
newConfig.period
)
}
this.initGroup = function (group) {
if (!this.groups.hasOwnProperty(group)) {
this.groups[group] = {
executions: 0,
remaining: this.limit,
penaltySum: 0,
}
}
}
this.resetGroups = function (lastLimit, lastPenalty) {
for (const group in this.groups) {
if (this.groups[group].executions > lastLimit + 1) {
//limit of last iteration was violated!
this.groups[group].penaltySum += lastPenalty
if (this.groups[group].penaltySum > lastLimit) {
this.groups[group].penaltySum = lastLimit
}
} else {
//limit of last iteration was not violated!
this.groups[group].penaltySum -= lastPenalty
if (this.groups[group].penaltySum < 0) {
this.groups[group].penaltySum = 0
}
}
let remainingBeforeReset = this.groups[group].remaining
this.groups[group].remaining = this.limit - this.groups[group].penaltySum
//always allow at least one request per iteration
if (this.groups[group].remaining <= 0) {
this.groups[group].remaining = 1
}
if (remainingBeforeReset != this.groups[group].remaining) {
this.logger(
`resetting quota for group '${group}' to ${this.groups[group].remaining}`
)
}
this.groups[group].executions = 0
}
// if (Object.keys(this.groups).length > 0) {
// console.log(this.groups)
// }
}
this.nextIteration()
}
RateLimiter.prototype.execute = function (group, callback) {
this.initGroup(group)
this.groups[group].executions++
this.groups[group].remaining--
if (this.groups[group].remaining >= 0) {
callback()
} else {
this.logger(`blocking execution of ${callback.name} for group '${group}'`)
}
if (this.groups[group].remaining == 0) {
this.logger(`quota for group '${group}' exhausted`)
if (this.onExhaustionCb) {
this.onExhaustionCb(group)
}
}
}
RateLimiter.prototype.overrideConfig = function (iterations) {
clearTimeout(this.timoutHandle)
this.iterations = iterations
this.isFirstIteration = true
this.limit = 0
this.groups = {}
this.logger(
'RateLimiter.overrideConfig: ' + JSON.stringify(iterations, null, 2)
)
this.nextIteration()
}
RateLimiter.prototype.destroy = function () {
clearTimeout(this.timoutHandle)
}
module.exports = RateLimiter