FAQ
I am using http.Get to fetch content. But when server doesn't set header
"Connection: keep-alive", many connections will be in CLOSE_WAIT state.

Then I discover the Request object returned by http.NewRequest has
attribute Close which is *false* by default. If I create Request object
manually, set Close = true and http.Do the request, everything is fine.

I think the default behavior of http.Get is not considering the situation
that server doesn't set keep-alive.

Is there any misunderstanding or mistake I have? Please point it out. Thank
you.


PS:

*source code:*

package main

import (
"net/http"
"time"
"fmt"
)


func main() {
for {
resp, err := http.Get("http://www.python.org")
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
time.Sleep(100 * time.Millisecond)
}
}

*shell command to observe:*
netstat -nt | grep 82.94 | grep CLOSE_WAIT | wc -l

(www.python.org is resolved as 82.94.164.162 here)

*python.org header*
curl -I "http://www.python.org"
HTTP/1.1 200 OK
Date: Wed, 12 Dec 2012 05:30:31 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Tue, 11 Dec 2012 18:11:29 GMT
ETag: "105800d-4ebf-4d0979c5fb640"
Accept-Ranges: bytes
Content-Length: 20159
Vary: Accept-Encoding
Content-Type: text/html

--

Search Discussions

  • Dave Cheney at Dec 12, 2012 at 2:09 pm
    The defer does not run until the enclosing function returns. In the case of
    main, that is never. I think if you close the response body explicitly,
    this will solve your problem.

    Dave
    On 13 Dec 2012 01:06, wrote:

    I am using http.Get to fetch content. But when server doesn't set header
    "Connection: keep-alive", many connections will be in CLOSE_WAIT state.

    Then I discover the Request object returned by http.NewRequest has
    attribute Close which is *false* by default. If I create Request object
    manually, set Close = true and http.Do the request, everything is fine.

    I think the default behavior of http.Get is not considering the situation
    that server doesn't set keep-alive.

    Is there any misunderstanding or mistake I have? Please point it out.
    Thank you.


    PS:

    *source code:*

    package main

    import (
    "net/http"
    "time"
    "fmt"
    )


    func main() {
    for {
    resp, err := http.Get("http://www.python.org")
    if err != nil {
    fmt.Println(err)
    }
    defer resp.Body.Close()
    time.Sleep(100 * time.Millisecond)
    }
    }

    *shell command to observe:*
    netstat -nt | grep 82.94 | grep CLOSE_WAIT | wc -l

    (www.python.org is resolved as 82.94.164.162 here)

    *python.org header*
    curl -I "http://www.python.org"
    HTTP/1.1 200 OK
    Date: Wed, 12 Dec 2012 05:30:31 GMT
    Server: Apache/2.2.16 (Debian)
    Last-Modified: Tue, 11 Dec 2012 18:11:29 GMT
    ETag: "105800d-4ebf-4d0979c5fb640"
    Accept-Ranges: bytes
    Content-Length: 20159
    Vary: Accept-Encoding
    Content-Type: text/html

    --

    --
  • Chris dollin at Dec 12, 2012 at 2:09 pm

    On 12 December 2012 05:31, wrote:
    I am using http.Get to fetch content. But when server doesn't set header
    "Connection: keep-alive", many connections will be in CLOSE_WAIT state.

    Then I discover the Request object returned by http.NewRequest has attribute
    Close which is *false* by default. If I create Request object manually, set
    Close = true and http.Do the request, everything is fine.

    I think the default behavior of http.Get is not considering the situation
    that server doesn't set keep-alive.

    Is there any misunderstanding or mistake I have? Please point it out. Thank
    you.

    source code:

    package main

    import (
    "net/http"
    "time"
    "fmt"
    )


    func main() {
    for {
    resp, err := http.Get("http://www.python.org")
    if err != nil {
    fmt.Println(err)
    }
    defer resp.Body.Close()
    time.Sleep(100 * time.Millisecond)
    }
    }
    Note that your deferred Close()s don't run until main exits.
    Might that be the problem? If so, the fix is straightforward.

    Chris

    --
    Chris "allusive" Dollin

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 12, '12 at 2:06p
activeDec 12, '12 at 2:09p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase