Does the REST handler retry failed requests?

See title. I'm mainly referring to errors with a 5xx code, but I do want to know if the REST handler retries any requests of any type. I want to handle those errors and would like to know if I'm just repeating error handling logic that discord.js already performs. And if there are community resources or examples on how best to implement this please point me to them, I wasn't able to find anything when I looked.
21 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
NyR
NyR3mo ago
The default retry is 3 for 500 error codes or timeout errors, it doesn't handle rest of the errors, except queuing requests on rate limits
d.js docs
d.js docs3mo ago
:propertysignature: RESTOptions#retries @2.4.0 The number of retries for errors with the 500 code, or errors that timeout
ElJay
ElJayOP3mo ago
So it handles rate limit errors completely?
NyR
NyR3mo ago
Yes, it won't throw error on rate limit (unless you want it to throw, which you can do so by providing appropriateoption), it'll queue it for retry when the limit expires determined by Retry-After in the response headers
ElJay
ElJayOP3mo ago
Also for 5xx codes and timeouts does it just immediately retry or is there some kind of back off? Is there a way to configure/add back off?
ElJay
ElJayOP3mo ago
So if I wanted to implement back off in the retries I would need to set that REST option to 0 and then implement retries myself? Or is there some easier way to do it?
NyR
NyR3mo ago
I don't think there's a way to do that within REST itself, I suppose that's the way you can handle it, yeah
ElJay
ElJayOP3mo ago
Is there any kind of setting where I could pass a custom function or something to implement this behavior? Or do I have to implement it at the top-level? For example:
await handleRetries(() => client.users.send(/* .. */));
// instead of
await client.users.send(/* .. */);
await handleRetries(() => client.users.send(/* .. */));
// instead of
await client.users.send(/* .. */);
I really don't want to have to do this if there's a better way.
NyR
NyR3mo ago
As I said, It already handles retries, I'm not sure what you are hoping to achieve by doing this?
ElJay
ElJayOP3mo ago
implementing back off instead of immediately retrying
NyR
NyR3mo ago
You'll have to implement it yourself then, there's offset option, but that's for rate limits to add on top of the Retry-After, I'm not sure why you don't want it to attempt immediately?
ElJay
ElJayOP3mo ago
I'm surprised you haven't heard of this kind of strategy before. It's standard practice and prevents several issues with immediate retries. A quick google gave me these explanations on what back-offs are and why they are important: https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter https://medium.com/bobble-engineering/how-does-exponential-backoff-work-90ef02401c65 https://medium.com/@roopa.kushtagi/decoding-exponential-backoff-a-blueprint-for-robust-communication-de21459aa98f To give a more tailored answer, during discord outages you are going to get 5xx errors and timeouts when you immediately retry, hit the retry limit, and the request will fail. but with back-off you avoid making unnecessary requests and are able to resume much more smoothly when the outage ends and the first few retries will be fairly fast, so in the cases where it's a one-time error you aren't losing a relevant amount of time
NyR
NyR3mo ago
You'll have to implement it yourself then
ElJay
ElJayOP3mo ago
would it be worth opening an issue about adding support for this to discord.js? I'm kinda surprised the library has nothing for this.
NyR
NyR3mo ago
You can if you want
space
space3mo ago
This sounds like a reasonable request to me.
ElJay
ElJayOP3mo ago
I opened a related issue (#10571), should I mark this post as resolved or leave it open?
NyR
NyR3mo ago
I say keep it open for any further discussion perhaps?
Sjögrén
Sjögrén2mo ago
I've noticed that this is problematic and can cause infinite loops of requests
Want results from more Discord servers?
Add your server