how to close net/http connection.

4,984 views
Skip to first unread message

ppms...@gmail.com

unread,
Jul 14, 2014, 6:22:45 AM7/14/14
to golan...@googlegroups.com
hi 
   I want to use net/http  get some file from website. unfortunately, i found i can't close the connection as i wish, the connection is leak. I know golang can reclaim the resource. but what i want is to close the connection immediately.  if i remove the dialTimeout function, the connection will been closed quicky as i wish, so what's wrong with my code? Thanks

ppmsn

code 

package getfile

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

var timeout = time.Duration(3) * time.Second

func dialTimeout(network, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, timeout)
return conn, err
}

func Getfile() {

transport := http.Transport{
Dial: dialTimeout,
}

client := &http.Client{Transport: &transport}

//client := &http.Client{}
reqest, _ := http.NewRequest("GET", "http://www.baidu.com/img/bdlogo.gif", nil)

reqest.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
reqest.Header.Set("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3")
reqest.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
reqest.Header.Set("Cache-Control", "max-age=0")
reqest.Header.Set("Connection", "keep-alive")

response, _ := client.Do(reqest)
if response.StatusCode == 200 {
d := make([]byte, 10024)
for {
body, err := response.Body.Read(d)
fmt.Println(body, err)
if (body == 0) || (err != nil) {
break
}
fmt.Println()
}
fmt.Println("response.Body.Close()")
response.Body.Close()
response = nil
client = nil
}

}



// 
main () {

   for ;; {
          Getfile()
   }

}

James Bardin

unread,
Jul 14, 2014, 10:29:47 AM7/14/14
to golan...@googlegroups.com, ppms...@gmail.com
You are requesting that client connections use keep-alive for subsequent requests (though the client will try to do this automatically without setting that header). In this case, closing the Body allows the transport to put that connection back into the keepalive pool. I'm not sure why removing the DialTimeout affects your code, are you using go1.3?

You can set Transport.DisableKeepAlives to prevent keep-alive connections altogether, or periodically use Transport.CloseIdleConnections to close out open connections in a keep-alive state.

ppms...@gmail.com

unread,
Jul 15, 2014, 6:24:29 AM7/15/14
to golan...@googlegroups.com, ppms...@gmail.com
yes, I am using GO 1.3 

output
go version
go version go1.3 darwin/amd64

like your said. I added one line like this. 

                response.Body.Close()
response = nil
transport.CloseIdleConnections()

it worked, this connection was closed. it is so fine. Thanks

I have any other question about net/http, can you help me? 
After reading  the net/http doc, I still do't know  how to set read timeout of response.Body.Read(). 

body, err := response.Body.Read(d)  // set readtimeout.

Jesse McNelis

unread,
Jul 15, 2014, 6:39:31 AM7/15/14
to ppms...@gmail.com, golang-nuts
On Tue, Jul 15, 2014 at 8:24 PM, <ppms...@gmail.com> wrote:
> I have any other question about net/http, can you help me?
> After reading the net/http doc, I still do't know how to set read timeout
> of response.Body.Read().
>
> body, err := response.Body.Read(d) // set readtimeout.

http://golang.org/pkg/net/http/#Client has a 'Timeout' field you can set.

benjiam shen

unread,
Jul 19, 2014, 11:29:30 PM7/19/14
to golan...@googlegroups.com, ppms...@gmail.com, jes...@jessta.id.au
Thanks a lot, but i don't think it is i need.I need how to set a timeout that read from socket   everytime . 

Matt Harden

unread,
Jul 20, 2014, 9:30:34 PM7/20/14
to benjiam shen, golang-nuts, Jesse McNelis
That timeout applies to reading the Body as well as the header, so I think it may do the job for you.


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Harden

unread,
Jul 20, 2014, 9:38:59 PM7/20/14
to benjiam shen, golang-nuts, Jesse McNelis
If you need to restart the timer at every Read, you can create your own time.Timer to call net.DefaultTransport.(*net.Transport).CancelRequest(req), and call timer.Reset after each successful Read.
Reply all
Reply to author
Forward
0 new messages