Reputation: 39
My question is simple: How many http requests make how many tcp connections to server in Go.
I'm coding a tool can send many http requests, but I find all this requests over one or two tcp connetions, seem like golang http Transport have a connections pool.
Upvotes: 0
Views: 744
Reputation: 3968
If you are using the DefaultTransport
for your HTTP requests then the TCP connections are reused.
DefaultTransport is the default implementation of Transport and is used by DefaultClient. It establishes network connections as needed and caches them for reuse by subsequent calls.
You can use MaxConnsPerHost
to limit the total number of connections:
// MaxConnsPerHost optionally limits the total number of
// connections per host, including connections in the dialing,
// active, and idle states. On limit violation, dials will block.
//
// Zero means no limit.
//
// For HTTP/2, this currently only controls the number of new
// connections being created at a time, instead of the total
// number. In practice, hosts using HTTP/2 only have about one
// idle connection, though.
MaxConnsPerHost int
Edit
I highly suggest you read the docs as Go has one of the most well documented standard library. Anyway, you can configure http.Transport
to disable keep-alive to force usage of one TCP connection per request.
// DisableKeepAlives, if true, disables HTTP keep-alives and
// will only use the connection to the server for a single
// HTTP request.
//
// This is unrelated to the similarly named TCP keep-alives.
DisableKeepAlives bool
Upvotes: 1