Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection timeout not supported #99

Open
stefan-guggisberg opened this issue Apr 3, 2020 · 0 comments
Open

connection timeout not supported #99

stefan-guggisberg opened this issue Apr 3, 2020 · 0 comments

Comments

@stefan-guggisberg
Copy link
Contributor

The timeout fetch option doesn't account for the time it takes to establish a connection.

An example:

  await fetch('http://example.com:81', { timeout: 1000 });

The above statement hangs forever (i.e. ~75s in my case on macos). Aborting the request using AbortController doesn't work either. The timeout option seems only to apply for already established connections.

The problem is here: https://github.com/grantila/fetch-h2/blob/master/lib/context-http1.ts#L375, where no timeout is set. Something like the following should work (sorry, not Typescript):

     async makeNewConnection(url, timeout) {
        let handled = false;
        return new Promise((resolve, reject) => {
            const { hostname, port } = utils_1.parseInput(url);
            const socket = net_1.createConnection({ port: parseInt(port, 10), host: hostname, timeout }, () => {
                // reset connect timeout
                socket.setTimeout(0);
                resolve(socket);
            });
            socket.once("error", (err) => {
                if (!handled) {
                    handled = true;
                    reject(err);
                }
            });
            socket.once("timeout", () => {
                if (!handled) {
                    handled = true;
                    reject(new core_1.TimeoutError());
                }
                socket.destroy();
            });
        });
    }

SImilar problem exists with https:// where a timeout can be set in the context options, e.g.
new Context( { session: { timeout: 1000 } }), but the timeout event is not handled:

https://github.com/grantila/fetch-h2/blob/master/lib/context-https.ts#L53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant