Version: Deno 1.44.4
I have written a basic rate limiter implementation. It uses setTimeout to delay between the request. Here’s the code implementation. The issue is whenever there are more than 2 requests, the new request waits for setTimeout to finish. I have tried everything but don’t understand what is causing this. It looks like Deno is queuing up request.
const headers = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*" }
const REQUEST_LIMITER = { timerInstance: 0, requests: new Map() };
Deno.serve({ port: 9000 }, requestHandler);
async function requestHandler(req: Request) {
console.log("----- New Request recieved -----");
if (req.url.endsWith("test")) return await testController();
return Response.json({ status: "error", message: "Invalid API endpoint" }, { status: 404, headers });
}
async function testController() {
const response = await new Promise((resolve) => {
function callback(data: string) {
resolve(data);
}
REQUEST_LIMITER.requests.set(crypto.randomUUID(), { callback });
callLimiter();
});
return Response.json({ status: "success", data: response }, { status: 200, headers });
}
function callLimiter() {
if (!REQUEST_LIMITER.timerInstance) {
limiter();
}
function limiter() {
if (!REQUEST_LIMITER.requests.size) return REQUEST_LIMITER.timerInstance = 0;
for (const [deduplicationId, data] of REQUEST_LIMITER.requests.entries()) {
REQUEST_LIMITER.requests.delete(deduplicationId);
data.callback("Resolved");
}
REQUEST_LIMITER.timerInstance = setTimeout(limiter, 5000);
}
}
Initially I assumed the server was doing other complex tasks parallelly so it failed to process new request. But then I extracted out in a separate file.
Then I assumed the front end framework was not firing the request correctly, so I prepared a small html page utility to fire request on click of button.
The code below is an html for index.html file. Try hitting the request button multiple times, you’ll observe the requests are taking longer and longer, as if all the request are waiting for their setTimeout to complete, but the logic does the opposite.
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<button onclick="makeAPICall()">Make Request</button>
<p id="result"></p>
<script>
async function makeAPICall() {
const response = fetch("http://localhost:9000/test");
if (response.status === 200) {
const responseData = await response.json();
document.getElementById("result").innerHTML = responseData.data;
}
}
</script>
</body>
</html>
Any help is appreciated.
supxd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.