Browse Source

Add ability to pass ignoreErrors to the configuration options.

When ignoreErrors is true, if an error is encountered connecting to redis the next() middleware is called.
develop
Ashley Streb 10 years ago
parent
commit
3c6f79aa7f
  1. 2
      README.md
  2. 1
      index.js
  3. 23
      tests/index.js

2
README.md

@ -46,6 +46,8 @@ limiter(options)
- `expire`: `Number` amount of time in `ms` before the rate-limited is reset
- `whitelist`: `function(req)` optional param allowing the ability to whitelist. return `boolean`, `true` to whitelist, `false` to passthru to limiter.
- `skipHeaders`: `Boolean` whether to skip sending HTTP headers for rate limits ()
- `ignoreErrors`: `Boolean` whether errors generated from redis should allow the middleware to call next(). Defaults to false.
### Examples
``` js

1
index.js

@ -12,6 +12,7 @@ module.exports = function (app, db) {
var key = 'ratelimit:' + opts.path + ':' + opts.method + ':' + lookups
db.get(key, function (err, limit) {
if (err && opts.ignoreErrors) return next()
var now = Date.now()
limit = limit ? JSON.parse(limit) : {
total: opts.total,

23
tests/index.js

@ -93,5 +93,28 @@ describe('rate-limiter', function () {
.expect(function(res) { if ('Retry-After' in res.headers) return 'Retry-After not to be set' })
.expect(429, done)
})
it('should process ignoreErrors', function (done) {
limiter({
path: '/route',
method: 'get',
lookup: ['connection.remoteAddress'],
total: 10,
expire: 1000 * 60 * 60,
ignoreErrors: true
})
app.get('/route', function (req, res) {
res.send(200, 'hello')
})
sinon.stub(redis, 'get', function(key, callback) {
callback({err: true})
})
request(app)
.get('/route')
.expect(200, done)
})
})
})

Loading…
Cancel
Save