Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make SSL sockets time out when idle for 60 seconds or more.

  • Loading branch information...
commit 777414a9772b82378ff64a9482e550748fe3fdc8 1 parent 10fbffd
Jonathan Steinert authored
Showing with 21 additions and 2 deletions.
  1. +21 −2 lib/Perlbal/SocketSSL.pm
View
23 lib/Perlbal/SocketSSL.pm
@@ -21,9 +21,10 @@ no warnings qw(deprecated);
use Danga::Socket 1.44;
use IO::Socket::SSL 0.98;
use Errno qw( EAGAIN );
+use Perlbal::Socket;
use base 'Danga::Socket';
-use fields qw( listener create_time );
+use fields qw( listener create_time alive_time);
# magic IO::Socket::SSL crap to make it play nice with us
{
@@ -48,6 +49,17 @@ use fields qw( listener create_time );
};
}
+Perlbal::Socket->set_socket_idle_handler('Perlbal::SocketSSL' => sub {
+ my Perlbal::SocketSSL $v = shift;
+
+ my $max_age = eval { $v->max_idle_time } || 0;
+ return unless $max_age;
+
+ # Attributes are in another class, don't violate object boundaries.
+ $v->close("perlbal_timeout")
+ if $v->{alive_time} < $Perlbal::tick_time - $max_age;
+});
+
# called: CLASS->new( $sock, $tcplistener )
sub new {
my Perlbal::SocketSSL $self = shift;
@@ -59,7 +71,7 @@ sub new {
${*$sock}->{_danga_socket} = $self;
$self->{listener} = $listener;
- $self->{create_time} = time;
+ $self->{alive_time} = $self->{create_time} = time;
$self->SUPER::new($sock);
@@ -127,11 +139,13 @@ sub try_accept {
sub event_read {
$_[0]->watch_read(0);
+ $_[0]->{alive_time} = $Perlbal::tick_time;
$_[0]->try_accept;
}
sub event_write {
$_[0]->watch_write(0);
+ $_[0]->{alive_time} = $Perlbal::tick_time;
$_[0]->try_accept;
}
@@ -139,4 +153,9 @@ sub event_err {
$_[0]->close('invalid_ssl_state');
}
+# You can tuna-fish, but you can't tune a Perlbal::SocketSSL
+sub max_idle_time {
+ return 60;
+}
+
1;
Please sign in to comment.
Something went wrong with that request. Please try again.