URL监控在netstat中产生多个已建立(连接)的条目。

huangapple go评论80阅读模式
英文:

URL monitor produces multiple ESTABLISHED (connection) entries in netstat

问题

我用Go语言编写了一个URL监控程序,但是在一段时间后,我发现netstat -nao|grep 80中有很多ESTABLISHED的条目。

getHttpStatusCode函数:

    HttpClient = &http.Client{
        Transport: &http.Transport{
            Dial: func(netw, addr string) (net.Conn, error) {
                deadline := time.Now().Add(30 * time.Second)
                c, err := net.DialTimeout(netw, addr, 20*time.Second)
                if err != nil {
                    return nil, err
                }

                c.SetDeadline(deadline)
                c.SetReadDeadline(deadline)
                c.SetWriteDeadline(deadline)
                return c, nil
            },
        },
    }

// ...

func getHttpStatusCode(url string) int {
    if url == "" {
        return 200
    }

    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return 0
    }

    req.Close = true
    req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17")
    resp, err := HttpClient.Do(req)
    if err != nil {
        return 0
    }

    defer resp.Body.Close()
    return resp.StatusCode
}

我查看了Go的手册,没有找到类似req.Close()的东西,只有defer resp.Body.Close()。

这是netstat -nao | grep 80的输出:

> tcp 1343352 0 192.168.2.33:29581 220.181.155.19:80 ESTABLISHED off (0.00/0/0)

以及tcpdump tcp port 80的输出:

14:32:54.085095 IP 113.12.80.13.http > wk_0_mysql.KIDC90805.zw.39174: Flags [.], seq 17376:18824, ack 1, win 42, options [nop,nop,TS val 4236145017 ecr 204896351], length 1448
14:32:54.109206 IP wk_0_mysql.KIDC90805.zw.25834 > 220.181.90.8.http: Flags [S], seq 714805337, win 14600, options [mss 1460,sackOK,TS val 204896416 ecr 0,nop,wscale 9], length 0
14:32:54.223349 IP 220.181.155.22.http > wk_0_mysql.KIDC90805.zw.19262: Flags [.], seq 864939135:864940583, ack 1630899997, win 42, options [nop,nop,TS val 1570834172 ecr 204896529], length 1448
14:32:54.223352 IP wk_0_mysql.KIDC90805.zw.19262 > 220.181.155.22.http: Flags [.], ack 1448, win 1301, options [nop,nop,TS val 204896530 ecr 1570834172], length 0
14:32:54.223432 IP 220.181.155.10.http > wk_0_mysql.KIDC90805.zw.27376: Flags [.], seq 3889371684:3889373132, ack 1106685068, win 42, options [nop,nop,TS val 3866364254 ecr 204896529], length 1448
14:32:54.223436 IP wk_0_mysql.KIDC90805.zw.27376 > 220.181.155.10.http: Flags [.], ack 1448, win 594, options [nop,nop,TS val 204896530 ecr 3866364254], length 0
14:32:54.275774 IP 121.12.101.130.http > wk_0_mysql.KIDC90805.zw.63329: Flags [.], seq 1314475629:1314477089, ack 642951590, win 54, length 1460
英文:

I wrote a URL monitor program in Go, but after a period of time I found many ESTABLISHED entries in netstat -nao|grep 80.

The getHttpStatusCode func:

    HttpClient = &http.Client{
        Transport: &http.Transport{
            Dial: func(netw, addr string) (net.Conn, error) {
                deadline := time.Now().Add(30 * time.Second)
                c, err := net.DialTimeout(netw, addr, 20*time.Second)
                if err != nil {
                    return nil, err
                }

                c.SetDeadline(deadline)
                c.SetReadDeadline(deadline)
                c.SetWriteDeadline(deadline)
                return c, nil
            },
        },
    }

// ...

func getHttpStatusCode(url string) int {
    if url == "" {
        return 200
    }

    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return 0
    }

    req.Close = true
    req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17")
    resp, err := HttpClient.Do(req)
    if err != nil {
        return 0
    }

    defer resp.Body.Close()
    return resp.StatusCode
}

I checked the Go manual and don't find something like req.Close() and just defer resp.Body.Close().

Here is the output of netstat -nao | grep 80:

> tcp 1343352 0 192.168.2.33:29581 220.181.155.19:80 ESTABLISHED off (0.00/0/0)

And the output of tcpdump tcp port 80:

14:32:54.085095 IP 113.12.80.13.http > wk_0_mysql.KIDC90805.zw.39174: Flags [.], seq 17376:18824, ack 1, win 42, options [nop,nop,TS val 4236145017 ecr 204896351], length 1448
14:32:54.109206 IP wk_0_mysql.KIDC90805.zw.25834 > 220.181.90.8.http: Flags [S], seq 714805337, win 14600, options [mss 1460,sackOK,TS val 204896416 ecr 0,nop,wscale 9], length 0
14:32:54.223349 IP 220.181.155.22.http > wk_0_mysql.KIDC90805.zw.19262: Flags [.], seq 864939135:864940583, ack 1630899997, win 42, options [nop,nop,TS val 1570834172 ecr 204896529], length 1448
14:32:54.223352 IP wk_0_mysql.KIDC90805.zw.19262 > 220.181.155.22.http: Flags [.], ack 1448, win 1301, options [nop,nop,TS val 204896530 ecr 1570834172], length 0
14:32:54.223432 IP 220.181.155.10.http > wk_0_mysql.KIDC90805.zw.27376: Flags [.], seq 3889371684:3889373132, ack 1106685068, win 42, options [nop,nop,TS val 3866364254 ecr 204896529], length 1448
14:32:54.223436 IP wk_0_mysql.KIDC90805.zw.27376 > 220.181.155.10.http: Flags [.], ack 1448, win 594, options [nop,nop,TS val 204896530 ecr 3866364254], length 0
14:32:54.275774 IP 121.12.101.130.http > wk_0_mysql.KIDC90805.zw.63329: Flags [.], seq 1314475629:1314477089, ack 642951590, win 54, length 1460

答案1

得分: 8

HTTP客户端默认会使用持久连接,您可以通过调用transport.CloseIdleConnections来关闭它们(参见文档)。

我不确定req.Close在客户端请求上是否起作用,它可能只适用于服务器。

英文:

The HTTP client will by default use keep-alive connections, you can close them by calling transport.CloseIdleConnections (from the docs).

I'm not sure req.Close does anything on client requests, it might only be for the server.

huangapple
  • 本文由 发表于 2013年3月24日 14:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/15595858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定