valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.66k stars 1.75k forks source link

about parameter MaxConnDuration #692

Closed ndsun closed 4 years ago

ndsun commented 4 years ago

when I user fasthttp.Client,why I can not set the MaxConnDuration value.I check the source code, fasthttpClient also user HostClient to send http request, but why HostClient can set MaxConnDuration , Client can't do this?

erikdubbelboer commented 4 years ago

Do you really want MaxConnDuration or do you want Client.MaxIdleConnDuration?

MaxConnDuration will kill the connection even if it just had a request. So it kills connections even if it isstill being used (not during a request). MaxIdleConnDuration will kill the connection only when it hasn't been used for MaxIdleConnDuration. This normally makes much more sense as there is no need to kill connections that are still being used.

If you really need MaxConnDuration in Client we can easily add it.

ndsun commented 4 years ago

Thanks for your reply. I know the differences between MaxConnDuration and MaxIdleConnDuration. And I really need MaxConnDuration.I want't to use it because I don't want a connection always being used, If you can add it, that's very good!

erikdubbelboer commented 4 years ago

This is fixed in: https://github.com/valyala/fasthttp/commit/5f666588000bc4a0e0852494958417036ccbb3b7

ndsun commented 4 years ago

Thank you very much!

transfercai commented 1 year ago

I'm curious about how MaxConnDuration works. Does it terminate connections while they are being used by a request?

For example, let's say I created a connection with a timeout of 10 seconds and MaxConnDuration is set to 12 seconds. If a downstream service responds in 9 seconds and another request comes in to reuse the same connection with a timeout of 10 seconds, the connection will be terminated in (12-9)=3 seconds. This can result in a timeout error.

erikdubbelboer commented 1 year ago

After MaxConnDuration it will send a Connection: close header and it will close the connection instead of putting it back into the connection pool. It won't affect the timeout of the request it self. So in your example the 2nd request would still get a 10 second timeout.