-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRateLimit.js
188 lines (158 loc) · 5.05 KB
/
RateLimit.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*!
* Redback
* Copyright(c) 2011 Chris O'Hara <cohara87@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Structure = require('../Structure');
/**
* See https://gist.github.com/chriso/54dd46b03155fcf555adccea822193da
*
* Count the number of times a subject performs an action over an interval
* in the immediate past - this can be used to rate limit the subject if
* the count goes over a certain threshold. For example, you could track
* how many times an IP (the subject) has viewed a page (the action) over
* a certain time frame and limit them accordingly.
*
* Usage:
* `redback.createRateLimit(action [, options]);`
*
* Options:
* `bucket_interval` - default is 5 seconds
* `bucket_span` - default is 10 minutes
* `subject_expiry` - default is 20 minutes
*
* Reference:
* https://gist.github.com/chriso/54dd46b03155fcf555adccea822193da
* http://redis.io/topics/data-types#hash
*
* Redis Structure:
* `(namespace:)action:<subject1> = hash(bucket => count)`
* `(namespace:)action:<subject2> = hash(bucket => count)`
* `(namespace:)action:<subjectN> = hash(bucket => count)`
*/
var RateLimit = exports.RateLimit = Structure.new();
/**
* Setup the RateLimit structure.
*
* @param {Object} options (optional)
* @api private
*/
RateLimit.prototype.init = function (options) {
options = options || {};
this.bucket_span = options.bucket_span || 600;
this.bucket_interval = options.bucket_interval || 5;
this.subject_expiry = options.subject_expiry || 1200;
this.bucket_count = Math.round(this.bucket_span / this.bucket_interval);
}
/**
* Get the bucket associated with the current time.
*
* @param {int} time (optional) - default is the current time (ms since epoch)
* @return {int} bucket
* @api private
*/
RateLimit.prototype.getBucket = function (time) {
time = (time || new Date().getTime()) / 1000;
return Math.floor((time % this.bucket_span) / this.bucket_interval);
}
/**
* Increment the count for the specified subject.
*
* @param {string} subject
* @param {Function} callback (optional)
* @return this
* @api public
*/
RateLimit.prototype.add = function (subject, callback) {
if (Array.isArray(subject)) {
return this.addAll(subject, callback);
}
var bucket = this.getBucket(), multi = this.client.multi();
subject = this.key + ':' + subject;
//Increment the current bucket
multi.hincrby(subject, bucket, 1)
//Clear the buckets ahead
multi.hdel(subject, (bucket + 1) % this.bucket_count)
.hdel(subject, (bucket + 2) % this.bucket_count)
//Renew the key TTL
multi.expire(subject, this.subject_expiry);
multi.exec(function (err) {
if (!callback) return;
if (err) return callback(err);
callback(null);
});
return this;
}
/**
* Count the number of times the subject has performed an action
* in the last `interval` seconds.
*
* @param {string} subject
* @param {int} interval
* @param {Function} callback
* @return this
* @api public
*/
RateLimit.prototype.count = function (subject, interval, callback) {
var bucket = this.getBucket(),
multi = this.client.multi(),
count = Math.floor(interval / this.bucket_interval);
subject = this.key + ':' + subject;
//Get the counts from the previous `count` buckets
multi.hget(subject, bucket);
while (count--) {
multi.hget(subject, (--bucket + this.bucket_count) % this.bucket_count);
}
//Add up the counts from each bucket
multi.exec(function (err, counts) {
if (err) return callback(err, null);
for (var count = 0, i = 0, l = counts.length; i < l; i++) {
if (counts[i]) {
count += parseInt(counts[i], 10);
}
}
callback(null, count);
});
return this;
}
/**
* An alias for `ratelimit.add(subject).count(subject, interval);`
*
* @param {string} subject
* @param {int} interval
* @param {Function} callback
* @return this
* @api public
*/
RateLimit.prototype.addCount = function (subject, interval, callback) {
var bucket = this.getBucket(),
multi = this.client.multi(),
count = Math.floor(interval / this.bucket_interval);
subject = this.key + ':' + subject;
//Increment the current bucket
multi.hincrby(subject, bucket, 1)
//Clear the buckets ahead
multi.hdel(subject, (bucket + 1) % this.bucket_count)
.hdel(subject, (bucket + 2) % this.bucket_count)
//Renew the key TTL
multi.expire(subject, this.subject_expiry);
//Get the counts from the previous `count` buckets
multi.hget(subject, bucket);
while (count--) {
multi.hget(subject, (--bucket + this.bucket_count) % this.bucket_count);
}
//Add up the counts from each bucket
multi.exec(function (err, counts) {
if (err) return callback(err, null);
for (var count = 0, i = 4, l = counts.length; i < l; i++) {
if (counts[i]) {
count += parseInt(counts[i], 10);
}
}
callback(null, count);
});
return this;
}