node-ytdl-core icon indicating copy to clipboard operation
node-ytdl-core copied to clipboard

How to set a timeout for getInfo request?

Open mh4ck opened this issue 5 years ago • 7 comments

Hi, i'm requesting the video info over proxy and some proxies take a lot of time (more than 30s) to finish the request.

Is there a simple way to set the max. time taken for a get info request, so that i can start again with another proxy? I already tried to set "timeout" in the request options but it seems to not have any affect.

Or is there any way to destroy the actual request so that i can set an own timer?

I already googled a lot for setting timeouts for requests over proxy but haven't found a working solution. Maybe someown got it already to work?

mh4ck avatar Jul 31 '20 21:07 mh4ck

try setting the timeout in the request object

let stream ytdl(id);
stream.on('request', (req) => {
  req.setTimeout(...);
});

this may be an option in ytdl that's added in the future

fent avatar Jul 31 '20 22:07 fent

Hi @fent, i have a seperate microservice for requesting the info and for downloading the video. So i'm not calling ytdl, instead i'm just calling getInfo().. There is no stream returned or?

I'm doing it that way to prevent double info requests if the download process throws an unhandled error or smth else and needed to restart.

mh4ck avatar Jul 31 '20 22:07 mh4ck

Hi @fent, i have a seperate microservice for requesting the info and for downloading the video. So i'm not calling ytdl, instead i'm just calling getInfo().. There is no stream returned or?

hmm, then it's something that could be added to requestOptions. otherwise, you can also add a timeout to an agent. look at https://nodejs.org/api/http.html#http_new_agent_options

I'm doing it that way to prevent double info requests if the download process throws an unhandled error or smth else and needed to restart.

in that case, you can change the internal cache that ytdl uses for caching info. it's at ytdl.cache.info.timeout, default is 1000 for 1 sec. I guess the cache should be documented.

fent avatar Jul 31 '20 23:07 fent

But the internal cache is not written to any database or file or? So if the node process restarts the cache is empty, or did i got it wrong?

Btw. why the info expiration time (that we get from yt) is not the directive for the timeout?

mh4ck avatar Aug 01 '20 08:08 mh4ck

But the internal cache is not written to any database or file or? So if the node process restarts the cache is empty, or did i got it wrong?

right, it's not written to anything, only memory. you can do something like what ytdl does to save the signature cache to a file

https://github.com/fent/node-ytdl/blob/master/bin/ytdl.js#L50-L66

to do items to make this better are

  • document all of the caches and ways to modify them
  • customizable storage
    • maybe as simple as documenting overwriting set and get
    • or providing some basic storage classes that can be inherited and overwritten such as MemoryStore, FileStore. these may already be on npm

Btw. why the info expiration time (that we get from yt) is not the directive for the timeout?

the format url expiration time?

fent avatar Aug 01 '20 21:08 fent

Forgive me if I'm missing something, but since ytdl.getInfo()'s options extend miniget, which then extends http.request, couldn't you just pass timeout as an option directly?

redbrain avatar Dec 11 '20 13:12 redbrain

ytdl.getInfo options does not extend miniget's options but close & nice spotting sth like ytdl('<id>', { requestOptions: { timeout: 420 } }); should work

TimeForANinja avatar Dec 11 '20 14:12 TimeForANinja