@withjoy/limiter 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/limitd-redis/LICENSE +21 -0
- package/limitd-redis/README.md +183 -0
- package/limitd-redis/docker-compose.yml +11 -0
- package/limitd-redis/index.js +2 -0
- package/limitd-redis/lib/cb.js +45 -0
- package/limitd-redis/lib/client.js +135 -0
- package/limitd-redis/lib/db.js +501 -0
- package/limitd-redis/lib/db_ping.js +106 -0
- package/limitd-redis/lib/put.lua +31 -0
- package/limitd-redis/lib/take.lua +48 -0
- package/limitd-redis/lib/utils.js +116 -0
- package/limitd-redis/lib/validation.js +64 -0
- package/limitd-redis/opslevel.yml +6 -0
- package/limitd-redis/package-lock.json +3484 -0
- package/limitd-redis/package.json +31 -0
- package/limitd-redis/test/cb.tests.js +124 -0
- package/limitd-redis/test/client.tests.js +194 -0
- package/limitd-redis/test/db.tests.js +1318 -0
- package/limitd-redis/test/validation.tests.js +124 -0
- package/limiter.js +58 -2
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -23,6 +23,12 @@ Your client can use any subset of these Buckets, but when it does, the Best Prac
|
|
|
23
23
|
|
|
24
24
|
However, for flexibility reasons, this is not strictly enforced.
|
|
25
25
|
|
|
26
|
+
## Why limitd-redis ?
|
|
27
|
+
Although this package is available on npm, it's no longer actively maintained. We've observed bugs in the existing published version, but these issues have already been resolved in the [limitd-redis](https://github.com/auth0/limitd-redis) gitHub repository. To address this, we've imported the latest stable version(v7.8.1) from there github repository and using it locally in the limiter.
|
|
28
|
+
|
|
29
|
+
In case we need to update in the future, we can replace the "limitd-redis" directory with the latest version of [limitd-redis](https://github.com/auth0/limitd-redis) from their GitHub repository.
|
|
30
|
+
|
|
31
|
+
Note: We need to add dependency from limitd-redis to limiter package json before publish. We are using limitd-redis as directly not package inside limiter.
|
|
26
32
|
|
|
27
33
|
## Testing:
|
|
28
34
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Auth0, Inc. <support@auth0.com> (http://auth0.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
[](https://travis-ci.org/auth0/limitd-redis)
|
|
2
|
+
|
|
3
|
+
`limitd-redis` is client for limits on top of `redis` using [Token Buckets](https://en.wikipedia.org/wiki/Token_bucket).
|
|
4
|
+
It's a fork from [LimitDB](https://github.com/limitd/limitdb).
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
npm i limitd-redis
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Configure
|
|
13
|
+
|
|
14
|
+
Create an instance of `limitd-redis` as follows:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
const Limitd = require('limitd-redis');
|
|
18
|
+
|
|
19
|
+
const limitd = new Limitd({
|
|
20
|
+
uri: 'localhost',
|
|
21
|
+
//or
|
|
22
|
+
nodes: [{
|
|
23
|
+
port: 7000,
|
|
24
|
+
host: 'localhost'
|
|
25
|
+
}],
|
|
26
|
+
buckets: {
|
|
27
|
+
ip: {
|
|
28
|
+
size: 10,
|
|
29
|
+
per_second: 5
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
prefix: 'test:',
|
|
33
|
+
ping: {
|
|
34
|
+
interval: 1000,
|
|
35
|
+
maxFailedAttempts: 5,
|
|
36
|
+
reconnectIfFailed: true
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Options available:
|
|
42
|
+
|
|
43
|
+
- `uri` (string): Redis Connection String.
|
|
44
|
+
- `nodes` (array): [Redis Cluster Configuration](https://github.com/luin/ioredis#cluster).
|
|
45
|
+
- `buckets` (object): Setup your bucket types.
|
|
46
|
+
- `prefix` (string): Prefix keys in Redis.
|
|
47
|
+
- `ping` (object): Configure ping to Redis DB.
|
|
48
|
+
|
|
49
|
+
Buckets:
|
|
50
|
+
|
|
51
|
+
- `size` (number): is the maximum content of the bucket. This is the maximum burst you allow.
|
|
52
|
+
- `per_interval` (number): is the amount of tokens that the bucket receive on every interval.
|
|
53
|
+
- `interval` (number): defines the interval in milliseconds.
|
|
54
|
+
- `unlimited` (boolean = false): unlimited requests (skip take).
|
|
55
|
+
- `skip_n_calls` (number): take will go to redis every `n` calls instead of going in every take.
|
|
56
|
+
|
|
57
|
+
Ping:
|
|
58
|
+
|
|
59
|
+
- `interval` (number): represents the time between two consecutive pings. Default: 3000.
|
|
60
|
+
- `maxFailedAttempts` (number): is the allowed number of failed pings before declaring the connection as dead. Default: 5.
|
|
61
|
+
- `reconnectIfFailed` (boolean): indicates whether we should try to reconnect is the connection is declared dead. Default: true.
|
|
62
|
+
|
|
63
|
+
You can also define your rates using `per_second`, `per_minute`, `per_hour`, `per_day`. So `per_second: 1` is equivalent to `per_interval: 1, interval: 1000`.
|
|
64
|
+
|
|
65
|
+
If you omit `size`, limitdb assumes that `size` is the value of `per_interval`. So `size: 10, per_second: 10` is the same than `per_second: 10`.
|
|
66
|
+
|
|
67
|
+
If you don't specify a filling rate with `per_interval` or any other `per_x`, the bucket is fixed and you have to manually reset it using `PUT`.
|
|
68
|
+
|
|
69
|
+
You can also define `overrides` inside your type definitions as follows:
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
buckets = {
|
|
73
|
+
ip: {
|
|
74
|
+
size: 10,
|
|
75
|
+
per_second: 5,
|
|
76
|
+
overrides: {
|
|
77
|
+
'127.0.0.1': {
|
|
78
|
+
size: 100,
|
|
79
|
+
per_second: 50
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
In this case the specific bucket for `127.0.0.1` of type `ip` will have a greater limit.
|
|
87
|
+
|
|
88
|
+
It is also possible to define overrides by regex:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
overrides: {
|
|
92
|
+
'local-ips': {
|
|
93
|
+
match: /192\.168\./
|
|
94
|
+
size: 100,
|
|
95
|
+
per_second: 50
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
It's possible to configure expiration of overrides:
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
overrides: {
|
|
104
|
+
'54.32.12.31': {
|
|
105
|
+
size: 100,
|
|
106
|
+
per_second: 50,
|
|
107
|
+
until: new Date(2016, 4, 1)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Breaking changes from `Limitdb`
|
|
113
|
+
|
|
114
|
+
* Elements will have a default TTL of a week unless specified otherwise.
|
|
115
|
+
|
|
116
|
+
## TAKE
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
limitd.take(type, key, [count], (err, result) => {
|
|
120
|
+
console.log(result);
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`limitd.take` takes the following arguments:
|
|
125
|
+
|
|
126
|
+
- `type`: the bucket type.
|
|
127
|
+
- `key`: the identifier of the bucket.
|
|
128
|
+
- `count`: the amount of tokens you need. This is optional and the default is 1.
|
|
129
|
+
- `configOverride`: caller-provided bucket configuration for this operation
|
|
130
|
+
|
|
131
|
+
The result object has:
|
|
132
|
+
|
|
133
|
+
- `conformant` (boolean): true if the requested amount is conformant to the limit.
|
|
134
|
+
- `remaining` (int): the amount of remaining tokens in the bucket.
|
|
135
|
+
- `reset` (int / unix timestamp): unix timestamp of the date when the bucket will be full again.
|
|
136
|
+
- `limit` (int): the size of the bucket.
|
|
137
|
+
|
|
138
|
+
## PUT
|
|
139
|
+
|
|
140
|
+
You can manually reset a fill a bucket using PUT:
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
limitd.put(type, key, [count], (err, result) => {
|
|
144
|
+
console.log(result);
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`limitd.put` takes the following arguments:
|
|
149
|
+
|
|
150
|
+
- `type`: the bucket type.
|
|
151
|
+
- `key`: the identifier of the bucket.
|
|
152
|
+
- `count`: the amount of tokens you want to put in the bucket. This is optional and the default is the size of the bucket.
|
|
153
|
+
- `configOverride`: caller-provided bucket configuration for this operation
|
|
154
|
+
|
|
155
|
+
## Overriding Configuration at Runtime
|
|
156
|
+
Since the method of storing overrides for buckets in memory does not scale to a large number, limitd-redis provides a way for callers to pass in configuration from an external data store. The shape of this `configOverride` parameter (available on `take`, `put`, `get`, and `wait`) is exactly the same as `Buckets` above ^.
|
|
157
|
+
|
|
158
|
+
An example configuration override call might look like this:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const configOverride = {
|
|
162
|
+
size: 45,
|
|
163
|
+
per_hour: 15
|
|
164
|
+
}
|
|
165
|
+
// take one
|
|
166
|
+
limitd.take(type, key, { configOverride }, (err, result) => {
|
|
167
|
+
console.log(result);
|
|
168
|
+
}
|
|
169
|
+
// take multiple
|
|
170
|
+
limitd.take(type, key, { count: 3, configOverride }, (err, result) => {
|
|
171
|
+
console.log(result);
|
|
172
|
+
}););
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Config overrides follow the same rules as Bucket configuration elements with respect to default size when not provided and ttl.
|
|
176
|
+
|
|
177
|
+
## Author
|
|
178
|
+
|
|
179
|
+
[Auth0](auth0.com)
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Based on the work of Jeremy Martin: https://github.com/jmar777/cb
|
|
2
|
+
// Added a second timeout(1) to force spinning again the event loop lap and verify if the initial operation has been successful
|
|
3
|
+
module.exports = function(callback) {
|
|
4
|
+
|
|
5
|
+
var cb = function() {
|
|
6
|
+
if (timedout || (once && count)) return;
|
|
7
|
+
count += 1;
|
|
8
|
+
tid && clearTimeout(tid);
|
|
9
|
+
|
|
10
|
+
var args = Array.prototype.slice.call(arguments);
|
|
11
|
+
process.nextTick(function() {
|
|
12
|
+
if (!errback) return callback.apply(this, args);
|
|
13
|
+
args[0] ? errback(args[0]) : callback.apply(this, args.slice(1));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
}, count = 0, once = false, timedout = false, errback, tid;
|
|
17
|
+
|
|
18
|
+
cb.timeout = function(ms) {
|
|
19
|
+
tid && clearTimeout(tid);
|
|
20
|
+
tid = setTimeout(function() {
|
|
21
|
+
// force another second timeout to verify if the operation has been successful
|
|
22
|
+
// No need to clear timeout since it has been triggered
|
|
23
|
+
tid = setTimeout(function() {
|
|
24
|
+
cb(new TimeoutError(ms));
|
|
25
|
+
timedout = true;
|
|
26
|
+
}, 1);
|
|
27
|
+
}, ms - 1);
|
|
28
|
+
return cb;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
cb.error = function(func) { errback = func; return cb; };
|
|
32
|
+
|
|
33
|
+
cb.once = function() { once = true; return cb; };
|
|
34
|
+
|
|
35
|
+
return cb;
|
|
36
|
+
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var TimeoutError = module.exports.TimeoutError = function TimeoutError(ms) {
|
|
40
|
+
this.message = 'Specified timeout of ' + ms + 'ms was reached';
|
|
41
|
+
Error.captureStackTrace(this, this.constructor);
|
|
42
|
+
};
|
|
43
|
+
TimeoutError.prototype = new Error;
|
|
44
|
+
TimeoutError.prototype.constructor = TimeoutError;
|
|
45
|
+
TimeoutError.prototype.name = 'TimeoutError';
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
const retry = require('retry');
|
|
3
|
+
const cbControl = require('./cb');
|
|
4
|
+
const LimitDBRedis = require('./db');
|
|
5
|
+
const disyuntor = require('disyuntor');
|
|
6
|
+
const validation = require('./validation');
|
|
7
|
+
const EventEmitter = require('events').EventEmitter;
|
|
8
|
+
|
|
9
|
+
const ValidationError = validation.LimitdRedisValidationError;
|
|
10
|
+
|
|
11
|
+
const circuitBreakerDefaults = {
|
|
12
|
+
timeout: '0.50s',
|
|
13
|
+
maxFailures: 50,
|
|
14
|
+
cooldown: '1s',
|
|
15
|
+
maxCooldown: '3s',
|
|
16
|
+
name: 'limitr',
|
|
17
|
+
trigger: (err) => {
|
|
18
|
+
return !(err instanceof ValidationError);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const retryDefaults = {
|
|
23
|
+
retries: 3,
|
|
24
|
+
minTimeout: 10,
|
|
25
|
+
maxTimeout: 30
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class LimitdRedis extends EventEmitter {
|
|
29
|
+
constructor(params) {
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
this.db = new LimitDBRedis(_.pick(params, [
|
|
33
|
+
'uri', 'nodes', 'buckets', 'prefix', 'slotsRefreshTimeout', 'slotsRefreshInterval',
|
|
34
|
+
'password', 'tls', 'dnsLookup', 'globalTTL', 'cacheSize', 'ping']));
|
|
35
|
+
|
|
36
|
+
this.db.on('error', (err) => {
|
|
37
|
+
this.emit('error', err);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this.db.on('ready', () => {
|
|
41
|
+
this.emit('ready');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
this.db.on('node error', (err, node) => {
|
|
45
|
+
this.emit('node error', err, node);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
this.breakerOpts = _.merge(circuitBreakerDefaults, params.circuitbreaker);
|
|
49
|
+
this.retryOpts = _.merge(retryDefaults, params.retry);
|
|
50
|
+
this.commandTimeout = params.commandTimeout || 75;
|
|
51
|
+
|
|
52
|
+
this.dispatch = disyuntor.wrapCallbackApi(this.breakerOpts, this.dispatch.bind(this));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static buildParams(type, key, opts, cb) {
|
|
56
|
+
const params = { type, key };
|
|
57
|
+
const optsType = typeof opts;
|
|
58
|
+
|
|
59
|
+
// handle lack of opts and/or cb
|
|
60
|
+
if (cb == null) {
|
|
61
|
+
if (optsType === 'function') {
|
|
62
|
+
cb = opts;
|
|
63
|
+
opts = undefined;
|
|
64
|
+
} else {
|
|
65
|
+
cb = _.noop;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (optsType === 'number' || opts === 'all') {
|
|
70
|
+
params.count = opts;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (optsType === 'object') {
|
|
74
|
+
_.assign(params, opts);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return [params, cb];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
handler(method, type, key, opts, cb) {
|
|
81
|
+
let [params, callback] = LimitdRedis.buildParams(type, key, opts, cb);
|
|
82
|
+
this.dispatch(method, params, callback);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
dispatch(method, params, cb) {
|
|
86
|
+
const operation = retry.operation(this.retryOpts);
|
|
87
|
+
operation.attempt((attempts) => {
|
|
88
|
+
this.db[method](params, cbControl((err, results) => {
|
|
89
|
+
if (err instanceof ValidationError) {
|
|
90
|
+
return cb(err, null, { attempts });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (operation.retry(err)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return cb(err ? operation.mainError() : null, results, { attempts });
|
|
98
|
+
}).timeout(this.commandTimeout));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
take(type, key, opts, cb) {
|
|
103
|
+
this.handler('take', type, key, opts, cb);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
wait(type, key, opts, cb) {
|
|
107
|
+
this.handler('wait', type, key, opts, cb);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get(type, key, opts, cb) {
|
|
111
|
+
this.handler('get', type, key, opts, cb);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
put(type, key, opts, cb) {
|
|
115
|
+
this.handler('put', type, key, opts, cb);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
reset(type, key, opts, cb) {
|
|
119
|
+
this.put(type, key, opts, cb);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
resetAll(cb) {
|
|
123
|
+
this.db.resetAll(cb);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
close(callback) {
|
|
127
|
+
this.db.close((err) => {
|
|
128
|
+
this.db.removeAllListeners();
|
|
129
|
+
callback(err);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = LimitdRedis;
|
|
135
|
+
module.exports.ValidationError = ValidationError;
|