request / request-promise

The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.
ISC License
4.77k stars 297 forks source link

timeout not working #316

Open GnidaKid69 opened 5 years ago

GnidaKid69 commented 5 years ago

I have a list of URLs where I want to make a request so I am using forEach and do all the requests with proxies and if proxy fails it moves on next one that's basically how my code works. I have set the timeout on 2000 ms and I am expecting it to drop request after that time give me an error and switch to another proxy but it's not doing that I guess. When it's checking last few links it takes too much time. I have 200 URLs to make a request on and last 10 or so takes more time than other 190. I think it's because the timeout is not working.

function Main(doc, i) {
    setTimeout(() => {
        var url = doc.url;
        var id = doc._id;
        var img = doc.img;
        var name = doc.name;

        var oldAvatarUrls = [];
        doc.avatars.forEach((avatar) => {
            oldAvatarUrls.push(avatar.url);
        });

        var listers = [];
        var newAvatarUrls = [];
        rp({
            url: url,
            method: 'GET',
            timeout: 2000,
            proxy: 'http://' + proxies[y]
        })
            .then((response) => {
                // Do something
            })
            .catch((err) => {
                if (y < proxies.length) {
                    y = y + 1;
                } else {
                    y = 0;
                }
                Main(doc, i);
            });
    }, 100 * i);
}
askucher commented 5 years ago

Does not work for me too

oleksiivorochenko commented 5 years ago

For me too

brianshen1990 commented 4 years ago

Not work either.

version: 4.2.4

Chengyanzhao commented 4 years ago

I have same problem. version: 4.2.2

strech345 commented 4 years ago

nobody interested to solve this?

I've the same problem v.4.2.4

i temporaly fix it by this

return new Promise((resolve, reject) => {

    const request = rp(options);
    request.then(res => {
        clearTimeout(timeout);
        resolve(res);
    }).catch(error => {
        clearTimeout(timeout);
        reject(error);
    })

    const timeout = setTimeout(() => {
        request.cancel();
        reject(new Error("Timeout more than 45 secs"));
    }, 1000 * 45);
});

Update:

maybe the cancel() not work, because in specific cases i get the error

(node:21056) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 pipe listeners added. Use emitter.setMaxListeners() to increase limit

It happend if inner timeout was called 10 times. So i guess it depends on this strange behaviour.

CreatiCoding commented 4 years ago

4.2.2 same problem

zedd3v commented 4 years ago

bump