abalabahaha / eris

A NodeJS Discord library
https://abal.moe/Eris/
MIT License
1.49k stars 417 forks source link

Event Loop blocks while Sending Large Attachments #1321

Open gamer0mega opened 2 years ago

gamer0mega commented 2 years ago

I am having issues with sending large attachments, they simply block the entire event loop until the attachment is sent. I couldn't find the cause inside the RequestHandler, sending a 4MB video file is enough to block the loop for a few seconds. Once I returned client in a function and used it in a eval, I had to restart the process just because the output file was never sent and the loop was completely blocked. I am running eris@0.16.2-dev (git+ssh://git@github.com/abalabahaha/eris.git#1a6e043331aea04de307a71d2e50e9680d1a5ae3)

DonovanDMC commented 2 years ago

This is bound to happen when you don't have a good network connection, as nodejs is single threaded

gamer0mega commented 2 years ago

This is bound to happen when you don't have a good network connection, as nodejs is single threaded

But magically, on the exact same VPS, the exact same file doesnt block the event loop while sending out on my upcoming lib

abalabahaha commented 2 years ago

This is bound to happen when you don't have a good network connection, as nodejs is single threaded

Irrelevant, threaded I/O is a major selling point of Node.

@gamer0mega can you provide a (somewhat minimal) repro script? Eris shouldn't be using sync APIs for network I/O, so something is being overlooked here.

gamer0mega commented 2 years ago

This is bound to happen when you don't have a good network connection, as nodejs is single threaded

Irrelevant, threaded I/O is a major selling point of Node.

@gamer0mega can you provide a (somewhat minimal) repro script? Eris shouldn't be using sync APIs for network I/O, so something is being overlooked here.

fs.readFile("video.mp4", ((err, buf) => msg.channel.createMessage("", {file: buf, name: "vid.mp4"})));

You can get the video at https://media.discordapp.net/attachments/795468742670417971/929560222249410642/vid.mp4

Here is a demo: https://media.discordapp.net/attachments/795468742670417971/929560623145173002/Screen_Recording_20220109-052052_Discord-Canary.mp4

abalabahaha commented 2 years ago

It seems your issue is that the ping message is delayed until after the file message? That is the ratelimiter system working as designed: requests in one route are sent sequentially, but without blocking the event loop or other routes. Try sending the two messages in different channels (different ratelimit routes), and they shouldn't be blocking each other if there aren't any other bottlenecks.

gamer0mega commented 2 years ago

It seems your issue is that the ping message is delayed until after the file message? That is the ratelimiter system working as designed: requests in one route are sent sequentially, but without blocking the event loop or other routes. Try sending the two messages in different channels (different ratelimit routes), and they shouldn't be blocking each other if there aren't any other bottlenecks.

There you go, The file is too big so i can show you from other channels. On my main bot i use a fork from DonovanDMC(0.16.0-dev) which i didnt update yet, and sending the exact same spongebob video is almost 3 times slower, I am unsure if thats an issue with the event count or the eris version. Video: https://media.discordapp.net/attachments/831113546846306324/930093239959191582/Screen_Recording_20220110-163828_Discord-Canary.mp4 Length: https://media.discordapp.net/attachments/795468742670417971/930093753652371496/Screenshot_20220110-164103_Discord-Canary.png