Skip to content

Commit d4e26c0

Browse files
committed
use ioredis by default and add useRedisPackage option
1 parent c71a7cc commit d4e26c0

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ Specific:
193193
* [indexKeyPrefix](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#indexkeyprefix) Combined indexes of MongoDB.
194194
* [timeoutMs](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#timeoutms) For Cluster.
195195
* [rejectIfRedisNotReady](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#rejectifredisnotready)
196+
* [useRedisPackage](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#useredispackage)
197+
* [useRedis3AndLowerPackage](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#useredis3andlowerpackage)
196198

197199
## API
198200

lib/RateLimiterRedis.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
2828

2929
this._rejectIfRedisNotReady = !!opts.rejectIfRedisNotReady;
3030

31-
this.useIoredis = opts.useIoredis;
31+
this.useRedisPackage = opts.useRedisPackage;
3232
this.useRedis3AndLowerPackage = opts.useRedis3AndLowerPackage;
3333
if (typeof this.client.defineCommand === 'function') {
3434
this.client.defineCommand("rlflxIncr", {
@@ -87,7 +87,7 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
8787

8888
if (forceExpire) {
8989
if (secDuration > 0) {
90-
if(this.useIoredis){
90+
if(!this.useRedisPackage && !this.useRedis3AndLowerPackage){
9191
multi.set(rlKey, points, "EX", secDuration);
9292
}else{
9393
multi.set(rlKey, points, { EX: secDuration });
@@ -96,14 +96,14 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
9696
multi.set(rlKey, points);
9797
}
9898

99-
if(this.useIoredis){
99+
if(!this.useRedisPackage && !this.useRedis3AndLowerPackage){
100100
return multi.pttl(rlKey).exec(true);
101101
}
102102
return multi.pTTL(rlKey).exec(true);
103103
}
104104

105105
if (secDuration > 0) {
106-
if(this.useIoredis){
106+
if(!this.useRedisPackage && !this.useRedis3AndLowerPackage){
107107
return this.client.rlflxIncr(
108108
[rlKey].concat([String(points), String(secDuration)]));
109109
}
@@ -130,7 +130,7 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
130130
});
131131
}
132132
} else {
133-
if(this.useIoredis){
133+
if(!this.useRedisPackage && !this.useRedis3AndLowerPackage){
134134
return multi.incrby(rlKey, points).pttl(rlKey).exec(true);
135135
}
136136

@@ -142,7 +142,7 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
142142
if (!this._isRedisReady()) {
143143
throw new Error('Redis connection is not ready');
144144
}
145-
if(this.useIoredis){
145+
if(!this.useRedisPackage && !this.useRedis3AndLowerPackage){
146146
return this.client
147147
.multi()
148148
.get(rlKey)

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions {
249249

250250
interface IRateLimiterRedisOptions extends IRateLimiterStoreOptions {
251251
rejectIfRedisNotReady?: boolean;
252-
useIoredis?: boolean;
252+
useRedisPackage?: boolean;
253253
useRedis3AndLowerPackage?: boolean;
254254
}
255255

test/BurstyRateLimiter.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,12 @@ describe('BurstyRateLimiter', () => {
8686
await redisClientClosed.disconnect();
8787
const rlRedisClosed = new RateLimiterRedis({
8888
storeClient: redisClientClosed,
89-
useIoredis: true,
9089
});
9190
const blRedis = new RateLimiterRedis({
9291
storeClient: redisMockClient,
9392
keyPrefix: 'bursty',
9493
points: 1,
9594
duration: 1,
96-
useIoredis: true,
9795
});
9896
const bursty = new BurstyRateLimiter(rlRedisClosed, blRedis);
9997

@@ -124,12 +122,10 @@ describe('BurstyRateLimiter', () => {
124122
storeClient: redisMockClient,
125123
points: 1,
126124
duration: 1,
127-
useIoredis: true,
128125
});
129126
const blRedisClosed = new RateLimiterRedis({
130127
storeClient: redisClientClosed,
131128
keyPrefix: 'bursty',
132-
useIoredis: true,
133129
});
134130
const bursty = new BurstyRateLimiter(rlRedis, blRedisClosed);
135131
await bursty.consume(testKey);

test/RateLimiterRedis.ioredis.test.js

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
2626
storeClient: redisMockClient,
2727
points: 2,
2828
duration: 5,
29-
useIoredis: true,
3029
});
3130
rateLimiter
3231
.consume(testKey)
@@ -47,7 +46,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
4746
storeClient: redisMockClient,
4847
points: 1,
4948
duration: 5,
50-
useIoredis: true,
5149
});
5250
rateLimiter
5351
.consume(testKey, 2)
@@ -65,7 +63,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
6563
points: 2,
6664
duration: 5,
6765
execEvenly: true,
68-
useIoredis: true,
6966
});
7067
rateLimiter
7168
.consume(testKey)
@@ -102,7 +99,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
10299
duration: 1,
103100
execEvenly: true,
104101
execEvenlyMinDelayMs: 20,
105-
useIoredis: true,
106102
});
107103
rateLimiter
108104
.consume(testKey)
@@ -129,7 +125,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
129125
storeClient: redisMockClient,
130126
points: 3,
131127
duration: 5,
132-
useIoredis: true
133128
});
134129
rateLimiter
135130
.consume(testKey)
@@ -157,7 +152,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
157152
storeClient: redisMockClient,
158153
points: 1,
159154
duration: 5,
160-
useIoredis: true
161155
});
162156
rateLimiter
163157
.consume(testKey)
@@ -187,7 +181,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
187181
duration: 5,
188182
inMemoryBlockOnConsumed: 2,
189183
inMemoryBlockDuration: 10,
190-
useIoredis: true
191184
});
192185
rateLimiter
193186
.consume(testKey)
@@ -213,7 +206,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
213206
points: 1,
214207
duration: 5,
215208
inMemoryBlockOnConsumed: 1,
216-
useIoredis: true
217209
});
218210
rateLimiter
219211
.consume(testKey)
@@ -233,7 +225,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
233225
points: 1,
234226
duration: 5,
235227
inMemoryBlockOnConsumed: 1,
236-
useIoredis: true
237228
});
238229
rateLimiter
239230
.consume(testKey, 2)
@@ -254,7 +245,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
254245
duration: 1,
255246
inMemoryBlockOnConsumed: 2,
256247
inMemoryBlockDuration: 2,
257-
useIoredis: true
258248
});
259249
// It blocks on the first consume as consumed points more than available
260250
rateLimiter
@@ -281,7 +271,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
281271
const rateLimiter = new RateLimiterRedis({
282272
storeClient: redisMockClient,
283273
inMemoryBlockDuration: 2,
284-
useIoredis: true
285274
});
286275
rateLimiter.reward('test');
287276
} catch (err) {
@@ -309,7 +298,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
309298

310299
const rateLimiter = new RateLimiterRedis({
311300
storeClient: redisMockClient,
312-
useIoredis: true
313301
});
314302

315303
await redisMockClient.disconnect();
@@ -335,9 +323,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
335323
points: 2,
336324
duration: 2,
337325
storeClient: redisMockClient,
338-
useIoredis: true
339326
}),
340-
useIoredis: true
341327
});
342328
await redisClientClosed.disconnect();
343329

@@ -362,9 +348,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
362348
points: 2,
363349
duration: 2,
364350
storeClient: redisMockClient,
365-
useIoredis: true
366351
}),
367-
useIoredis: true
368352
});
369353
await redisClientClosed.disconnect();
370354

@@ -388,9 +372,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
388372
points: 2,
389373
duration: 2,
390374
storeClient: redisMockClient,
391-
useIoredis: true
392375
}),
393-
useIoredis: true
394376
});
395377
await redisClientClosed.disconnect();
396378

@@ -420,9 +402,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
420402
points: 1,
421403
duration: 1,
422404
storeClient: redisMockClient,
423-
useIoredis: true
424405
}),
425-
useIoredis: true
426406
});
427407
await redisClientClosed.disconnect();
428408

@@ -436,7 +416,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
436416
it('use keyPrefix from options', () => {
437417
const testKey = 'key';
438418
const keyPrefix = 'test';
439-
const rateLimiter = new RateLimiterRedis({ keyPrefix, storeClient: redisMockClient, useIoredis: true });
419+
const rateLimiter = new RateLimiterRedis({ keyPrefix, storeClient: redisMockClient });
440420

441421
expect(rateLimiter.getKey(testKey)).to.equal('test:key');
442422
});
@@ -448,7 +428,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
448428
points: 1,
449429
duration: 1,
450430
blockDuration: 2,
451-
useIoredis: true
452431
});
453432
rateLimiter
454433
.consume(testKey, 2)
@@ -468,7 +447,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
468447
points: 1,
469448
duration: 1,
470449
blockDuration: 2,
471-
useIoredis: true
472450
});
473451
sinon.stub(rateLimiter, '_block').callsFake(() => Promise.reject(new Error()));
474452
rateLimiter
@@ -489,7 +467,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
489467
points: 1,
490468
duration: 1,
491469
blockDuration: 2,
492-
useIoredis: true
493470
});
494471
rateLimiter
495472
.consume(testKey, 2)
@@ -517,7 +494,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
517494
storeClient: redisMockClient,
518495
points: 1,
519496
duration: 1,
520-
useIoredis: true
521497
});
522498
rateLimiter.block(testKey, 2).then(() => {
523499
rateLimiter
@@ -539,7 +515,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
539515
storeClient: redisMockClient,
540516
points: 2,
541517
duration: 1,
542-
useIoredis: true
543518
});
544519
rateLimiter
545520
.consume(testKey)
@@ -567,7 +542,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
567542
storeClient: redisMockClient,
568543
points: 2,
569544
duration: 1,
570-
useIoredis: true
571545
});
572546
await redisMockClient.disconnect()
573547
await rateLimiter
@@ -628,7 +602,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
628602
storeClient: redisMockClient,
629603
points: 2,
630604
duration: 1,
631-
useIoredis: true
632605
});
633606
rateLimiter
634607
.get(testKey)
@@ -648,7 +621,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
648621
storeClient: redisMockClient,
649622
points: 2,
650623
duration: 1,
651-
useIoredis: true
652624
});
653625
rateLimiter
654626
.consume(testKey)
@@ -668,7 +640,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
668640
storeClient: redisMockClient,
669641
points: 2,
670642
duration: 1,
671-
useIoredis: true
672643
});
673644
rateLimiter.delete(testKey)
674645
.then((resDel) => {
@@ -684,7 +655,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
684655
storeClient: redisMockClient,
685656
points: 2,
686657
duration: 1,
687-
useIoredis: true
688658
});
689659
await redisMockClient.disconnect();
690660
await rateLimiter.delete(testKey)
@@ -698,7 +668,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
698668
storeClient: redisMockClient,
699669
points: 2,
700670
duration: 5,
701-
useIoredis: true
702671
});
703672
rateLimiter
704673
.consume(testKey, 1, { customDuration: 1 })
@@ -717,7 +686,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
717686
storeClient: redisMockClient,
718687
points: 2,
719688
duration: 5,
720-
useIoredis: true
721689
});
722690
rateLimiter
723691
.consume(testKey, 1, { customDuration: 1 })
@@ -743,9 +711,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
743711
points: 2,
744712
duration: 3,
745713
storeClient: redisMockClient,
746-
useIoredis: true
747714
}),
748-
useIoredis: true
749715
});
750716
await redisClientClosed.disconnect();
751717

@@ -766,7 +732,6 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
766732
blockDuration: 10,
767733
inMemoryBlockOnConsumed: 2,
768734
inMemoryBlockDuration: 10,
769-
useIoredis: true
770735
});
771736
rateLimiter
772737
.consume(testKey)
@@ -791,7 +756,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
791756

792757
it('does not expire key if duration set to 0', (done) => {
793758
const testKey = 'neverexpire';
794-
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 2, duration: 0, useIoredis: true });
759+
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 2, duration: 0 });
795760
rateLimiter.consume(testKey, 1)
796761
.then(() => {
797762
rateLimiter.consume(testKey, 1)
@@ -814,7 +779,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
814779

815780
it('block key forever, if secDuration is 0', (done) => {
816781
const testKey = 'neverexpire';
817-
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1, useIoredis: true });
782+
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1 });
818783
rateLimiter.block(testKey, 0)
819784
.then(() => {
820785
setTimeout(() => {
@@ -833,7 +798,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
833798

834799
it('set points by key', (done) => {
835800
const testKey = 'set';
836-
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1, useIoredis: true });
801+
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1 });
837802
rateLimiter.set(testKey, 12)
838803
.then(() => {
839804
rateLimiter.get(testKey)
@@ -849,7 +814,7 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
849814

850815
it('set points by key forever', (done) => {
851816
const testKey = 'setforever';
852-
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1, useIoredis: true });
817+
const rateLimiter = new RateLimiterRedis({ storeClient: redisMockClient, points: 1, duration: 1 });
853818
rateLimiter.set(testKey, 12, 0)
854819
.then(() => {
855820
setTimeout(() => {

0 commit comments

Comments
 (0)