Package Exports
- ioredis-ratelimit
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ioredis-ratelimit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ioredis-ratelimit
Generic ratelimit tool on top of ioredis.
Install
$ npm i ioredis-ratelimit --saveUsage
var ratelimit = require('ioredis-ratelimit')(opts);
ratelimit(id).then(...).catch(...);opts:
- client: {Object} client of ioredis.
- limit: {Number} concurrent in duration milliscond, default
1. - duration: {Number} duration in milliscond, default
1000. - ttl: {Number} expire in milliscond, must greater than or equal to
.duration, default86400000. - key: {String|Function} ratelimiter's key.
Examples
simple:
'use strict';
var Redis = require('ioredis');
var ratelimit = require('ioredis-ratelimit')({
client: new Redis(),
key: 'limiter',
limit: 3,
duration: 1000,
ttl: 86400000 // one day
});
ratelimit().then(console.log).catch(console.error);// { total: 3, remaining: 2 }
ratelimit().then(console.log).catch(console.error);// { total: 3, remaining: 1 }
ratelimit().then(console.log).catch(console.error);// { total: 3, remaining: 0 }
ratelimit().then(console.log).catch(console.error);// [Error: Exceeded the limit]Express:
'use strict';
var app = require('express')();
var Redis = require('ioredis');
var ratelimit = require('ioredis-ratelimit')({
client: new Redis(),
key: function (req) {
return 'limiter:' + req.user._id;
},
limit: 1,
duration: 1000,
ttl: 86400000 // one day
});
...
app.use(function (req, res, next) {
ratelimit(req)
.then(function () {
next();
})
.catch(next);
});
app.get('/', function () {});
...Test
$ npm testLicense
MIT