go - 关闭 Go http.Client 的连接池
问题描述
出于测试目的,我正在尝试net/http.Client
在 Go 中创建一个禁用连接池的功能。我想要实现的是在每个 HTTP/1.x 请求的地址上建立一个新的 TCP 连接。
目前我有:
c = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
任何想法我应该如何调整这个?
我看到如果我设置c.Transport.MaxIdleConns = 1
它可以工作,但我不确定这是否仍然允许 1 个使用中 + 1 个空闲(总共 2 个)TCP 连接:
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int
同样,似乎c.Dialer.KeepAlive = -1
也可以这样做:
// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are enabled if supported by the protocol
// and operating system. Network protocols or operating systems
// that do not support keep-alives ignore this field.
// If negative, keep-alives are disabled.
但我不确定 TCP 连接 + Keep-Alive + HTTP 的行为。
另一种方法是尝试尽快杀死空闲的 TCP 连接,所以我设置了c.Transport.IdleConnTimeout = 1*time.Nanosecond
.
当我这样做时,我Client.Do()
现在偶尔会返回错误:
tls: use of closed connection
我怀疑这是一个 Go stdlib 问题(可能是一场比赛),它使用了一个应该从池中取出的连接。
解决方案
在函数Transport.tryPutIdleConn中将连接添加到池中。如果Transport.DisableKeepAlives为 true 或 Transport.MaxIdleConnsPerHost小于零,则连接不会被池化。
设置任一值都会禁用池化。Connection: close
当 DisableKeepAlives 为 true 时,传输会添加请求标头。根据您正在测试的内容,这可能是可取的,也可能不是可取的。
以下是设置 DisableKeepAlives 的方法:
t := http.DefaultTransport.(*http.Transport).Clone()
t.DisableKeepAlives = true
c := &http.Client{Transport: t}
在操场上运行 DisableKeepAlives = true 的演示。
以下是设置 MaxIdleConnsPerHost 的方法:
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConnsPerHost = -1
c := &http.Client{Transport: t}
在操场上运行 MaxIdleConnsPerHost = -1 的演示。
上面的代码克隆了默认传输以确保使用默认传输选项。如果您明确想要问题中的选项,请使用
c = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true,
},
}
或者
c = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: -1,
},
}
MaxIdleConnsPerHost 不限制每个主机的活动连接数。 有关演示,请参阅此操场示例。
通过将Dialer.KeepAlive设置为 -1不会禁用池 化。请参阅此答案以获取解释。
推荐阅读
- javascript - 反应不加载脚本
- excel - 如何/使用哪个公式来显示错误条件的组合文本结果(用于挂起的任务报告使用)?
- python - 对 MFCC 处理感到困惑
- python - 如何设计一个 Python 函数来打印仅将前对角元素设置为 True 的矩阵?
- node.js - 生产构建 - React Client App 和 Express Api 错误无法在模块外使用 import 语句
- python - 如何使用配置文件部署烧瓶应用程序?
- haskell - getAllFiles(但不是符号链接)
- amazon-web-services - 如何在 AMI 中安装 aws-cfn-bootstrap?
- ios - iOS UiView 布局问题
- reactjs - 为什么我的方法没有在 reactjs 中被调用?