我的问题很简单:在Go中,多少个http请求使多少个tcp连接到服务器。
我正在编写一个工具可以发送许多http请求,但我发现所有这些请求都通过一个或两个tcp连接,似乎golang http传输有一个连接池。
发布于 2018-11-07 11:01:48
如果您正在为您的HTTP请求使用DefaultTransport,那么将重用这些TCP连接。
DefaultTransport是传输的默认实现,由DefaultClient使用。它根据需要建立网络连接,并缓存这些连接以供后续调用重用。
您可以使用MaxConnsPerHost来限制连接总数:
// 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编辑
我强烈建议您阅读docs,因为Go拥有文档最完善的标准库之一。无论如何,您可以将http.Transport配置为禁用keep-alive,以强制每个请求使用一个TCP连接。
// 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 boolhttps://stackoverflow.com/questions/53182674
复制相似问题