Skip to content

Commit

Permalink
Made ttl reset consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketcodes committed Nov 5, 2024
1 parent 4db21e3 commit 8ffc638
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ await fastify.register(import('@fastify/rate-limit'), {
- `allowList`: array of string of ips to exclude from rate limiting. It can be a sync or async function with the signature `(request, key) => {}` where `request` is the Fastify request object and `key` is the value generated by the `keyGenerator`. If the function return a truthy value, the request will be excluded from the rate limit.
- `redis`: by default, this plugin uses an in-memory store, but if an application runs on multiple servers, an external store will be needed. This plugin requires the use of [`ioredis`](https://github.com/redis/ioredis).<br> **Note:** the [default settings](https://github.com/redis/ioredis/blob/v4.16.0/API.md#new_Redis_new) of an ioredis instance are not optimal for rate limiting. We recommend customizing the `connectTimeout` and `maxRetriesPerRequest` parameters as shown in the [`example`](https://github.com/fastify/fastify-rate-limit/tree/master/example/example.js).
- `nameSpace`: choose which prefix to use in the redis, default is 'fastify-rate-limit-'
- `continueExceeding`: Renew user limitation when user sends a request to the server when still limited
- `continueExceeding`: Renew user limitation when user sends a request to the server when still limited. This will take priority over exponentialBackoff
- `store`: a custom store to track requests and rates which allows you to use your own storage mechanism (using an RDBMS, MongoDB, etc.) as well as further customizing the logic used in calculating the rate limits. A simple example is provided below as well as a more detailed example using Knex.js can be found in the [`example/`](https://github.com/fastify/fastify-rate-limit/tree/master/example) folder
- `skipOnError`: if `true` it will skip errors generated by the storage (e.g. redis not reachable).
- `keyGenerator`: a sync or async function to generate a unique identifier for each incoming request. Defaults to `(request) => request.ip`, the IP is resolved by fastify using `request.connection.remoteAddress` or `request.headers['x-forwarded-for']` if [trustProxy](https://fastify.dev/docs/latest/Reference/Server/#trustproxy) option is enabled. Use it if you want to override this behavior
Expand All @@ -160,7 +160,7 @@ await fastify.register(import('@fastify/rate-limit'), {
- `onExceeding`: callback that will be executed before request limit has been reached.
- `onExceeded`: callback that will be executed after request limit has been reached.
- `onBanReach`: callback that will be executed when the ban limit has been reached.
- `exponentialBackoff`: Renew user limitation exponentially when user sends a request to the server when still limited
- `exponentialBackoff`: Renew user limitation exponentially when user sends a request to the server when still limited.

`keyGenerator` example usage:
```js
Expand Down
3 changes: 1 addition & 2 deletions store/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ const lua = `
if ttl == -1 or (continueExceeding and current > max) then
redis.call('PEXPIRE', key, timeWindow)
ttl = timeWindow
end
-- If the key is new or if its incremented value has exceeded the max value and exponential backoff is enabled then set its TTL
if ttl == -1 or (exponentialBackoff and current > max) then
elseif ttl == -1 or (exponentialBackoff and current > max) then
local backoffExponent = current - max - 1
ttl = math.min(timeWindow * (2 ^ backoffExponent), MAX_SAFE_INTEGER)
redis.call('PEXPIRE', key, ttl)
Expand Down

0 comments on commit 8ffc638

Please sign in to comment.