diff --git a/docs/content/gettingstarted.md b/docs/content/gettingstarted.md index 6ee051f24..4892df5f2 100644 --- a/docs/content/gettingstarted.md +++ b/docs/content/gettingstarted.md @@ -90,7 +90,7 @@ Web-Mystery Tutorials: - Running a Discord bot written in Deno in Docker YouTube Tutorials: -- Coming soon to [NTM Development](https://www.youtube.com/channel/UCkOFck-WCQtolha4NJuK7zA/) +- [Discordeno Bot Tutorials YouTube series](https://youtu.be/rIph9-BGsuQ) --- diff --git a/src/module/requestManager.ts b/src/module/requestManager.ts index dbbdabd15..ee96700da 100644 --- a/src/module/requestManager.ts +++ b/src/module/requestManager.ts @@ -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();