出于测试目的,我尝试在 Go 中创建一个禁用连接池的 net/http.Client
。我想要实现的是每个 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小于零。
设置任一值都会禁用池化。当DisableKeepAlives 为true 时,传输会添加Connection: close
请求 header 。这可能是理想的,也可能不是理想的,具体取决于您正在测试的内容。
以下是设置DisableKeepAlives的方法:
t := http.DefaultTransport.(*http.Transport).Clone()
t.DisableKeepAlives = true
c := &http.Client{Transport: t}
Run a demonstration of DisableKeepAlives = true on the playground .
以下是设置 MaxIdleConnsPerHost 的方法:
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConnsPerHost = -1
c := &http.Client{Transport: t}
Run a demonstration of MaxIdleConnsPerHost = -1 on the playground .
上面的代码clones默认传输以确保使用默认传输选项。如果您明确想要问题中的选项,请使用
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 不限制每个主机的事件连接数。 See this playground example for a demonstration .
设置 Dialer.KeepAlive 不会禁用池化至-1。请参阅this answer以获得解释。
关于go - 关闭 Go http.Client 的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57683132/