fent / node-miniget

A small http(s) GET library.
MIT License
55 stars 17 forks source link

[question]what will happen when the stream pipe to is closed? #64

Closed ocylib closed 2 years ago

ocylib commented 2 years ago

I created a server as follow:

const http = require('http');
const servre = http.createServer((req, res) => {
        //url is a link to a video, such as url in format  get by ytdl-core
    const stream = miniget(url);
        res.writeHead(200, {'Content-Type': `video/mp4`,});
    stream.pipe(res);
});

servre.listen(3000, () => console.log('running'));

by doing this, when I run the server, I can watch the video at localhost:3000. it seems everything is fine

however, when I open more than six tabs to open the same page( localhost:3000 ), the sixth tab will never respond to anything and the network is pending until closing a tab.

I thought the lake of memory caused this. and I add the following codes:

setInterval(() => {
    const { rss, external } = process.memoryUsage();
    console.log('memoryUsage:', (rss + external) / 1024 / 1024);
}, 1000);

when the server starts before opening many tabs, the memory used about 34M went tabs opened, until never respond, the memory used about 60M close all tabs, the memory used about 55M, will never return to 34M

I wonder what miniget does when the browser tab is closed, will it stop the request automatically and clear memory or not? if not, I tried res.on('close',()=>{stream._destroy()}), but it seems doesn't work

TimeForANinja commented 2 years ago

the stream.pipe(res) should also pipe events like close from res to stream ytdl should then do some cleanup and the node garbage collection should free the memory at some point

what u're experiencing is a browser limit - allowing only 6 open sockets to a given server

ocylib commented 2 years ago

Nice work. I opened six tabs in one browser and six tabs in another browser, same PC, everything is fine