This commit is contained in:
Skillz
2020-11-12 10:38:39 -05:00
2 changed files with 48 additions and 44 deletions

View File

@@ -90,7 +90,7 @@ Web-Mystery Tutorials:
- <a href="https://web-mystery.com/articles/running-discord-bot-written-deno-docker" target="_blank">Running a Discord bot written in Deno in Docker</a>
YouTube Tutorials:
- Coming soon to [NTM Development](https://www.youtube.com/channel/UCkOFck-WCQtolha4NJuK7zA/)
- [Discordeno Bot Tutorials YouTube series](https://youtu.be/rIph9-BGsuQ)
---

View File

@@ -64,56 +64,60 @@ async function cleanupQueues() {
}
async function processQueue() {
if (
(Object.keys(pathQueues).length) && !globallyRateLimited
) {
await Promise.allSettled(
Object.values(pathQueues).map(async (pathQueue) => {
const request = pathQueue.shift();
if (!request) return;
// Putting this code inside a function like this allows us to use tail recursion like a while loop without hitting the max stack error.
async function avoidMaxStackError() {
if (
(Object.keys(pathQueues).length) && !globallyRateLimited
) {
await Promise.allSettled(
Object.values(pathQueues).map(async (pathQueue) => {
const request = pathQueue.shift();
if (!request) return;
const rateLimitedURLResetIn = await checkRatelimits(request.url);
const rateLimitedURLResetIn = await checkRatelimits(request.url);
if (request.bucketID) {
const rateLimitResetIn = await checkRatelimits(request.bucketID);
if (rateLimitResetIn) {
// This request is still rate limited readd to queue
addToQueue(request);
} else if (rateLimitedURLResetIn) {
// This URL is rate limited readd to queue
addToQueue(request);
if (request.bucketID) {
const rateLimitResetIn = await checkRatelimits(request.bucketID);
if (rateLimitResetIn) {
// This request is still rate limited readd to queue
addToQueue(request);
} else if (rateLimitedURLResetIn) {
// This URL is rate limited readd to queue
addToQueue(request);
} else {
// This request is not rate limited so it should be run
const result = await request.callback();
if (result && result.rateLimited) {
addToQueue(
{ ...request, bucketID: result.bucketID || request.bucketID },
);
}
}
} else {
// This request is not rate limited so it should be run
const result = await request.callback();
if (result && result.rateLimited) {
addToQueue(
{ ...request, bucketID: result.bucketID || request.bucketID },
);
if (rateLimitedURLResetIn) {
// This URL is rate limited readd to queue
addToQueue(request);
} else {
// This request has no bucket id so it should be processed
const result = await request.callback();
if (request && result && result.rateLimited) {
addToQueue(
{ ...request, bucketID: result.bucketID || request.bucketID },
);
}
}
}
} else {
if (rateLimitedURLResetIn) {
// This URL is rate limited readd to queue
addToQueue(request);
} else {
// This request has no bucket id so it should be processed
const result = await request.callback();
if (request && result && result.rateLimited) {
addToQueue(
{ ...request, bucketID: result.bucketID || request.bucketID },
);
}
}
}
}),
);
}),
);
}
if (Object.keys(pathQueues).length) {
avoidMaxStackError();
cleanupQueues();
} else queueInProcess = false;
}
if (Object.keys(pathQueues).length) {
await delay(1000);
processQueue();
cleanupQueues();
} else queueInProcess = false;
return avoidMaxStackError();
}
processRateLimitedPaths();