当前位置 博文首页 > 文章内容

    go中的一个网络重连复用

    作者: 栏目:未分类 时间:2020-06-30 10:04:31

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    // Body represents the response body. // // The response body is streamed on demand as the Body field // is read. If the network connection fails or the server // terminates the response, Body.Read calls return an error. // // 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.

     

    // The default HTTP client's Transport does not // attempt to reuse HTTP/1.0 or HTTP/1.1 TCP connections // ("keep-alive") unless the Body is read to completion and is // closed.

     

    大家看到里面的话,只有当 body 读取并关闭,而我上面的代码只是关闭了,Body 并没有读取。所以导致了 client 没有 reuse TCP connection。

    所以这个完全明白了,我们必须在关闭之前完全的读取 Body 里面的数据,我把原来的代码改成了下面之后就解决了问题

     

    重用

    package main
    
    import (
        "io"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
        count := 100
        for i := 0; i < count; i++ {
            resp, err := http.Get("https://www.oschina.net")
            if err != nil {
                panic(err)
            }
    
            io.Copy(ioutil.Discard, resp.Body)
            resp.Body.Close()
        }
    }

     

     

    不重用

    package main
    
    import (
        "io"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
        count := 100
        for i := 0; i < count; i++ {
            resp, err := http.Get("https://www.oschina.net")
            if err != nil {
                panic(err)
            }
    
            //io.Copy(ioutil.Discard, resp.Body)
            resp.Body.Close()
        }
    }

     

     

     

    本人也亲测证明了这一点

     

    https://gocn.vip/topics/10626

    https://gocn.vip/topics/10626