Skip to content

Commit

Permalink
Add support for redis connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Doyle committed Nov 15, 2019
1 parent 66ef759 commit 2543b78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ $ npm install --save rate-limit-redis
## Usage

```js
var RateLimit = require('express-rate-limit');
var RedisStore = require('rate-limit-redis');
const RateLimit = require('express-rate-limit');
const RedisStore = require('rate-limit-redis');

var limiter = new RateLimit({
const limiter = new RateLimit({
store: new RedisStore({
// see Configuration
}),
Expand All @@ -32,15 +32,16 @@ app.use(limiter);
```

## Connect to UDP Socket
```
var RateLimit = require('express-rate-limit');
var RedisStore = require('rate-limit-redis');
var Redis = require('ioredis');
var client = new Redis('/tmp/redis.sock');

var limiter = new RateLimit({
```js
const RateLimit = require('express-rate-limit');
const RedisStore = require('rate-limit-redis');
const Redis = require('ioredis');
const client = new Redis('/tmp/redis.sock');

const limiter = new RateLimit({
store: new RedisStore({
client: client,
client: client
}),
max: 100, // limit each IP to 100 requests per windowMs
delayMs: 0 // disable delaying - full speed until the max limit is reached
Expand All @@ -49,10 +50,11 @@ var limiter = new RateLimit({

## Configuration

* **expiry**: seconds - how long each rate limiting window exists for. Defaults to `60`.
* **resetExpiryOnChange**: boolean - if the expiry time should be reset every time a key is incremented/decremented. This means that when the limit is reached and the user is given a 429 response, the rate limit window is extended. Defaults to `false`.
* **prefix**: string - prefix to add to entries in Redis. Defaults to `rl:`.
* **client**: [Redis Client](https://github.com/NodeRedis/node_redis) or [ioredis Client](https://github.com/luin/ioredis)- A Redis Client to use. Defaults to `require('redis').createClient();`.
- **expiry**: seconds - how long each rate limiting window exists for. Defaults to `60`.
- **resetExpiryOnChange**: boolean - if the expiry time should be reset every time a key is incremented/decremented. This means that when the limit is reached and the user is given a 429 response, the rate limit window is extended. Defaults to `false`.
- **prefix**: string - prefix to add to entries in Redis. Defaults to `rl:`.
- **client**: [Redis Client](https://github.com/NodeRedis/node_redis) or [ioredis Client](https://github.com/luin/ioredis)- A Redis Client to use. Defaults to `require('redis').createClient();`.
- **redisURL**: string - a Redis connection string to be used for the default client connection. Ignored when the `client` option is provided. [Redis Client connection string format and options](https://github.com/NodeRedis/node_redis#rediscreateclient).

## License

Expand Down
5 changes: 3 additions & 2 deletions lib/redis-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ var RedisStore = function(options) {
options = defaults(options, {
expiry: 60, // default expiry is one minute
prefix: "rl:",
resetExpiryOnChange: false
resetExpiryOnChange: false,
redisURL: undefined,
});

var expiryMs = Math.round(1000 * options.expiry);

// create the client if one isn't provided
options.client = options.client || redis.createClient();
options.client = options.client || redis.createClient(options.redisURL);

var setExpire = function(replies, rdskey) {
// if this is new or has no expiry
Expand Down

0 comments on commit 2543b78

Please sign in to comment.