help
help copied to clipboard
http2.request is downloading data with a very slow speed, if the network latency is significant
Node.js Version
v22.5.1
NPM Version
v10.8.2
Operating System
Linux VPS 6.8.0-38-generic #38-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 7 15:25:01 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
http2
Description
As the title suggests, the downloading speed could be around 200-300KB/s if latency is about 200ms.
If the latency is negligible, like <5ms, the downloading speed seems to be normal.
I tried createConnection callback with tls.connect() as well, after setting highWaterMark to 1048576 the speed still didn't seem to recover to normal.
Minimal Reproduction
- Set up a cloud server VPS and install caddy http2 server on it. For example, the server VPS is located in Los Angeles, United States.
- Put some download test file on the server VPS.
- Set up another cloud VPS as the client. For example, the client VPS is located in Singapore.
- On the client VPS, use
curl --http2to test download speed, which looks normal. - Then use something like the following script to test http2 downloading speed in NodeJS, which is very slow.
async function testDownload(VPS_DOMAIN_NAME, TEST_FILENAME) {
let session = await new Promise((resolve) => {
http2.connect(`https://${VPS_DOMAIN_NAME}`, (session) => {
console.log("connected");
resolve(session);
});
});
let opts = {
[http2.constants.HTTP2_HEADER_METHOD]: http2.constants.HTTP2_METHOD_GET,
[http2.constants.HTTP2_HEADER_PATH]: `/${TEST_FILENAME}`,
}
let total = 0; startTime = Date.now();
await new Promise((resolve) => {
session.request(opts).on('data', (chunk) => {
total += chunk.byteLength;
console.log(`${Math.trunc(total / (Date.now() - startTime))} KB/s`);
}).on('end', () => {
console.log(`total = ${total}`);
session.destroy();
resolve();
});
});
}
Output
root@test:~# ping VPS_DOMAIN_NAME
PING VPS_DOMAIN_NAME (VPS_IP) 56(84) bytes of data.
64 bytes from VPS_IP: icmp_seq=1 ttl=54 time=201 ms
root@test:~# curl --http2 https://VPS_DOMAIN/TEST_FILENAME > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 245M 100 245M 0 0 5533k 0 0:00:45 0:00:45 --:--:-- 6470k
root@test:~# node testdownload.js
connected
24 KB/s
47 KB/s
43 KB/s
59 KB/s
73 KB/s
73 KB/s
79 KB/s
92 KB/s
95 KB/s
...
(it climibed up to about 300KB/s and then stayed steady)
...
308 KB/s
308 KB/s
308 KB/s
308 KB/s
308 KB/s
308 KB/s
total = 257062168
Before You Submit
- [X] I have looked for issues that already exist before submitting this
- [X] My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask