-
-
Notifications
You must be signed in to change notification settings - Fork 76
Added exponential backoff #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gurgunday
merged 10 commits into
fastify:master
from
aniketcodes:feature-exponential_backoff
Nov 15, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
412f8a2
Added exponential backoff
aniketcodes f4a2718
Updated readme
aniketcodes f5d3ae5
Added checks for max safe integer
aniketcodes cb5940a
Update store/LocalStore.js
aniketcodes 4c57264
Update store/RedisStore.js
aniketcodes 4db21e3
Restored original order
aniketcodes 8ffc638
Made ttl reset consistent
aniketcodes 9d84a4c
Update README.md
aniketcodes 0ac9a5d
Update store/RedisStore.js
aniketcodes 1bb3034
Added type test and fixed max int lua
aniketcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| 'use strict' | ||
| const { test } = require('node:test') | ||
| const assert = require('assert') | ||
| const Fastify = require('fastify') | ||
| const rateLimit = require('../index') | ||
|
|
||
| const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
|
||
| test('Exponential Backoff', async (t) => { | ||
| const fastify = Fastify() | ||
|
|
||
| // Register rate limit plugin with exponentialBackoff set to true in routeConfig | ||
| await fastify.register(rateLimit, { max: 2, timeWindow: 500 }) | ||
|
|
||
| fastify.get( | ||
| '/expoential-backoff', | ||
| { | ||
| config: { | ||
| rateLimit: { | ||
| max: 2, | ||
| timeWindow: 500, | ||
| exponentialBackoff: true | ||
| } | ||
| } | ||
| }, | ||
| async (req, reply) => 'exponential backoff applied!' | ||
| ) | ||
|
|
||
| // Test | ||
| const res = await fastify.inject({ url: '/expoential-backoff', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-remaining'], '1') | ||
|
|
||
| const res2 = await fastify.inject({ url: '/expoential-backoff', method: 'GET' }) | ||
| assert.deepStrictEqual(res2.statusCode, 200) | ||
| assert.deepStrictEqual(res2.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res2.headers['x-ratelimit-remaining'], '0') | ||
|
|
||
| const res3 = await fastify.inject({ url: '/expoential-backoff', method: 'GET' }) | ||
| assert.deepStrictEqual(res3.statusCode, 429) | ||
| assert.deepStrictEqual(res3.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res3.headers['x-ratelimit-remaining'], '0') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 500 ms' | ||
| }, | ||
| JSON.parse(res3.payload) | ||
| ) | ||
|
|
||
| const res4 = await fastify.inject({ url: '/expoential-backoff', method: 'GET' }) | ||
| assert.deepStrictEqual(res4.statusCode, 429) | ||
| assert.deepStrictEqual(res4.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res4.headers['x-ratelimit-remaining'], '0') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 1 second' | ||
| }, | ||
| JSON.parse(res4.payload) | ||
| ) | ||
|
|
||
| // Wait for the window to reset | ||
| await sleep(1000) | ||
| const res5 = await fastify.inject({ url: '/expoential-backoff', method: 'GET' }) | ||
| assert.deepStrictEqual(res5.statusCode, 200) | ||
| assert.deepStrictEqual(res5.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res5.headers['x-ratelimit-remaining'], '1') | ||
| }) | ||
|
|
||
| test('Global Exponential Backoff', async (t) => { | ||
| const fastify = Fastify() | ||
|
|
||
| // Register rate limit plugin with exponentialBackoff set to true in routeConfig | ||
| await fastify.register(rateLimit, { max: 2, timeWindow: 500, exponentialBackoff: true }) | ||
|
|
||
| fastify.get( | ||
| '/expoential-backoff-global', | ||
| { | ||
| config: { | ||
| rateLimit: { | ||
| max: 2, | ||
| timeWindow: 500 | ||
| } | ||
| } | ||
| }, | ||
| async (req, reply) => 'exponential backoff applied!' | ||
| ) | ||
|
|
||
| // Test | ||
| let res | ||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-remaining'], '1') | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-remaining'], '0') | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 500 ms' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 1 second' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 2 seconds' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 4 seconds' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
| }) | ||
|
|
||
| test('MAx safe Exponential Backoff', async (t) => { | ||
| const fastify = Fastify() | ||
|
|
||
| // Register rate limit plugin with exponentialBackoff set to true in routeConfig | ||
| await fastify.register(rateLimit, { max: 2, timeWindow: 500, exponentialBackoff: true }) | ||
|
|
||
| fastify.get( | ||
| '/expoential-backoff-global', | ||
| { | ||
| config: { | ||
| rateLimit: { | ||
| max: 2, | ||
| timeWindow: '285421 years' | ||
| } | ||
| } | ||
| }, | ||
| async (req, reply) => 'exponential backoff applied!' | ||
| ) | ||
|
|
||
| // Test | ||
| let res | ||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-remaining'], '1') | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 200) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-remaining'], '0') | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 285421 years' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 285421 years' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 285421 years' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
|
|
||
| res = await fastify.inject({ url: '/expoential-backoff-global', method: 'GET' }) | ||
| assert.deepStrictEqual(res.statusCode, 429) | ||
| assert.deepStrictEqual(res.headers['x-ratelimit-limit'], '2') | ||
| assert.deepStrictEqual( | ||
| { | ||
| statusCode: 429, | ||
| error: 'Too Many Requests', | ||
| message: 'Rate limit exceeded, retry in 285421 years' | ||
| }, | ||
| JSON.parse(res.payload) | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.