fix: 429 handling bug

This commit is contained in:
Skillz4Killz
2022-09-20 03:52:19 +00:00
committed by GitHub
parent b0d7a7bf28
commit acabe468ae
2 changed files with 11 additions and 0 deletions

View File

@@ -66,6 +66,9 @@ export async function processGlobalQueue(rest: RestManager) {
bucketId: request.payload.bucketId,
reject: request.request.reject,
respond: request.request.respond,
retryRequest: function() {
rest.globalQueue.unshift(request);
},
retryCount: request.payload.retryCount ?? 0,
payload: rest.createRequestBody(rest, {
method: request.request.method,

View File

@@ -1,3 +1,4 @@
import { delay } from "../mod.ts";
import { HTTPResponseCodes } from "../types/shared.ts";
import { BASE_URL } from "../util/constants.ts";
import { RequestMethod } from "./rest.ts";
@@ -9,6 +10,7 @@ export interface RestSendRequestOptions {
bucketId?: string;
reject?: Function;
respond?: Function;
retryRequest?: Function;
retryCount?: number;
payload?: {
headers: Record<string, string>;
@@ -94,6 +96,7 @@ export async function sendRequest<T>(rest: RestManager, options: RestSendRequest
body,
});
} else {
// TOO MANY ATTEMPTS, GET RID OF REQUEST FROM QUEUE.
if (options.retryCount && options.retryCount++ >= rest.maxRetryCount) {
rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(options)}`);
// REMOVE ITEM FROM QUEUE TO PREVENT RETRY
@@ -105,6 +108,11 @@ export async function sendRequest<T>(rest: RestManager, options: RestSendRequest
// @ts-ignore Code should never reach here
return;
} // RATE LIMITED, ADD BACK TO QUEUE
else {
const json = await response.json();
await delay(json.retry_after * 1000);
return options.retryRequest?.();
}
}
}