eventscripts: Tweak the timeout check in kill_tcp_connections()
authorMartin Schwenke <martin@meltin.net>
Tue, 30 Apr 2013 01:39:46 +0000 (11:39 +1000)
committerAmitay Isaacs <amitay@gmail.com>
Mon, 6 May 2013 06:22:15 +0000 (16:22 +1000)
This has 2 advantages:

1. It uses get_tcp_connections_for_ip() to check for leftover
   connections, instead of custom code.

2. It checks for the timeout condition before sleeping.  The current
   code sleeps and then checks, so wastes a second.

Signed-off-by: Martin Schwenke <martin@meltin.net>
config/functions

index 76173dee12924da539648e23fd92bc437feac215..4515d2db63113573a7556635d506a6fba29742b6 100755 (executable)
@@ -620,16 +620,22 @@ kill_tcp_connections() {
        [ $_killcount -gt 0 ] || {
            return;
        }
+
        _count=0
-       while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
-           sleep 1
-           _count=`expr $_count + 1`
-           [ $_count -gt 3 ] && {
+       while : ; do
+           if [ -z "$(get_tcp_connections_for_ip $_IP)" ] ; then
+               echo "killed $_killcount TCP connections to released IP $_IP"
+               return
+           fi
+
+           _count=$(($_count + 1))
+           if [ $_count -gt 3 ] ; then
                echo "Timed out killing tcp connections for IP $_IP"
-               return;
-           }
+               return
+           fi
+
+           sleep 1
        done
-       echo "killed $_killcount TCP connections to released IP $_IP"
     }
 }