From 6d1ae5319e0c7ba07e20aa14805fd015dd40439e Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Mon, 4 Aug 2014 09:26:56 -0400 Subject: [PATCH] LP#1042850: Add TCP-level keepalive Some client TCP stacks fail to actually close down their sockets all the way, leading to a pile up of stale backends that can never go away. So, we will use Linux's TCP_KEEPALIVE tuning capabilities to probe the connection on a regular basis. This should detect the half- closed situation and let the backend shut itself down. Signed-off-by: Mike Rylander Signed-off-by: Bill Erickson --- SIPServer.pm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SIPServer.pm b/SIPServer.pm index 4ea82c5..1aba170 100755 --- a/SIPServer.pm +++ b/SIPServer.pm @@ -26,7 +26,7 @@ use Sys::Syslog qw(syslog); use Net::Server::PreFork; use Net::Server::Proto; use IO::Socket::INET; -use Socket qw(:crlf); +use Socket qw(:crlf SOL_SOCKET SO_KEEPALIVE IPPROTO_TCP TCP_KEEPALIVE); use Data::Dumper; # For debugging require UNIVERSAL::require; @@ -113,6 +113,19 @@ sub process_request { my ($sockaddr, $port, $proto); my $transport; + # This is kind of kinky, but allows us to avoid requiring Socket::Linux. + # A simple "Socket::Linux"->use won't suffice since we need access to + # all of it's bareword constants as well. + eval <<' EVAL'; + use Socket::Linux qw(TCP_KEEPINTVL TCP_KEEPIDLE TCP_KEEPCNT); + setsockopt($self->{server}->{client}, SOL_SOCKET, SO_KEEPALIVE, 1); + setsockopt($self->{server}->{client}, IPPROTO_TCP, TCP_KEEPIDLE, 120); + setsockopt($self->{server}->{client}, IPPROTO_TCP, TCP_KEEPINTVL, 10); + EVAL + + syslog('LOG_DEBUG', + "Consider installing Socket::Linux for TCP keepalive: $@") if $@; + $self->{account} = undef; # New connection, no need to keep login info $self->{config} = $config; -- 2.43.2