How does multithreading affect http keep-alive connection?
http.Client has a
Transport to which it delegates a lot of the low-level details of making a request. You can change pretty much anything by giving your client a custom Transport. The rest of this answer will largely assume that you're using
http.DefaultClient or at least a client with
When making a new request, if an idle connection to the appropriate server is available, the transport will use it.
If no idle connection is available (because there never was one, or because other goroutines are using them all, or because the server closed the connection, or there was some other error) then the transport will consider making a new connection, limited by
MaxConnsPerHost (default: no limit). If
MaxConnsPerHost would be exceeded, then the request will block until an existing request completes and a connection becomes available. Otherwise, a new connection will be made for this request.
On completion of a request, the client will cache the connection for later use (limited by
DefaultTransport has a limit of 100 idle connections globally, and no limit per-host).
Idle connections will be closed after
IdleConnTimeout if they aren't used to make a request; for
DefaultTransport the limit is 90 seconds.
All of which means that by default, Go will make enough connections to satisfy parallelism (up to certain limits which you can adjust) but it will also reuse keep-alive connections as much as possible by maintaining a pool of idle connections for some length of time.