使用gorequest在Golang中重用相同的TCP连接

时间:2016-12-18 18:32:12

标签: go net-http gorequest

这里我尝试使用gorequest将20个文档发布到我的web服务,但它创建了20个不同的TCP连接。

如何确保它只使用一个TCP连接而不是20个连接或重复使用相同的连接。

代码段:

request := gorequest.New()
for i := 1; i <= 20; i++ {
    jsonH := `{"hostname": "node1"}`
    req := request.Post(url).
        Set("Content-Type", "application/json").
        Set("Authorization", fmt.Sprintf("Bearer %s", RWToken)).
        Send(jsonH)

    resp, _, _ := req.End()
    fmt.Printf("%T", req)

    logs.Info(resp.StatusCode)
}

当我做netstat -an | grep 8181

tcp        0      0 192.168.0.1:8181         0.0.0.0:*                   LISTEN
tcp        0      0 192.168.0.1:8181         192.168.0.1:60929        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60928        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60945        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60944        TIME_WAIT
tcp        0      0 192.168.0.1:60927        192.168.0.1:8181         TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60946        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60943        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60947        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60934        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60936        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60935        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60940        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60932        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60939        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60941        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60930        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60937        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60933        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60938        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60931        TIME_WAIT
tcp        0      0 192.168.0.1:8181         192.168.0.1:60942        TIME_WAIT

0 个答案:

没有答案