FAQ
Hi I have a question about how to close an http connection explicitly?

My program is a server providing REST API and clients calls these API using
golang net.http package.

The symptom is that I can observe in the server side /proc/<process_id>/fd
directory, the socket handlers are increasing as the clients issues API
calls, even the call is finished, the SOCKET fd will still be there. Thus
the max limit will be reached before too long. I checked the client side
code the resp is closed after each call as follows:

esp, err := http.Get("http://example.com/")
if err != nil {
  // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...

My question is that if there is a way for the server to force to close the
http connection to release the socket handlers?

thanks!
Wei


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Islan Dberry at Oct 23, 2013 at 9:32 pm
    Set the ReadTimeout (http://golang.org/pkg/net/http/#Server).
    On Wednesday, October 23, 2013 12:49:18 PM UTC-7, Wei Tang wrote:

    Hi I have a question about how to close an http connection explicitly?

    My program is a server providing REST API and clients calls these API
    using golang net.http package.

    The symptom is that I can observe in the server side /proc/<process_id>/fd
    directory, the socket handlers are increasing as the clients issues API
    calls, even the call is finished, the SOCKET fd will still be there. Thus
    the max limit will be reached before too long. I checked the client side
    code the resp is closed after each call as follows:

    esp, err := http.Get("http://example.com/")
    if err != nil {
    // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    // ...

    My question is that if there is a way for the server to force to close the
    http connection to release the socket handlers?

    thanks!
    Wei

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Specode at Oct 24, 2013 at 3:56 am
    On HTTP 1.1, Any request for the client will have keep-alive, If you want
    to set a keep-alive timeout (server side) :

    s := &http.Server{
      Addr: ":80",
      ReadTimeout: 10 * time.Second, // this is timeout
    }

    log.Fatal(s.ListenAndServe())


    if you want disable keep-alive:

    tr := &http.Transport{
       DisableKeepAlives: true,
    }
    c := &http.Client{ Transport: tr }
    resp, err := c.Get("https://www.google.com")

    On Thursday, October 24, 2013 3:49:18 AM UTC+8, Wei Tang wrote:

    Hi I have a question about how to close an http connection explicitly?

    My program is a server providing REST API and clients calls these API
    using golang net.http package.

    The symptom is that I can observe in the server side /proc/<process_id>/fd
    directory, the socket handlers are increasing as the clients issues API
    calls, even the call is finished, the SOCKET fd will still be there. Thus
    the max limit will be reached before too long. I checked the client side
    code the resp is closed after each call as follows:

    esp, err := http.Get("http://example.com/")
    if err != nil {
    // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    // ...

    My question is that if there is a way for the server to force to close the
    http connection to release the socket handlers?

    thanks!
    Wei

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Wei Tang at Oct 24, 2013 at 3:04 pm
    Thanks Specod and Islan for the reply.

    I tried the second option of Specod which disconnect the connection from
    client side, it works.

    As for setting ReadTimeout, it seems not suitable for my use case where big
    data downloading requires a unpredictable long time.

    Thanks a lot for your help!
    Wei


    On Wed, Oct 23, 2013 at 10:56 PM, Specode wrote:

    On HTTP 1.1, Any request for the client will have keep-alive, If you want
    to set a keep-alive timeout (server side) :

    s := &http.Server{
    Addr: ":80",
    ReadTimeout: 10 * time.Second, // this is timeout
    }

    log.Fatal(s.ListenAndServe())


    if you want disable keep-alive:

    tr := &http.Transport{
    DisableKeepAlives: true,
    }
    c := &http.Client{ Transport: tr }
    resp, err := c.Get("https://www.google.com")

    On Thursday, October 24, 2013 3:49:18 AM UTC+8, Wei Tang wrote:

    Hi I have a question about how to close an http connection explicitly?

    My program is a server providing REST API and clients calls these API
    using golang net.http package.

    The symptom is that I can observe in the server side
    /proc/<process_id>/fd directory, the socket handlers are increasing as the
    clients issues API calls, even the call is finished, the SOCKET fd will
    still be there. Thus the max limit will be reached before too long. I
    checked the client side code the resp is closed after each call as follows:

    esp, err := http.Get("http://example.com/"**)
    if err != nil {
    // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    // ...

    My question is that if there is a way for the server to force to close
    the http connection to release the socket handlers?

    thanks!
    Wei


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/agB-n6V7UsQ/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jared Bischof at Oct 24, 2013 at 4:05 pm
    For the server side, adding the following to the header of the
    http.ResponseWriter solved the problem for us (I'm working with Wei):

    w.Header().Set("Connection", "close")


    Thanks for the client-side code!

    On Thursday, October 24, 2013 10:04:24 AM UTC-5, Wei Tang wrote:

    Thanks Specod and Islan for the reply.

    I tried the second option of Specod which disconnect the connection from
    client side, it works.

    As for setting ReadTimeout, it seems not suitable for my use case where
    big data downloading requires a unpredictable long time.

    Thanks a lot for your help!
    Wei



    On Wed, Oct 23, 2013 at 10:56 PM, Specode <0x000...@gmail.com<javascript:>
    wrote:
    On HTTP 1.1, Any request for the client will have keep-alive, If you want
    to set a keep-alive timeout (server side) :

    s := &http.Server{
    Addr: ":80",
    ReadTimeout: 10 * time.Second, // this is timeout
    }

    log.Fatal(s.ListenAndServe())


    if you want disable keep-alive:

    tr := &http.Transport{
    DisableKeepAlives: true,
    }
    c := &http.Client{ Transport: tr }
    resp, err := c.Get("https://www.google.com")

    On Thursday, October 24, 2013 3:49:18 AM UTC+8, Wei Tang wrote:

    Hi I have a question about how to close an http connection explicitly?

    My program is a server providing REST API and clients calls these API
    using golang net.http package.

    The symptom is that I can observe in the server side
    /proc/<process_id>/fd directory, the socket handlers are increasing as the
    clients issues API calls, even the call is finished, the SOCKET fd will
    still be there. Thus the max limit will be reached before too long. I
    checked the client side code the resp is closed after each call as follows:

    esp, err := http.Get("http://example.com/"**)
    if err != nil {
    // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    // ...

    My question is that if there is a way for the server to force to close
    the http connection to release the socket handlers?

    thanks!
    Wei


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/agB-n6V7UsQ/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Islan Dberry at Oct 24, 2013 at 4:28 pm

    On Thursday, October 24, 2013 8:04:24 AM UTC-7, Wei Tang wrote:

    As for setting ReadTimeout, it seems not suitable for my use case where
    big data downloading requires a unpredictable long time.
    The ReadTimeout is suitable if the big data that you are downloading is in
    the response.

    The ReadTimeout specifies the timeout for reading a request. The time to
    write the response is not covered by this timeout.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 23, '13 at 7:49p
activeOct 24, '13 at 4:28p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase