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
net/http, crypto/tls: first HTTP request is consistently slower during TLS handshake in Go 1.17, but not 1.18 #50298
Comments
In the test performed, // Transport specifies the mechanism by which individual
// HTTP requests are made.
// If nil, DefaultTransport is used.
Transport RoundTripper If all 3 instances of an HTTP client end up reusing the default transport, there may be something cached and reused for future connections. Maybe this isn't a problem because of This doesn't appear to reproduce consistently for me with Go 1.18 Beta 1 and using my internet. I got a result that looks like:
So, it happened on first program execution, but not second and third. I would imagine this can happen because of how the internet works: when making a request to a new server for the first time, routers take a bit longer to route packets the first time. For subsequent requests, those "first time" delays aren't incurred. For related discussion, see https://stackoverflow.com/questions/54078692/why-first-network-call-takes-more-time-than-subsequent-ones. This is to say there may still be room for improvement in certain parts of |
Can you clarify how you're getting to that number? From your log, it looked like all 3 requests got first byte within 20 ms, not 100-200 ms. |
I tried running this with creating each client like this:
and the issue persists.
Yes, I was not clear about this in my original post - the line to track is
Makes sense, but I'm seeing the same thing when sending requests across different servers:
|
Thanks for clarifying. I'm still not seeing the same difference you're describing on my machine, at least not with Go 1.18 Beta 1:
Edit: But I do see it with Go 1.17.5! I'll let someone more familiar with |
Thank you anyways Dmitri! I tried running this with Go 1.18 Beta 1 and I don't see the issue either. I think the absolute first request to the server is still the slowest, by a small margin, but the speed up persists across multiple runs. |
Closing as 1.17 is no longer supported |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm seeing the first http request using
net/http
package andClient
to be consistently 100-200ms slower than the consecutive ones. This happens both when sending requests to one or multiple servers. The code to reproduce the issue:What did you expect to see?
Roughly the same timings for all the requests.
What did you see instead?
The slowdown for the first request happens for every server I tried and it's always only the first request, whether the Client is reused or recreated for every request.
Just to make sure that there's no confusion - I tried this for multiple servers, the first request is always slower. For server X, the average request duration is N if it's the first request and M if it's one of the consecutive requests. Duration M for consecutive requests is roughly the same across multiple runs regardless of whether the first request was to the server X or some other server. The duration M is always 100-200ms lower than N.
The logs I posted seem to indicate that the bulk of the slowdown is coming from TLS handshake. I couldn't find anything in the code that would point to some overhead/initialization happening exclusively for the first request.
I'm not necessarily saying there's a bug - this might be the expected behavior. In that case it would be great to update the documentation, I spend few hours researching this and found no explanation for this behavior.
Thank you for your time!
The text was updated successfully, but these errors were encountered: