Clean up rate limiter

This commit is contained in:
Christopher Cookman 2024-12-23 16:42:50 -07:00
parent 55ee84d2f3
commit 9520086bfe
2 changed files with 4 additions and 2 deletions

View file

@ -1,5 +1,7 @@
// This script is included to help us test the rate limiter.
const apiUrl = 'https://ubs.rtech.foundation/api/v1/info'; // Replace with your API URL const apiUrl = 'https://ubs.rtech.foundation/api/v1/info'; // Replace with your API URL
const interval = 1000; // 1 second const interval = 500; // 1 second
const callApi = async () => { const callApi = async () => {
try { try {

View file

@ -17,7 +17,7 @@ const middleware = (req, res, next) => {
if (!global.rateLimitList[requestIp]) { if (!global.rateLimitList[requestIp]) {
global.rateLimitList[requestIp] = { requests: 0, lastRequest: Date.now() }; global.rateLimitList[requestIp] = { requests: 0, lastRequest: Date.now() };
} }
console.log(`IP made ${global.rateLimitList[requestIp].requests}/${maxRequests} requests in ${(Date.now() - global.rateLimitList[requestIp].lastRequest) / 1000}/${timeWindow} seconds.`); // console.log(`IP made ${global.rateLimitList[requestIp].requests}/${maxRequests} requests in ${(Date.now() - global.rateLimitList[requestIp].lastRequest) / 1000}/${timeWindow} seconds.`);
if (global.rateLimitList[requestIp].lastRequest + timeWindow * 1000 < Date.now()) { if (global.rateLimitList[requestIp].lastRequest + timeWindow * 1000 < Date.now()) {
global.rateLimitList[requestIp] = { requests: 0, lastRequest: Date.now() }; global.rateLimitList[requestIp] = { requests: 0, lastRequest: Date.now() };
} else { } else {