首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每个tcp连接上的每个http请求

每个tcp连接上的每个http请求
EN

Stack Overflow用户
提问于 2018-11-07 09:58:03
回答 1查看 268关注 0票数 0

我的问题很简单:在Go中,多少个http请求使多少个tcp连接到服务器。

我正在编写一个工具可以发送许多http请求,但我发现所有这些请求都通过一个或两个tcp连接,似乎golang http传输有一个连接池。

EN

回答 1

Stack Overflow用户

发布于 2018-11-07 11:01:48

如果您正在为您的HTTP请求使用DefaultTransport,那么将重用这些TCP连接。

DefaultTransport是传输的默认实现,由DefaultClient使用。它根据需要建立网络连接,并缓存这些连接以供后续调用重用。

您可以使用MaxConnsPerHost来限制连接总数:

代码语言:javascript
复制
// 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连接。

代码语言:javascript
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53182674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档