0

Here I am trying to POST 20 docs to my webservice using gorequest, but it creates 20 different TCP connection.

How can I make sure it use only one TCP connection instead of 20 or reuse the same connection.

Snippets:

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)
}

When i do 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
James Sapam
  • 16,036
  • 12
  • 50
  • 73
  • 1
    The underlying Transport should try to reuse connections when possible. Are you using a current version of Go? Does the same thing happen if you use only a standard `http.Client`? – JimB Dec 18 '16 at 19:24
  • I am using go version 1.7, ya it happens the same when i used http.Client. – James Sapam Dec 18 '16 at 20:01
  • Are you certain the server isn't closing the connection or sending a `Connection: close` header? Is it responding with http/1.1? Does it work with another client like curl? – JimB Dec 18 '16 at 20:08
  • Sever is closing the conneciton after sometime, but when i do `netstat -an | grep PORT` show up all the connection. Ya it return `Proto:"HTTP/1.1.` – James Sapam Dec 18 '16 at 20:23
  • Yes i tried curl and it create new TCP connection for every curl. – James Sapam Dec 18 '16 at 20:34
  • 2
    It's a bug in Gorequest where it creates a new Transport for every request. See the open issue here https://github.com/parnurzeal/gorequest/issues/75#issuecomment-231979117 – Nadh Dec 19 '16 at 07:12
  • @James: First, I would reevaluate your choice of using `gorequest` at all (if it can't handle a basic concept of not discarding Transports and leaking connections, I question the overall quality), since the http package itself is not difficult to use. If you can't solve your issue that way, we need a [mcve] to try and diagnose the actual problem here. – JimB Dec 19 '16 at 13:46
  • @nadh ya it works for sequential post after setting `request.Transport.DisableKeepAlives = false` but not for parallel goroutine. I need to check out more. – James Sapam Dec 19 '16 at 18:20
  • @JimB Thank you so much for your effort and tips. Let me try using native net/http library and will let you know. – James Sapam Dec 19 '16 at 18:21

0 Answers0