mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -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[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
|
@ -204,6 +202,7 @@ export class Discord implements INodeType {
|
||||||
|
|
||||||
if(!body.payload_json){
|
if(!body.payload_json){
|
||||||
requestOptions = {
|
requestOptions = {
|
||||||
|
resolveWithFullResponse: true,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body,
|
body,
|
||||||
uri: webhookUri,
|
uri: webhookUri,
|
||||||
|
@ -214,6 +213,7 @@ export class Discord implements INodeType {
|
||||||
};
|
};
|
||||||
}else {
|
}else {
|
||||||
requestOptions = {
|
requestOptions = {
|
||||||
|
resolveWithFullResponse: true,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body,
|
body,
|
||||||
uri: webhookUri,
|
uri: webhookUri,
|
||||||
|
@ -223,34 +223,50 @@ export class Discord implements INodeType {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let maxTries = 5;
|
let maxTries = 5;
|
||||||
|
let response;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try {
|
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;
|
break;
|
||||||
} catch (error) {
|
} 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) {
|
if (error.statusCode === 429) {
|
||||||
//* Await ratelimit to be over
|
const retryAfter = error.response?.headers['retry-after'] || 1000;
|
||||||
|
|
||||||
await new Promise<void>((resolve) =>
|
await new Promise<void>((resolve) =>
|
||||||
setTimeout(resolve, error.response.body.retry_after || 150),
|
setTimeout(resolve, +retryAfter),
|
||||||
);
|
);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//* Different Discord error, throw it
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
} while (--maxTries);
|
} while (--maxTries);
|
||||||
|
|
||||||
if (maxTries <= 0) {
|
if (maxTries <= 0) {
|
||||||
throw new Error(
|
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 });
|
returnData.push({ success: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue