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

Bug: context deadline exceeded (Client.Timeout exceeded while awaiting headers) #646

Open
daidai21 opened this issue Apr 13, 2023 · 8 comments

Comments

@daidai21
Copy link

daidai21 commented Apr 13, 2023

when use like this:

func service(query string) {
	resp, err := resty.New().SetTimeout(20 * time.Second).R().
		EnableTrace().
		SetFormData(map[string]string{
			"query": query,
		}).
		Post("xxx")

	
}

out: (occasionally)

context deadline exceeded (Client.Timeout exceeded while awaiting headers)
@daidai21
Copy link
Author

change to:

cli := resty.New().SetTimeout(20 * time.Second)


func service(query string) {
	resp, err := cli.R().
		EnableTrace().
		SetFormData(map[string]string{
			"query": query,
		}).
		Post("xxx")
}

then succeed.

The error will no longer occur

@daidai21
Copy link
Author

1GB mem, 1core cpu server

@jeevatkm
Copy link
Member

@daidai21 typically, creating a resty client once and firing N no. of request using that client is recommended.
In the first example, you're creating a client every time, which is unsuitable for system resource usage.
The second example usage is correct.

@athenakia
Copy link

@daidai21 typically, creating a resty client once and firing N no. of request using that client is recommended. In the first example, you're creating a client every time, which is unsuitable for system resource usage. The second example usage is correct.

I encountered the same case when I followed the second example as well. Moreover, why does the first example result in an error, instead of giving a warning tip? This is quite frustrating.

@athenakia
Copy link

@daidai21 typically, creating a resty client once and firing N no. of request using that client is recommended. In the first example, you're creating a client every time, which is unsuitable for system resource usage. The second example usage is correct.

I encountered the same case when I followed the second example as well. Moreover, why does the first example result in an error, instead of giving a warning tip? This is quite frustrating.

Sorry, the issue I am encountering is likely due to network instability caused by IDC.

@athenakia
Copy link

#593

@athenakia
Copy link

@daidai21 typically, creating a resty client once and firing N no. of request using that client is recommended. In the first example, you're creating a client every time, which is unsuitable for system resource usage. The second example usage is correct.

I encountered the same case when I followed the second example as well. Moreover, why does the first example result in an error, instead of giving a warning tip? This is quite frustrating.

Sorry, the issue I am encountering is likely due to network instability caused by IDC.

This IDC provides a dynamic IP server, so it should be a old problem(network problems when switching IP).

@athenakia
Copy link

ChatGPT suggests setting DisableKeepAlives in the transport:

client.SetTransport(&http.Transport{
	DisableKeepAlives: true,
})

It works well.

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

No branches or pull requests

3 participants