I need to write a function that fetches the rate limit test page 500 times with a 200 status code as fast as possible. Cloudflare will rate limit you if you make more than 10 requests within a minute to https://www.cloudflare.com/rate-limit-test.
This is my code so far:
const getRateLimited = () => {
const promises = [];
for (let i = 0; i < 500; i++) {
promises.push(fetch('https://www.cloudflare.com/rate-limit-test'));
}
return Promise.all(promises)
.then(promise => res.send(promise[0].status))
};
Is there a better way of doing this and using setTimeOut
?
10 requests per minute is 1 every 6 seconds
So, just wait between requests
Here is code using async/await - no error checking, that's up to you
const getRateLimited = async () => {
const responses = [];
for (let i = 0; i < 500; i++) {
const response = await fetch('https://www.cloudflare.com/rate-limit-test');
responses.push(response);
await new Promise(r => setTimeout(r, 6000));
}
return res.send(responses[0].status); // why?
};
Why do you res.send
the first status though?
edit: thinking about it, you want as fast as possible,
First, make the request start every 6 seconds, and remove the last delay
const getRateLimited = async () => {
const responses = [];
for (let i = 0; i < 500; i++) {
const now = Date.now();
const response = await fetch('https://www.cloudflare.com/rate-limit-test');
responses.push(response);
if (i < 499) {
const after = Date.now();
const delay = 6000 - (after - now);
await new Promise(r => setTimeout(r, delay));
}
}
return res.send(responses[0].status);
};
This should make 500 requests over 2994 seconds - which is as fast as you can go I believe
edit: of course, if you can make 10 requests, then wait a minute, then make 10 requests, then wait a minute ... rinse and repeat ... you can do it in 2940 + some seconds for the last 10 requests - that would knock another 50-54 seconds off the time it takes
const getRateLimited = async () => {
const responses = [];
for (let i = 0; i < 500; i++) {
const response = await fetch('https://www.cloudflare.com/rate-limit-test/');
responses.push(response);
if (i < 499 && i % 10 === 9) {
await new Promise(r => setTimeout(r, 60000));
}
}
return res.send(responses[0].status);
};