http client, connections reuse and growing TIME_WAIT

3,738 views
Skip to first unread message

Konstantin Cherkasoff

unread,
Oct 25, 2013, 9:05:07 PM10/25/13
to golan...@googlegroups.com
Can somebody explain me how to configure http client to reuse connections?
In my example number of TIME_WAIT connections is continuously growing.



--
Konstantin Cherkasoff

James Bardin

unread,
Oct 25, 2013, 10:45:09 PM10/25/13
to golan...@googlegroups.com


On Friday, October 25, 2013 9:05:07 PM UTC-4, Konstantin Cherkasoff wrote:
Can somebody explain me how to configure http client to reuse connections?
In my example number of TIME_WAIT connections is continuously growing.


TIME_WAIT is a normal part of the TCP stack's operation, and has no real overhead. If for some reason you were able to exhaust all ports on your system, on linux you can set `net.ipv4.tcp_tw_reuse = 1` to let the system recycle ports in that state.

The http client will use keepalives by default, reusing connections when it can.


Konstantin Cherkasoff

unread,
Oct 26, 2013, 1:27:04 AM10/26/13
to golan...@googlegroups.com

Yes, the problem is that my program very quickly reaches "Too many open files".
I'm sure it can be fixed without twiking TCP settings with sysctl.

Tamás Gulácsi

unread,
Oct 26, 2013, 2:40:18 AM10/26/13
to golan...@googlegroups.com
Close connections. Do it explicitly or by disallowing keepalive at transport level or by using HTTP1.0 or setting ReadTimeout.
Or configure the client to send connection: close at the last request.

Konstantin Cherkasoff

unread,
Oct 26, 2013, 1:39:33 PM10/26/13
to golan...@googlegroups.com

Close connections. Do it explicitly or by disallowing keepalive at transport level or by using HTTP1.0 or setting ReadTimeout.
Or configure the client to send connection: close at the last request.

The point is that http.DefaultClient doesn't reuse connections as expected.


If I run ab with only 2 concurrent request (ab -c 1 -n 10000 http://localhost:8080/)
then connections are reused and netstat shows:

Only 2 established connections and source ports are constant
tcp    0    139 67.229.38.178:33845     174.129.127.10:443      ESTABLISHED 1000 172523290 16010/test
tcp    0    139 67.229.38.178:33847     174.129.127.10:443      ESTABLISHED 1000 172523295 16010/test


If I run ab with 4 concurrent requests (ab -c 4 -n 10000 http://localhost:8080/)
then connections are somehow not reused and netstsat shows:

Thousands of TIME_WAIT connections
tcp    0      0 67.229.38.178:60217     174.129.127.10:443      TIME_WAIT   0          0           -
tcp    0      0 67.229.38.178:35741     174.129.127.10:443      TIME_WAIT   0          0           -
tcp    0      0 67.229.38.178:35450     174.129.127.10:443      TIME_WAIT   0          0           -

and only 4 established connections, but source ports are changing 
tcp 0 139 67.229.38.178:37656 174.129.127.10:443 ESTABLISHED 1000 172388569 16010/test
tcp 0 139 67.229.38.178:37662 174.129.127.10:443 ESTABLISHED 1000 172388578 16010/test
tcp 0 139 67.229.38.178:37663 174.129.127.10:443 ESTABLISHED 1000 172388579 16010/test
tcp 0 139 67.229.38.178:37657 174.129.127.10:443 ESTABLISHED 1000 172388570 16010/test


Is there a bug in my code? How it can be fixed?

Matt Harden

unread,
Oct 26, 2013, 6:09:45 PM10/26/13
to Konstantin Cherkasoff, golang-nuts
The default Client uses http.DefaultTransport, and this Transport limits the number of idle connections to the same host to 2 (http.DefaultMaxIdleConnsPerHost). A quick & dirty way around this would be:

    http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 0

0 means unlimited. This could be a problem if other parts of your program also use the default Client.

By the way, some sites will block you if you create too many concurrent connections, since this can look like a denial of service attack.


--
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/groups/opt_out.

Konstantin Cherkasoff

unread,
Oct 27, 2013, 3:10:37 AM10/27/13
to golan...@googlegroups.com, Konstantin Cherkasoff
Yes, I got the idea.
If the program needs more client connection than MaxIdleConnsPerHost then http.Client will allocate more and more new source ports and (because of TIME_WAIT socket state) they will be exhausted soon and very quickly we'll get "Cannot assign requested address".
Reply all
Reply to author
Forward
0 new messages