apify / proxy-chain

Node.js implementation of a proxy server (think Squid) with support for SSL, authentication and upstream proxy chaining.
https://www.npmjs.com/package/proxy-chain
Apache License 2.0
839 stars 139 forks source link

Can't set a request timeout #289

Closed thecatontheflat closed 1 year ago

thecatontheflat commented 2 years ago

I can't seem to find a way to set the request timeout out of the box. For example, if the target URL is not responding, it will just hang the socket.

I am going to solve it by handling the timeout myself via setTimeout() and checking if there was any response in the socket, but maybe I'm missing a more straightforward way?

thecatontheflat commented 2 years ago

I guess something like this works?

    prepareRequestFunction: ({ request, username, password, hostname, port, isHttp, connectionId }) => {

      // If we received no data, we should release the socket
      const connectionTimeout = 1 * 1000;
      setTimeout(() => {
        const stats = server.getConnectionStats(connectionId);

        if (stats && stats.srcTxBytes === 0) {
          server.closeConnection(connectionId);
          console.log('Releasing hanged connection');
        }

      }, connectionTimeout);
    },
Jakeroid commented 1 year ago

Was something changed with that?

fnesveda commented 1 year ago

This should be rather solved on the client side, see e.g. the Axios docs for how to do it: https://axios-http.com/docs/cancellation

In short:

const response = await axios.get('https://example.com', {
    signal: AbortSignal.timeout(5000)
})