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.91k stars 1.76k forks source link

custom dialer in fasthttp.Client does not respect request timeout #1664

Closed avivpxi closed 10 months ago

avivpxi commented 1 year ago

If a custom dialer is set when using a fasthttp.Client, request timeout will not be respected if a dial operation hangs.

example code:

customDialer := fasthttp.TCPDialer{
    DNSCacheDuration: time.Hour,
}
client := &fasthttp.Client{
    Dial: customDialer.Dial,
}
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
client.DoTimeout(req, resp, time.Millisecond * 500)

with this code, if customDialer.Dial call hangs, you would expect client.DoTimeout to still fail after 500ms, but it would actually occur after 3s due to the default dialer timeout. Note how this function call actually ignores dialTimeout which is respected if no custom dialer was set.

I know we can't control any external dialer implementation if it chooses to ignore timeouts, but I think we should document here that overriding the default dialer causes request timeouts to be ignored.

Furthermore, I think we can consider adding a secondary [DialFunc](https://github.com/valyala/fasthttp/blob/master/client.go#L625) type that accepts timeout to allow custom dialers to respect request timeouts. Note that currently, if I use the built-in dialer with a custom setting (like the example above) I can't set a per-request timeout. Only a global one - meaning when I create a custom dialer I can set it's timeout using the global timeout variable, but if I want to dispatch different requests with different timeouts, the current dialer interface can't support it.

If it matters, i'd be happy to contribute to either the doc change or the implementation change if either is considered.

erikdubbelboer commented 1 year ago

I agree. A pull request for the documentation and DialFuncWithTimeout would also be very welcome! I don't have time to build this myself.

avivpxi commented 12 months ago

on it.

avivcarmis commented 12 months ago

PR - https://github.com/valyala/fasthttp/pull/1669