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

Confusion about timeout #1529

Closed
kukayiyi opened this issue Mar 22, 2023 · 5 comments
Closed

Confusion about timeout #1529

kukayiyi opened this issue Mar 22, 2023 · 5 comments

Comments

@kukayiyi
Copy link
Contributor

I want to use fasthttp.client.DoTimeout() to implement timeout control. It should return immediately after the given timeout and proceed to the next step. After searching various sources, I only found that I can modify the client’s dial to achieve this, but this is not elegant and loses the meaning of using DoTimeout(). My requirement is to easily set different timeouts for each request. After checking the history versions, I found that this problem might have occurred after #1346 . As a last resort, I had to downgrade my fasthttp to version 1.38.0 in my project to meet my needs. Am I using it wrong?

@kukayiyi
Copy link
Contributor Author

I have checked the source code and tried to modify readTimeout, writeTimeout and other max* parameters, but none of them work. No matter how other parameters are set, DoTimeout() will follow the time of the client’s dial.

@kukayiyi
Copy link
Contributor Author

I used the official sample code https://github.com/valyala/fasthttp/blob/master/examples/client/client.go and still have the same problem. Also, GetTimeout has a similar problem. [When the timeout parameter set is less than 3 seconds, it can normally return a timeout, but when it is greater than 3 seconds, it still fails to work

@erikdubbelboer
Copy link
Collaborator

I'm not sure I fully understand, can you post some sample code how you are using fasthttp?

@kukayiyi
Copy link
Contributor Author

kukayiyi commented Mar 29, 2023

I'm not sure I fully understand, can you post some sample code how you are using fasthttp?

var client  = &fasthttp.Client{
	MaxConnsPerHost:           1024, // MaxConnsPerHost  default is 512, increase to 16384
	ReadTimeout:               15 * time.Second,
	WriteTimeout:              15 * time.Second,

	// I set dial as a workaround, but that changes all timeouts
	//Dial: func(addr string) (net.Conn, error) {
	//	return fasthttp.DialTimeout(addr, time.Duration(config.Conf.Server.RequestTimeout)*time.Millisecond)
	//},
	//
}
req := fasthttp.AcquireRequest()
// build req
resp := fasthttp.AcquireResponse()
err := client.DoTimeout(req, resp, 15*time.Second)

I use it like this, but when the access is slow, it will return after 3 seconds, not 15 seconds, unless I set the dial

@erikdubbelboer
Copy link
Collaborator

I'm afraid that currently setting the Dial function yourself is the only way to make this work.

The timeout is not propagated to the right place:

fasthttp/client.go

Lines 1912 to 1920 in d0f2727

timeout := c.ReadTimeout + c.WriteTimeout
if timeout <= 0 {
timeout = DefaultDialTimeout
}
deadline := time.Now().Add(timeout)
for n > 0 {
addr := c.nextAddr()
tlsConfig := c.cachedTLSConfig(addr)
conn, err = dialAddr(addr, c.Dial, c.DialDualStack, c.IsTLS, tlsConfig, c.WriteTimeout)

A pull request to fix this would be welcome.

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

No branches or pull requests

2 participants