Skip to content

Commit

Permalink
Refine rete limit headers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPereverzyev committed Jul 1, 2020
1 parent c488829 commit be054c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -186,9 +186,11 @@ Each middleware function has the following arguments:
- `axiosInstance` - Axios instance, only applicable to Axios middleware, not available for Koa or Express (see examples above)
- `key` - each counter is identified by key, thus the argument is __required__. It can be string or function, in case of later, context with `req` and `res` will be passed as the only argument to the function
- `options` - object representing optional configuration properties:
- `upper` or `limit` - sets the upper QPS limit (normally it's max allowed QPS), request above the limit will result in response with 429 status, in case of Axios, exception with `retryAfter` property will be thrown instead, default 100
- `lower` - sets the lower QPS limit, applicable when at least one of `avoidLatency`, `avoidErrors` or `avoidDisconnects` options are enabled, default - `upper` or `limit`
- `interval` - time string or number of seconds defining time frame to which the limits are applied, default 1 sec
- `upper` or `limit` - sets the upper rate limit (normally it's max allowed rate), request above the limit will result in response with 429 status, in case of Axios, exception with `retryAfter` property will be thrown instead, default 100
- `lower` - sets the lower rate limit, applicable when at least one of `avoidLatency`, `avoidErrors` or `avoidDisconnects` options are enabled, default - `upper` or `limit`
- `interval` - time string or number of seconds defining time frame to which the limits are applied, default 1 sec
- `rewardAmt` - fraction added to the current rate limit each time counter is rewarded for performance, default is 0.1, current rate limit can not be bigger than `upper` limit
- `penaltyAmt` - fraction substracted from the current rate limit each time counter is penalized for performance, default is -0.2, current rate limit can not be smaller than `lower` limit
- `breaksAfter` - sets the number of failures to open the circuit and respond with 503 for `breakDuration` sec, in case of Axios, exception with `retryAfter` property will be thrown instead. The counter is reset each time successfull response is received
- `breakDuration` - time string or number of seconds for circuit to remain open, default 1 sec
- `avoidLatency` - number of milliseconds (fractions allowed), reduce upper rate limit when response takes longer than specified, not set by default (0)
Expand Down
8 changes: 4 additions & 4 deletions lib/middleware.js
Expand Up @@ -10,8 +10,8 @@ module.exports.koa = function (key, options) {
const counterKey = keyValue || key(ctx)
const counter = counters().start(counterKey, options).tick(now)
if (options.verbose) {
ctx.set('X-RateLimit-Limit', counter.upper)
ctx.set('X-RateLimit-Remaining', counter.upper > counter.count ? counter.upper - counter.count : 0)
ctx.set('X-RateLimit-Limit', counter.limit | 0)
ctx.set('X-RateLimit-Remaining', counter.limit > counter.count ? (counter.limit - counter.count) | 0 : 0)
ctx.set('X-RateLimit-Reset', counter.slice - now + 1)
}
if (counter.isOpen()) {
Expand Down Expand Up @@ -41,8 +41,8 @@ module.exports.express = function (key, options) {
const counterKey = keyValue || key(ctx)
const counter = counters().start(counterKey, options).tick(now)
if (options.verbose) {
res.set('X-RateLimit-Limit', counter.upper)
res.set('X-RateLimit-Remaining', counter.upper > counter.count ? counter.upper - counter.count : 0)
res.set('X-RateLimit-Limit', counter.limit | 0)
res.set('X-RateLimit-Remaining', counter.limit > counter.count ? (counter.limit - counter.count) | 0 : 0)
res.set('X-RateLimit-Reset', counter.slice - now + 1)
}
if (counter.isOpen()) {
Expand Down
6 changes: 6 additions & 0 deletions test/middleware.spec.js
Expand Up @@ -124,6 +124,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 1
test.counterStub.slice = 1e12

Expand All @@ -145,6 +146,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 11
test.counterStub.slice = 1e12
test.counterStub.isOver.returns(true)
Expand All @@ -167,6 +169,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 11
test.counterStub.slice = 1e12
test.counterStub.isOpen.returns(true)
Expand Down Expand Up @@ -332,6 +335,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 1
test.counterStub.slice = 1e12

Expand All @@ -353,6 +357,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 11
test.counterStub.slice = 1e12
test.counterStub.isOver.returns(true)
Expand All @@ -375,6 +380,7 @@ describe('middleware', () => {
const test = setupTest()
test.options.verbose = true
test.counterStub.upper = 10
test.counterStub.limit = 10
test.counterStub.count = 11
test.counterStub.slice = 1e12
test.counterStub.isOpen.returns(true)
Expand Down

0 comments on commit be054c7

Please sign in to comment.