This is a creation in Article, where the information may have evolved or changed.
Objective
About Time_wait Online Introduction There are many, I do not wordy, before the company environment has a large number of machines in the time_wait, the study of the Golang and the basis of HTTP, summary under.
HTTP protocol 1.1 defaults to a long connection, if a client uses the HTTP1.1 protocol, but does not want to use long links, you need to indicate in the header that the value of connection is close, and if the server side does not want to support long links, it is also necessary to explicitly describe con in response The value of Nection is close.
Get/http/1.1Host:example.Comuser-agent: Go1.1Package HTTP**Connection:close**Accept-encoding: gziphttp/1.1 501 notImplementedcontent-type: text/htmlcontent-length:357**Connection:close**Date: Mon, -Mar . -: the: AboutGMTSERVER:ECSF (iad/ -D2)
Some treatment of long-length connection in Golang
1) Golang Forced short Connection
Out in the header of the request plus connection:close , you can also set the request struct Close member variable to true , for example:
_:= http.NewRequest("Get""http://example.com"nil)req.Closetrue
2) Golang Requirements for long connections
Golang client does not set active disconnection, but also pay attention to, want to maintain a long connection, to ensure that the following two points
1. defer resp.Body.Close() //别忘了close body,不然长连接保持不了
2. body, err := ioutil.ReadAll(resp.Body)//记得读完resp.body或者放置一个结束符号 io.Copy(ioutil.Discard,resp.Body)
3) solve the problem of generating a large number of close_wait
Solution online Quite a lot, to summarize a more appropriate solution, and then fill in