title: How should I limit the slow disconnect of thousands of tcp connections?
body: <p>I have a golang program running on centos that usually has around 5k tcp clients connected. Every once in a while this number goes to around 15k for about an hour and still everything is fine.</p>
  
  <p>The program has a slow shutdown mode where it stops accepting new clients and slowly kills all currently connected clients over the course of 20 mins. During these slow shutdown periods if the machine has 15k clients, sometimes I get:</p>
  
  <pre><code>Wed Oct 31 21:28:23 2018] net_ratelimit: 482 callbacks suppressed
  [Wed Oct 31 21:28:23 2018] TCP: too many orphaned sockets
  [Wed Oct 31 21:28:23 2018] TCP: too many orphaned sockets
  [Wed Oct 31 21:28:23 2018] TCP: too many orphaned sockets
  </code></pre>
  
  <p>I have tried adding:</p>
  
  <pre><code>echo "net.ipv4.tcp_max_syn_backlog=5000" &gt;&gt; /etc/sysctl.conf
  echo "net.ipv4.tcp_fin_timeout=10" &gt;&gt; /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_recycle=1" &gt;&gt; /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_reuse=1" &gt;&gt; /etc/sysctl.conf
  sysctl -f /etc/sysctl.conf
  </code></pre>
  
  <p>And these values are set, I see them with their correct new values. A typical sockstat is:</p>
  
  <pre><code>cat /proc/net/sockstat
  sockets: used 31682
  TCP: inuse 17286 orphan 5 tw 3874 alloc 31453 mem 15731
  UDP: inuse 8 mem 3
  UDPLITE: inuse 0
  RAW: inuse 0
  FRAG: inuse 0 memory 0
  </code></pre>
  
  <p>And ideas how to stop the too many orphaned socket error and crash? Should I increase the 20 min slow shutdown period to 40 mins? Increase tcp_mem? Thanks!</p>