我们有特定的NodeJS软件,可以将文件下载到其本地文件系统中。文件的大小可以从几KB到GB。有时,没有明显的原因,下载速度实际上会变慢。实际的连接是相当体面和稳定的。令人奇怪的是,缓慢的下载只会在下载开始时发生,而不会扩展到适当的速度。
此问题是间歇性发生的,可能是从第一次下载开始或在另一个下载完成之后发生。该代码按顺序下载文件,因此不能一次下载多个文件。发生问题时,只需停止NodeJS进程并重新启动即可解决该问题。
在输出样本下面:
content_VIDEO_a67a8535-2db4-4d36-9869-f16b1916df77_20180717_en-US.zip
[ ] 0%
0.00 of 3.26 GB
------------------------------------
content_VIDEO_a67a8535-2db4-4d36-9869-f16b1916df77_20180717_en-US.zip
[================================== ] 34%
1.13 of 3.26 GB
------------------------------------
content_VIDEO_a67a8535-2db4-4d36-9869-f16b1916df77_20180717_en-US.zip
[======================================================================= ] 71%
2.33 of 3.26 GB
content_VIDEO_a67a8535-2db4-4d36-9869-f16b1916df77_20180717_en-US.zip
[================================================================================================= ] 97%
3.17 of 3.26 GB
------------------------------------
Download of content_VIDEO_a67a8535-2db4-4d36-9869-f16b1916df77_20180717_en-US.zip completed.
Downloading next file
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.00 of 3.48 GB
------------------------------------
content_VIDEO_51bd27ee-12c5-49e0-b321-f0be58d647b3_20180717_en-US.zip
[ ] 0%
0.01 of 3.48 GB
如您在上面的输出中看到的,第一个(或上一个文件)以正常速度下载。同一台服务器上的第二个几乎根本没有下载。
对于下载,库Request(v2.87.0)与Request-progress(v3.0.0)一起用于显示下载进度。 Windows和Linux平台均会发生此问题。我们也注意到也会在多个NodeJS版本上发布,例如8.11.1和8.11.3
用于下载的代码:
function download (url, pathToFile) {
const context = this;
return new Promise((resolve, reject) => {
const options = {
// Set 10 minutes timeout for connect / fetch
timeout: 600000,
headers: {}
};
const req = request.get(url, options);
progress(req, {
throttle: 60000, // Throttle the progress event, defaults to 1000ms
delay: 0 // Only start to emit after a delay of some milliseconds - default is 0ms
})
.on('progress', (state) => {
showProgress(state);
})
.on('error', (error) => {
req.abort();
reject(error);
})
.on('response', (response) => {
response.on('end', resolve);
})
.pipe(fs.createWriteStream(pathToFile, { flags: options.headers.Range ? 'a' : 'w' }));
});
};
function showProgress(state) {
let progressIndicator = '';
let balanceIndicator = '';
let progress = 0;
const percent = parseInt(state.percent * 100);
for (progress = 1; progress <= percent; progress++) {
progressIndicator += '=';
}
for (let balance = progress; balance <= 100; balance++) {
balanceIndicator += ' ';
}
let totalSize = ((state.size.total / 1024) / 1024).toFixed(2);
let receivedSize = ((state.size.transferred / 1024) / 1024).toFixed(2);
let sizeUnit = 'MB';
if (totalSize >= 1024) {
totalSize = (totalSize / 1024).toFixed(2);
receivedSize = (receivedSize / 1024).toFixed(2);
sizeUnit = 'GB';
}
console.log(`[${progressIndicator}${balanceIndicator}]`, `${percent}%`);
console.log(receivedSize, 'of', totalSize, sizeUnit, '\n');
console.log('------------------------------------');
console.log(' ');
}
没有错误,下载速度非常慢,因此很难调试出此问题的出处。
欢迎提出任何建议