如果给公司做网站,做决定的网站,2万元建设网站贵吗,网络优化工具app手机版最近部署的web程序#xff0c;服务器上出现不少time_wait的tcp连接状态#xff0c;占用了tcp端口#xff0c;花费几天时间排查。之前我有结论#xff1a;HTTP keep-alive 是在应用层对TCP连接的滑动续约复用#xff0c;如果客户端、服务器稳定续约#xff0c;就成了名副其… 最近部署的web程序服务器上出现不少time_wait的tcp连接状态占用了tcp端口花费几天时间排查。 之前我有结论HTTP keep-alive 是在应用层对TCP连接的滑动续约复用如果客户端、服务器稳定续约就成了名副其实的长连接。有关[Http持久连接]的一切卷给你看HTTP1.1 Keep-Alive到底算不算长连接目前所有的HTTP网络库(不论是客户端、服务端)都默认开启了HTTP Keep-Alive通过Request/Response的Connection标头来协商复用连接。01非常规的行为形成的短连接 我手上有个项目由于历史原因客户端禁用了Keep-Alive服务端默认开启了Keep-Alive如此一来协商复用连接失败 客户端每次请求会使用新的TCP连接 也就是回退为短连接。客户端强制禁用Keep-Alivepackage main
import (fmtio/ioutillognet/httptime
)func main() {tr : http.Transport{DisableKeepAlives: true,}client : http.Client{Timeout: 10 * time.Second,Transport: tr,}for {requestWithClose(client)time.Sleep(time.Second * 1)}
}func requestWithClose(client *http.Client) {resp, err : client.Get(http://10.100.219.9:8081)if err ! nil {fmt.Printf(error occurred while fetching page, error: %s, err.Error())return}defer resp.Body.Close()c, err : ioutil.ReadAll(resp.Body)if err ! nil {log.Fatalf(Couldnt parse response body. %v, err)}fmt.Println(string(c))
}web服务端默认开启Keep-Alivepackage mainimport (fmtlognet/http
)// 根据RemoteAddr 知道客户端使用的持久连接
func IndexHandler(w http.ResponseWriter, r *http.Request) {fmt.Println(receive a request from:, r.RemoteAddr, r.Header)w.Write([]byte(ok))
}func main() {fmt.Printf(Starting server at port 8081\n)// net/http 默认开启持久连接if err : http.ListenAndServe(:8081, http.HandlerFunc(IndexHandler)); err ! nil {log.Fatal(err)}
}从服务端的日志看确实是短连接。receive a request from: 10.22.34.48:54722 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54724 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54726 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54728 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54731 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54733 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54734 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54738 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54740 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54741 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54743 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54744 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]
receive a request from: 10.22.34.48:54746 map[Accept-Encoding:[gzip] Connection:[close] User-Agent:[Go-http-client/1.1]]02谁是主动断开方我想当然的以为 客户端是主动断开方被现实啪啪打脸。某一天服务器上超过300的time_wait报警告诉我这tmd是服务器主动终断连接。常规的TCP4次挥手 主动断开方会进入time_wait状态等待2MSL后释放占用的SOCKET以下是从服务器上tcpdump抓取的tcp连接信息。2,3红框显示 Server端先发起TCP的FIN消息, 之后Client回应ACK确认收到Server的关闭通知 之后Client再发FIN消息告知现在可以关闭了 Server端最后发ACK确认收到并进入time_wait状态等待2MSL的时间关闭Socket。特意指出红框1表示TCP双端同时关闭[1]此时会在Client,Server同时留下time_wait痕迹发生概率较小。03没有源码说个串串此种情况是服务端主动关闭我们翻一翻golang httpServer的源码•http.ListenAndServe(:8081)•server.ListenAndServe()•srv.Serve(ln)•go c.serve(connCtx) 使用go协程来处理每个请求服务器连接处理请求的简略源码如下func (c *conn) serve(ctx context.Context) {c.remoteAddr c.rwc.RemoteAddr().String()ctx context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())defer func() {if !c.hijacked() {c.close() // go协程conn处理请求的协程退出时主动关闭底层的TCP连接c.setState(c.rwc, StateClosed, runHooks)}}()......// HTTP/1.x from here on.ctx, cancelCtx : context.WithCancel(ctx)c.cancelCtx cancelCtxdefer cancelCtx()c.r connReader{conn: c}c.bufr newBufioReader(c.r)c.bufw newBufioWriterSize(checkConnErrorWriter{c}, 410)for {w, err : c.readRequest(ctx)..... serverHandler{c.server}.ServeHTTP(w, w.req)w.cancelCtx()if c.hijacked() {return}w.finishRequest()if !w.shouldReuseConnection() {if w.requestBodyLimitHit || w.closedRequestBodyEarly() {c.closeWriteAndWait()}return}c.setState(c.rwc, StateIdle, runHooks)c.curReq.Store((*response)(nil))if !w.conn.server.doKeepAlives() {// Were in shutdown mode. We mightve replied// to the user without Connection: close and// they might think they can send another// request, but such is life with HTTP/1.1.return}if d : c.server.idleTimeout(); d ! 0 {c.rwc.SetReadDeadline(time.Now().Add(d))if _, err : c.bufr.Peek(4); err ! nil {return}}c.rwc.SetReadDeadline(time.Time{})}
}我们需要关注①for循环表示尝试复用该conn用于处理迎面而来的请求②w.shouldReuseConnection() false 表明读取到ClientConnectionClose标头设置closeAfterReplytrue跳出for循环协程即将结束结束之前执行defer函数defer函数内close该连接 c.close()......// Close the connection.func (c *conn) close() { c.finalFlush() c.rwc.Close()}③如果 w.shouldReuseConnection() true则将该连接状态置为idle 并继续走for循环复用连接处理后续请求。04我的收获1. TCP 4次挥手的八股文2. 短连接的效应主动关闭方会在机器上产生 time_wait状态需要等待2MSL时间才会关闭SOCKET3.golang http keep-alive复用tcp连接的源码级分析4.tcpdump抓包的姿势引用链接[1] TCP双端同时关闭: https://blog.csdn.net/q1007729991/article/details/69950255有态度的马甲建立了真● 高质量交流群大佬汇聚、无事静默、有事激活、深度思考。[长按图片加我好友]年终总结2021技术文大盘点 | 打包过去面向未来项目总结麻雀虽小五脏俱全理念总结实话实说只会.NET会让我们一直处于鄙视链、食物链的下游云原生系列 什么是云原生点“赞”戳“在看”体现态度很有必要!