Golang long-Length connection treatment

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.