mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
fix(Discord Node): Fix broken rate limit handling (#3311)
* 🔨 fix and additional option to return response headers * ⚡ Remove "return response headers" parameter Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
parent
d71967878f
commit
b687ba11cc
|
@ -117,8 +117,6 @@ export class Discord implements INodeType {
|
|||
],
|
||||
};
|
||||
|
||||
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
|
@ -204,6 +202,7 @@ export class Discord implements INodeType {
|
|||
|
||||
if(!body.payload_json){
|
||||
requestOptions = {
|
||||
resolveWithFullResponse: true,
|
||||
method: 'POST',
|
||||
body,
|
||||
uri: webhookUri,
|
||||
|
@ -214,6 +213,7 @@ export class Discord implements INodeType {
|
|||
};
|
||||
}else {
|
||||
requestOptions = {
|
||||
resolveWithFullResponse: true,
|
||||
method: 'POST',
|
||||
body,
|
||||
uri: webhookUri,
|
||||
|
@ -223,34 +223,50 @@ export class Discord implements INodeType {
|
|||
};
|
||||
}
|
||||
let maxTries = 5;
|
||||
let response;
|
||||
|
||||
do {
|
||||
try {
|
||||
await this.helpers.request(requestOptions);
|
||||
response = await this.helpers.request(requestOptions);
|
||||
const resetAfter = response.headers['x-ratelimit-reset-after'] * 1000;
|
||||
const remainingRatelimit = response.headers['x-ratelimit-remaining'];
|
||||
|
||||
// remaining requests 0
|
||||
// https://discord.com/developers/docs/topics/rate-limits
|
||||
if (!+remainingRatelimit) {
|
||||
await new Promise<void>((resolve) =>
|
||||
setTimeout(resolve, resetAfter || 1000),
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
} catch (error) {
|
||||
// HTTP/1.1 429 TOO MANY REQUESTS
|
||||
// Await when the current rate limit will reset
|
||||
// https://discord.com/developers/docs/topics/rate-limits
|
||||
if (error.statusCode === 429) {
|
||||
//* Await ratelimit to be over
|
||||
const retryAfter = error.response?.headers['retry-after'] || 1000;
|
||||
|
||||
await new Promise<void>((resolve) =>
|
||||
setTimeout(resolve, error.response.body.retry_after || 150),
|
||||
setTimeout(resolve, +retryAfter),
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
//* Different Discord error, throw it
|
||||
throw error;
|
||||
}
|
||||
} while (--maxTries);
|
||||
|
||||
if (maxTries <= 0) {
|
||||
throw new Error(
|
||||
'Could not send Webhook message. Max. amount of rate-limit retries reached.',
|
||||
'Could not send Webhook message. Max amount of rate-limit retries reached.',
|
||||
);
|
||||
}
|
||||
|
||||
returnData.push({ success: true });
|
||||
}
|
||||
|
||||
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue