前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang-MaxIdleConns与MaxIdleConnsPerHost

golang-MaxIdleConns与MaxIdleConnsPerHost

原创
作者头像
Roy@Cloud
发布2020-08-23 15:34:18
8.8K0
发布2020-08-23 15:34:18
举报
文章被收录于专栏:迁移服务平台迁移服务平台

在业务上使用http访问,需要初始化httpClient,其中在高并发场景下,MaxIdleConns与MaxIdleConnsPerHost的配置会影响业务的请求效率。

Client.Transports属性中包含:

  • MaxIdleConns 所有host的连接池最大连接数量,默认无穷大
  • MaxIdleConnsPerHost 每个host的连接池最大空闲连接数,默认2
  • MaxConnsPerHost 对每个host的最大连接数量,0表示不限制

相对应的代码解释(/src/net/http/transport.go:193:198:210)

// MaxIdleConns controls the maximum number of idle (keep-alive)

// connections across all hosts. Zero means no limit.

MaxIdleConns int

// MaxIdleConnsPerHost, if non-zero, controls the maximum idle

// (keep-alive) connections to keep per-host. If zero,

// DefaultMaxIdleConnsPerHost is used.

MaxIdleConnsPerHost int

// 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


测试:

如果MaxConnsPerHost=1,则只有一个http client被创建.

如果MaxIdleConnsPerHost=1,则会缓存一个http client.

golang 中默认http.client

newClient := &http.Client{

Timeout: time.Minute * 1, //设置超时时间

Transport: &http.Transport{

Dial: (&net.Dialer{

Timeout: 30 * time.Second, //限制建立TCP连接的时间

KeepAlive: 30 * time.Second,

}).Dial,

TLSHandshakeTimeout: 10 * time.Second, //限制 TLS握手的时间

ResponseHeaderTimeout: 10 * time.Second, //限制读取response header的时间,默认 timeout + 5*time.Second

ExpectContinueTimeout: 1 * time.Second, //限制client在发送包含 Expect: 100-continue的header到收到继续发送body的response之间的时间等待。

MaxIdleConns: 2, //所有host的连接池最大连接数量,默认无穷大

MaxIdleConnsPerHost: 1, //每个host的连接池最大空闲连接数,默认2

MaxConnsPerHost: 1, //每个host的最大连接数量

IdleConnTimeout: 3 * time.Minute, //how long an idle connection is kept in the connection pool.

},

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档