当 http client 返回值为不为空,只读取 response header,但不读 body 内容就执行 response.Body.Close(),那么连接会被主动关闭,得不到复用。

测试代码如下:

// xiaorui.ccfunc HttpGet() {for {fmt.Println("new")resp, err := http.Get("http://www.baidu.com")if err != nil {fmt.Println(err)continue}if resp.StatusCode == http.StatusOK {continue}resp.Body.Close()fmt.Println("go num", runtime.NumGoroutine())}
}

正如大家所想,除了 HEAD Method 外,很少会有只读取 header 的需求吧。

话说,golang httpclient 需要注意的地方着实不少。

  • 如没有 response.Body.Close(),有些小场景造成 persistConn 的 writeLoop 泄露。

  • 如 header 和 body 都不管,那么会造成泄露的连接干满连接池,后面的请求只能是短连接

上下文

由于某几个业务系统会疯狂调用各区域不同的 k8s 集群,为减少跨机房带来的时延、兼容新老 k8s 集群 api、减少k8s api-server 的负载,故而开发了 k8scache 服务。在部署运行后开始对该服务进行监控,发现 metrics 呈现的 QPS 跟连接数不成正比,qps 为 1500,连接数为 10 个。开始以为触发 idle timeout 被回收,但通过历史监控图分析到连接依然很少。????

按照对 k8scache 调用方的理解,他们经常粗暴的开启不少协程来对 k8scache 进行访问。已知默认的 golang httpclient transport 对连接数是有默认限制的,连接池总大小为 100,每个 host 连接数为 2。当并发对某 url 进行请求时,无法归还连接池,也就是超过连接池大小的连接会被主动clsoe()。所以,我司的 golang 脚手架中会对默认的 httpclient 创建高配的 transport,不太可能出现连接池爆满被 close 的问题。

如果真的是连接池爆了?  谁主动挑起关闭,谁就有 tcp time-wait 状态,但通过 netstat 命令只发现少量跟 k8scache 相关的 time-wait。

排查问题

已知问题,  为隐藏敏感信息,索性使用简单的场景设立问题的 case

tcpdump抓包分析问题?

包信息如下,通过最后一行可以确认是由客户端主动触发 RST连接重置 。触发RST的场景有很多,但常见的有 tw_bucket 满了、tcp 连接队列爆满且开启 tcp_abort_on_overflow、配置 so_linger、读缓冲区还有数据就给 close。

通过 linux 监控和内核日志可以确认不是内核配置的问题,配置 so_linger 更不可能。???? 大概率就一个可能,关闭未清空读缓冲区的连接。

22:11:01.790573 IP (tos 0x0, ttl 64, id 29826, offset 0, flags [DF], proto TCP (6), length 60)host-46.54550 > 110.242.68.3.http: Flags [S], cksum 0x5f62 (incorrect -> 0xb894), seq 1633933317, win 29200, options [mss 1460,sackOK,TS val 47230087 ecr 0,nop,wscale 7], length 0
22:11:01.801715 IP (tos 0x0, ttl 43, id 0, offset 0, flags [DF], proto TCP (6), length 52)110.242.68.3.http > host-46.54550: Flags [S.], cksum 0x00a0 (correct), seq 1871454056, ack 1633933318, win 29040, options [mss 1452,nop,nop,sackOK,nop,wscale 7], length 0
22:11:01.801757 IP (tos 0x0, ttl 64, id 29827, offset 0, flags [DF], proto TCP (6), length 40)host-46.54550 > 110.242.68.3.http: Flags [.], cksum 0x5f4e (incorrect -> 0xb1f5), seq 1, ack 1, win 229, length 0
22:11:01.801937 IP (tos 0x0, ttl 64, id 29828, offset 0, flags [DF], proto TCP (6), length 134)host-46.54550 > 110.242.68.3.http: Flags [P.], cksum 0x5fac (incorrect -> 0xb4d6), seq 1:95, ack 1, win 229, length 94: HTTP, length: 94GET / HTTP/1.1Host: www.baidu.comUser-Agent: Go-http-client/1.122:11:01.814122 IP (tos 0x0, ttl 43, id 657, offset 0, flags [DF], proto TCP (6), length 40)110.242.68.3.http > host-46.54550: Flags [.], cksum 0xb199 (correct), seq 1, ack 95, win 227, length 0
22:11:01.815179 IP (tos 0x0, ttl 43, id 658, offset 0, flags [DF], proto TCP (6), length 4136)110.242.68.3.http > host-46.54550: Flags [P.], cksum 0x6f4e (incorrect -> 0x0e70), seq 1:4097, ack 95, win 227, length 4096: HTTP, length: 4096HTTP/1.1 200 OKBdpagetype: 1Bdqid: 0x8b3b62c400142f77Cache-Control: privateConnection: keep-aliveContent-Encoding: gzipContent-Type: text/html;charset=utf-8Date: Wed, 09 Dec 2020 14:11:01 GMT...
22:11:01.815214 IP (tos 0x0, ttl 64, id 29829, offset 0, flags [DF], proto TCP (6), length 40)host-46.54550 > 110.242.68.3.http: Flags [.], cksum 0x5f4e (incorrect -> 0xa157), seq 95, ack 4097, win 293, length 0
22:11:01.815222 IP (tos 0x0, ttl 43, id 661, offset 0, flags [DF], proto TCP (6), length 4136)110.242.68.3.http > host-46.54550: Flags [P.], cksum 0x6f4e (incorrect -> 0x07fa), seq 4097:8193, ack 95, win 227, length 4096: HTTP
22:11:01.815236 IP (tos 0x0, ttl 64, id 29830, offset 0, flags [DF], proto TCP (6), length 40)host-46.54550 > 110.242.68.3.http: Flags [.], cksum 0x5f4e (incorrect -> 0x9117), seq 95, ack 8193, win 357, length 0
22:11:01.815243 IP (tos 0x0, ttl 43, id 664, offset 0, flags [DF], proto TCP (6), length 5848)...host-46.54550 > 110.242.68.3.http: Flags [.], cksum 0x5f4e (incorrect -> 0x51ba), seq 95, ack 24165, win 606, length 0
22:11:01.815369 IP (tos 0x0, ttl 64, id 29834, offset 0, flags [DF], proto TCP (6), length 40)host-46.54550 > 110.242.68.3.http: Flags [R.], cksum 0x5f4e (incorrect -> 0x51b6), seq 95, ack 24165, win 606, length 0

通过 lsof 找到进程关联的 TCP 连接,然后使用 ss 或 netstat 查看读写缓冲区。信息如下,recv-q 读缓冲区确实是存在数据。这个缓冲区字节一直未读,直到连接关闭引发了 rst。

$ lsof -p 54330
COMMAND   PID USER   FD      TYPE    DEVICE SIZE/OFF       NODE NAME
...
aaa     54330 root    1u      CHR     136,0      0t0          3 /dev/pts/0
aaa     54330 root    2u      CHR     136,0      0t0          3 /dev/pts/0
aaa     54330 root    3u  a_inode      0,10        0       8838 [eventpoll]
aaa     54330 root    4r     FIFO       0,9      0t0  223586913 pipe
aaa     54330 root    5w     FIFO       0,9      0t0  223586913 pipe
aaa     54330 root    6u     IPv4 223596521      0t0        TCP host-46:60626->110.242.68.3:http (ESTABLISHED)$ ss -an|egrep "68.3:80"
State      Recv-Q      Send-Q       Local Address:Port        Peer Address:Port
ESTAB      72480       0            172.16.0.46:60626         110.242.68.3:80

strace 跟踪系统调用

通过系统调用可分析出,貌似只是读取了 header 部分了,还未读到 body 的数据。

[pid  8311] connect(6, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("110.242.68.3")}, 16 <unfinished ...>
[pid 195519] epoll_pwait(3,  <unfinished ...>
[pid  8311] <... connect resumed>)      = -1 EINPROGRESS (操作现在正在进行)
[pid  8311] epoll_ctl(3, EPOLL_CTL_ADD, 6, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=2350546712, u64=140370471714584}} <unfinished ...>
[pid 195519] getsockopt(6, SOL_SOCKET, SO_ERROR,  <unfinished ...>
[pid 192592] nanosleep({tv_sec=0, tv_nsec=20000},  <unfinished ...>
[pid 195519] getpeername(6, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("110.242.68.3")}, [112->16]) = 0
[pid 195519] getsockname(6,  <unfinished ...>
[pid 195519] <... getsockname resumed>{sa_family=AF_INET, sin_port=htons(47746), sin_addr=inet_addr("172.16.0.46")}, [112->16]) = 0
[pid 195519] setsockopt(6, SOL_TCP, TCP_KEEPINTVL, [15], 4) = 0
[pid 195519] setsockopt(6, SOL_TCP, TCP_KEEPIDLE, [15], 4 <unfinished ...>
[pid  8311] write(6, "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip\r\n\r\n", 94 <unfinished ...>
[pid 192595] read(6,  <unfinished ...>
[pid 192595] <... read resumed>"HTTP/1.1 200 OK\r\nBdpagetype: 1\r\nBdqid: 0xc43c9f460008101b\r\nCache-Control: private\r\nConnection: keep-alive\r\nContent-Encoding: gzip\r\nContent-Type: text/html;charset=utf-8\r\nDate: Wed, 09 Dec 2020 13:46:30 GMT\r\nExpires: Wed, 09 Dec 2020 13:45:33 GMT\r\nP3p: CP=\" OTI DSP COR IVA OUR IND COM \"\r\nP3p: CP=\" OTI DSP COR IVA OUR IND COM \"\r\nServer: BWS/1.1\r\nSet-Cookie: BAIDUID=996EE645C83622DF7343923BF96EA1A1:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nSet-Cookie: BIDUPSID=99"..., 4096) = 4096
[pid 192595] close(6 <unfinished ...>

逻辑代码

那么到这里,可以大概猜测问题所在,找到业务方涉及到 httpclient 的逻辑代码。伪代码如下,跟上面的结论一样,只是读取了header,但并未读取完response body数据。

还以为是特殊的场景,结果是使用不当,把请求投递过去后只判断 http code?真正的业务 code 是在 body 里的。????

urls := []string{...}
for _, url := range urls {resp, err := http.Post(url, ...)if err != nil {// ...}if resp.StatusCode == http.StatusOK {continue}// handle redis cache// handle mongodb// handle rocketmq// ...resp.Body.Close()
}

如何解决

不细说了,把 header length 长度的数据读完就可以了。

分析问题

先不管别人使用不当,再分析下为何出现短连接,连接不能复用的问题。

为什么不读取 body 就出问题?其实 http.Response 字段描述中已经有说明了。当 Body 未读完时,连接可能不能复用。

 // The http Client and Transport guarantee that Body is always// non-nil, even on responses without a body or responses with// a zero-length body. It is the caller's responsibility to// close Body. The default HTTP client's Transport may not// reuse HTTP/1.x "keep-alive" TCP connections if the Body is// not read to completion and closed.//// The Body is automatically dechunked if the server replied// with a "chunked" Transfer-Encoding.//// As of Go 1.12, the Body will also implement io.Writer// on a successful "101 Switching Protocols" response,// as used by WebSockets and HTTP/2's "h2c" mode.Body io.ReadCloser

众所周知,golang httpclient 要注意 response Body 关闭问题,但上面的 case 确实有关了 body,只是非常规地没去读取 reponse body 数据。这样会造成连接异常关闭,继而引起连接池不能复用。

一般 http 协议解释器是要先解析 header,再解析 body,结合当前的问题开始是这么推测的,连接的 readLoop 收到一个新请求,然后尝试解析 header 后,返回给调用方等待读取 body,但调用方没去读取,而选择了直接关闭 body。那么后面当一个新请求被 transport roundTrip 再调度请求时,readLoop 的 header 读取和解析会失败,因为他的读缓冲区里有前面未读的数据,必然无法解析 header。按照常见的网络编程原则,协议解析失败,直接关闭连接。

想是这么想的,但还是看了 golang net/http 的代码,结果不是这样的。????

分析源码

httpclient 每个连接会创建读写协程两个协程,分别使用 reqch 和 writech 来跟 roundTrip 通信。上层使用的response.Body 其实是经过多次封装的,一次封装的 body 是直接跟 net.conn 进行交互读取,二次封装的 body 则是加强了 close 和 eof 处理的 bodyEOFSignal。

当未读取 body 就进行 close 时,会触发 earlyCloseFn() 回调,看 earlyCloseFn 的函数定义,在 close 未见 io.EOF 时才调用。自定义的 earlyCloseFn 方法会给 readLoop 监听的 waitForBodyRead 传入 false,  这样引发 alive 为 false 不能继续循环的接收新请求,只能是退出调用注册过的 defer 方法,关闭连接和清理连接池。

// xiaorui.ccfunc (pc *persistConn) readLoop() {closeErr := errReadLoopExiting // default value, if not changed belowdefer func() {pc.close(closeErr)      // 关闭连接pc.t.removeIdleConn(pc) // 从连接池中删除}()...alive := truefor alive {...rc := <-pc.reqch  // 从管道中拿到请求,roundTrip 对该管道进行输入trace := httptrace.ContextClientTrace(rc.req.Context())var resp *Responseif err == nil {resp, err = pc.readResponse(rc, trace) // 更多的是解析 header} else {err = transportReadFromServerError{err}closeErr = err}...waitForBodyRead := make(chan bool, 2)body := &bodyEOFSignal{body: resp.Body,// 提前关闭 !!! 输出falseearlyCloseFn: func() error {waitForBodyRead <- false...},// 正常收尾 !!!fn: func(err error) error {isEOF := err == io.EOFwaitForBodyRead <- isEOF...},}resp.Body = bodyselect {case rc.ch <- responseAndError{res: resp}:case <-rc.callerGone:return}select {case bodyEOF := <-waitForBodyRead:replaced := pc.t.replaceReqCanceler(rc.cancelKey, nil) // before pc might return to idle pool// alive 为 false, 不能继续 continuealive = alive &&bodyEOF &&!pc.sawEOF &&pc.wroteRequest() &&replaced && tryPutIdleConn(trace)...case <-rc.req.Cancel:alive = falsepc.t.CancelRequest(rc.req)case <-rc.req.Context().Done():alive = falsepc.t.cancelRequest(rc.cancelKey, rc.req.Context().Err())case <-pc.closech:alive = false}}
}

bodyEOFSignal 的 Close():

// xiaorui.ccfunc (es *bodyEOFSignal) Close() error {es.mu.Lock()defer es.mu.Unlock()if es.closed {return nil}es.closed = trueif es.earlyCloseFn != nil && es.rerr != io.EOF {return es.earlyCloseFn()}err := es.body.Close()return es.condfn(err)
}

最终会调用 persistConn 的 close(), 连接关闭并关闭closech:

// xiaorui.ccfunc (pc *persistConn) close(err error) {pc.mu.Lock()defer pc.mu.Unlock()pc.closeLocked(err)
}func (pc *persistConn) closeLocked(err error) {if err == nil {panic("nil error")}pc.broken = trueif pc.closed == nil {pc.closed = errpc.t.decConnsPerHost(pc.cacheKey)if pc.alt == nil {if err != errCallerOwnsConn {pc.conn.Close() // 关闭连接}close(pc.closech) // 通知读写协程}}
}

总之

同事的 httpclient 使用方法有些奇怪,除了 head method 之外,还真想不到有不读取 body 的请求。所以,大家知道 httpclient 有这么一回事就行了。

另外,一直觉得 net/http 的代码太绕,没看过一些介绍直接看代码很容易陷进去,有时间专门讲讲 http client 的实现。

Go http client 连接池不复用的问题相关推荐

  1. SqlConnection调用Dispose方法之后还可以在连接池中复用吗?

    在上一篇中简单讨论了SqlConnection的正确使用方式,顺带浅谈了一下连接池,不小心带出了一个问题:SqlConnection的Close和Dispose方法执行之后,该连接对象是不是真的放回到 ...

  2. aiohttp mysql_aiohttp 怎么复用连接池

    最近,在看 python 的异步编程( asyncio )部分,在使用 aiomysql 的时候遇到了困难,已经困惑我两三天了.可能是自己资质愚钝,看了 aiomysql 的官网例子( https:/ ...

  3. mysql清理连接数缓存,MySQL连接池、线程缓存、线程池的区别

    1. MySQL连接池 连接池通常实现在client端,是指应用(客户端)预先创建一定的连接,利用这些连接服务于客户端所有的DB请求.如果某一个时刻,空闲的连接数小于DB的请求数,则需要将请求排队,等 ...

  4. 连接池:别让连接池帮了倒忙

    今天,我再与你说说另一种很重要的池化技术,即连接池. 我先和你说说连接池的结构.连接池一般对外提供获得连接.归还连接的接口给客户端使用,并暴露最小空闲连接数.最大连接数等可配置参数,在内部则实现连接建 ...

  5. Apache HttpClient连接池泄露问题排查

    Apache HttpClient连接池泄露问题排查 问题背景 业务系统主要的业务是一个数据聚合管理平台,其中系统有一个功能是同步所有资源(简称 大同步) 业务同步数据请求数据工具是适配 Apache ...

  6. OkHttp3——连接池

    连接池 每次Request都创建新的Http连接,容易浪费资源和时间,TCP3次握手,断开连接要2次或4次挥手.Http1.0中有Keep-Alive用来保持连接,在一定时间范围内,相同的请求复用旧连 ...

  7. nginx upstream中长连接池的维护

    nginx中的长连接分为: 下游客户端和nginx的长连接 nginx反向代理中和上游服务器之间的长连接 upstream中的长连接池 当nginx反向代理请求上游服务器时,第一次时会建立TCP连接, ...

  8. rabbmitmq连接池[已过生产]

    源码地址https://gitee.com/tym_hmm/rabbitmq-pool-go rabbitmq 连接池channel复用 开发语言 golang 依赖库 go get -u gitee ...

  9. 基于 Netty 如何实现高性能的 HTTP Client 的连接池

    使用netty作为http的客户端,pool又该如何进行设计.本文将会进行详细的描述. 1. 复用类型的选型 1.1 channel 复用 多个请求可以共用一个channel 模型如下: 模型 特点: ...

最新文章

  1. UWP 图片剪切旋转工具
  2. Java 面试之技术框架
  3. HTM服务器l控件与WEB服务器控件(一)
  4. Introduction to Computer Networking学习笔记(五):ARP协议(Address Resolution Protocol)
  5. 安装terrasolid模块的“setup.exe”弹窗setup.inf not found
  6. onshape 做参考面等虚拟几何的装配和原点定位
  7. 电阻(1)电阻种类篇
  8. python的对文档密码的简单破解
  9. 耐得住寂寞方能不寂寞
  10. 【diskgenius】【Error on partition resizing.(2000011a)Out of disk space.】【The partition(or volume)“PART
  11. 解决win10注册错误 错误代码0x8002801c
  12. 我们如何在Linkerd 2.2里设计重试 1
  13. 网站流量日志数据分析系统(1)
  14. (闲杂笔记1) 控件尺寸与像素关系
  15. HADOOP HA之NameNode HA集群配置与应用
  16. ArcGIS Server 注册私有云存储并发布影像切片服务
  17. 不只是电商,苏宁打通全供应链的野望
  18. 【机器学习sklearn】决策树(Decision Tree)算法
  19. 安易硬盘数据恢复软件v8.81官方版
  20. 安全购买数码相机七大步骤[荐]

热门文章

  1. SpringBoot项目使用微服务后在Service窗口启动应用后不显示端口号
  2. liunxC下零碎知识点的总结
  3. 什么?ES6 中还有 Tail Calls!
  4. webservice linux 杀进程
  5. jquery模糊查询
  6. Python-crawler-citeulike
  7. 网络排错模型之我见----模型,基线,协议,数据包
  8. 对联一副,勉励奋斗在网络事业上的兄弟们
  9. vs.net 2005 中自定义模版项
  10. 2021牛客多校7 - xay loves trees(dfs序+主席树-标记永久化)