Hi Alex: 

thanks for your answer, really helped me.

As you mentioned, the connection was indeed closed by the server side: in 
the packages captured, the server side sent the FIN first; seems like that 
python's HTTP module and nginx all close the conenction on server side

i wrote a simple http server, which do not close the connection, shows the 
TCP connection was reused:

<https://lh3.googleusercontent.com/-QQmj20kftYQ/Ws1wW6fxhsI/AAAAAAAAANk/n8oGgG0NNZYIedfNuRYBMjRlyNpinxFGgCLcBGAs/s1600/reused.jpg>



thx again, Have a nice day!
B.R.

在 2018年4月10日星期二 UTC+8下午10:58:04,Alex Efros写道:
>
> Hi! 
>
> On Tue, Apr 10, 2018 at 06:14:46AM -0700, Sun Frank wrote: 
> > my question is how to reuse underlying TCP connection when doing a lot 
> of 
> > frequent short HTTP requests using golang? 
>
> This happens by default: https://play.golang.org/p/XnzQoGqQlno 
>
> > they say the key is to close resp.Body 
>
> Yes, but any correct code should close all resources. 
> Not closing resp.Body is just one of ways how bad code may break things 
> (like reuse of TCP connections). 
>
> > then I start a http server:   
> > python3 -m http.server 8080 
> > and then i start package capture: 
> > sudo tcpdump -i lo -s 0 -w /tmp/p1.pcap dst port 8080 
> > then run: go run main.go, i still got 3 times of TCP handshake, even 
> after 
> > i change MaxIdleConnections to 1 
>
> - MaxIdleConnections doesn't limit amount of connections client may open. 
> - New connections may open because server closes old ones, not because of 
>   client setup. 
> - Server may close connections for many reasons, for ex. if it see header 
>   "Connection: close" with protocol HTTP/1.1 or if it see protocol 
>   HTTP/1.0 without "Connection: keep-alive" or a lot of other reasons. 
>
> -- 
>                         WBR, Alex. 
>

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

Reply via email to