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

Allow a connect timeout to be specified #1739

Closed
mattiash opened this issue Aug 18, 2018 · 16 comments
Closed

Allow a connect timeout to be specified #1739

mattiash opened this issue Aug 18, 2018 · 16 comments
Assignees

Comments

@mattiash
Copy link

Summary

I want to use axios to connect to an http-server that is available via several different servers with different url:s. If one of the servers does not respond, I want to try another server instead. To get fast failovers, I want to set a low timeout for the request.

But the server sometimes takes a long time to respond (when I am making a complex query). To give the server time to generate the response, I have to set a high timeout.

What I really want to do is set a low connectTimeout, so that if the server has not accepted the tcp connection within this timeout, the request is aborted, but a high timeout to give the server time to generate the response.

Context

  • axios version: v0.18.0
  • Environment: node v8.11.3

PR coming up.

@SteffenLanger
Copy link

Hi @matthiash,

The idea is great. What is the current status of this issue and can I help you or the axios team somehow?

@mattiash
Copy link
Author

mattiash commented Nov 3, 2018

Well, I haven't gotten any feedback on my PR from anyone on the axios team. I solved my problem by using got instead. It allows me to specify timeouts for connect and request separately: https://github.com/sindresorhus/got#timeout

@benb1983
Copy link

benb1983 commented Jan 28, 2020

+1 this is big problem. Work around is to add a CancelToken, but it seems this could easily be baked in to the core..

@gwuah
Copy link

gwuah commented Jan 30, 2020

@benb1983 Can you give an example of how to implement that with CancelToken ?

@benb1983
Copy link

benb1983 commented Feb 4, 2020

@gwuah something like this... Sorry I lifted it from my code, but hopefully gives you an idea

let response = null
const source = axios.CancelToken.source()

        setTimeout(() => {
          if (response === null) {
            source.cancel()
          }
        }, 5000) // connection timeout here in ms (5 seconds)

response = await axios.post(this.authUrl, this.authData, {
        ...headers,
       cancelToken: source.token
      })

@cb-manideep
Copy link

+1

@lixaotec
Copy link

+1

@jasonsaayman
Copy link
Member

Hi,

I looked at the pull request, I think this is not the way to go about making this change. I will have a look in the future at how we can solve this. For now we are working on a release, after which we will look at this again.

Thanks

@dstokesf5
Copy link

@jasonsaayman, any updates on when this issue might be revisited?

@tdaniely
Copy link

In node you can do this by configuring the agents in the defaults, in an instance, or per request.
In the browser, I don't think a web page has any control, it's completely the browsers decision.

import axios from "axios";
import http from "http";
import https from "https";

axios.defaults.httpAgent = new http.Agent({ timeout: 5000 });
axios.defaults.httpsAgent = new https.Agent({ timeout: 5000 });

let client = axios.create({
    httpAgent: new http.Agent({ timeout: 5000 }),
    httpsAgent: new https.Agent({ timeout: 5000 }),
});

let response = axios.request({
    httpAgent: new http.Agent({ timeout: 5000 }),
    httpsAgent: new https.Agent({ timeout: 5000 }),
});

@Spasfonx
Copy link

Spasfonx commented Jan 28, 2021

Thanks @tdaniely this workaround with the Agent works perfectly 👌 we can differentiate connect timeout and request timeout. The only drawback is we have a wrong error message, the value in the error message is the timeout specified in axios config value instead of the correct Agent timeout value :

axios.defaults.timeout = 3000
axios.defaults.httpAgent = new http.Agent({ timeout: 500 })
axios.defaults.httpsAgent = new https.Agent({ timeout: 500 })

axios.get("https://www.google.com:81")
   .catch(console.log) // display 'timeout of 3000ms exceeded' but timeout after 500ms

@tdaniely
Copy link

Yes I was checking the timeout behaviour and I noticed some weird things in the code.
Specifically most of it is in not even axios but follow-redirects.
And it's throwing fake os error ECONNABORTED when the failure is applicative, which is even weirder.
Like you I would expect to get a specific error for a response timeout and a specific error for the tcp handshake timeout.
And this is especially important to detect if we are blocked by DDOS protection, or the server actually didn't respond.
Might want to open a seperate issue about the timeout throw handling.
I'm not contributing to axios, this is just what I see.

@jonmcnamaradale
Copy link

Thank you @tdaniely this helped me too, I couldn't understand why I wasn't getting a timeout but it is working fine now with your tip.

@huytran1ibm
Copy link

same here, we are experiencing ECONNABORTED errors

@jasonsaayman
Copy link
Member

See #4209

@serg06
Copy link

serg06 commented Nov 11, 2022

Before Axios V1 you can do:

axios.get("https://www.google.com:81", {
    httpAgent: http.Agent({ timeout: 500 }),
    httpsAgent: https.Agent({ timeout: 500 }),
});

I'm not sure if this works with Axios V1.

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

Successfully merging a pull request may close this issue.