0

If we must connect the db (redis)every time we need to write to or read from the db .After some operates then close the connection . Because the connect is frequently (assume the case must connect and then close) .As a result ,Too many TIME_WAIT state socket stay in kernel. some thing like :

`netstat -vatnl | grep 6379

tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN         
tcp        0      0 127.0.0.1:36476             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:37193             127.0.0.1:6379            ESTABLISHED 
tcp        0      0 127.0.0.1:36480             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:36479             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:36472             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:36457             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:36460             127.0.0.1:6379              TIME_WAIT   
tcp        0      0 127.0.0.1:36481             127.0.0.1:6379              TIME_WAIT `
  1. Can we reuse the port in client ? such call bind() before connect() with the sock opt SO_REUSEPORT
  2. sysctl -w net.ipv4.tcp_timestamps=1 sysctl -w net.ipv4.tcp_tw_recycle=1<
    This way indeed help a lot .But I can still see many TIME_WAIT state
  3. Set socket opt SO_LINGER l_onoff=0 ; l_liger=1 or some other ways?
jiamo
  • 1,406
  • 1
  • 17
  • 29
  • the `net.ipv4.tcp_tw_recycle` has been completely removed from Linux 4.12 due to NAT issue. for more details please refer to https://stackoverflow.com/a/73770219/3011380 – zangw Sep 19 '22 at 08:36

1 Answers1

2

TIME_WAITs are part of tcp protocol tear-down processes. They ensure that packets of old-connection aren't accepted as part of new-connection. So unless there are 1000s of them should leave them as it is.

Having said that as source/dest are localhost there shouldn't be a situation where packets of old-connection can be considered as new-connection's. Among your proposed solutions 1 can be used. Solutions 2 & 3 may have wider implications, so please check before applying them.

tuxuday
  • 2,977
  • 17
  • 18
  • `bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_family = AF_INET; cliaddr.sin_addr.s_addr=htonl(0x7f000001); cliaddr.sin_port = htons(43333); bind(s,(struct sockaddr *)&cliaddr,sizeof(cliaddr));` seem take no effect . Does it need to call bind ? – jiamo Apr 09 '12 at 10:42
  • I am sorry i mistook SO_REUSEPORT as SO_REUSEADDR. I haven't used SO_REUSEPORT! [Check This](http://www.unixguide.net/network/socketfaq/4.11.shtml) – tuxuday Apr 09 '12 at 11:29
  • Hope you had set the flag with setsockopt() and don't need to bind(). If you set these don't think it would necessarily move it to CLOSED state, rather it would allow new sockets to use that address/port combination. [This should be more useful](http://serverfault.com/questions/329845/how-to-forcibly-close-a-socket-in-time-wait) – tuxuday Apr 09 '12 at 11:41