Skip to content

Commit

Permalink
improved accept performance in Mojo::IOLoop::Server
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 9, 2014
1 parent 2f8f742 commit 21bbb91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,8 +1,9 @@

4.98 2014-05-03
4.98 2014-05-09
- Removed deprecated get_line function from Mojo::Util.
- Removed deprecated content_xml, replace_content, text_before, text_after
and to_xml methods from Mojo::DOM.
- Improved accept performance in Mojo::IOLoop::Server.

4.97 2014-04-30
- Deprecated support for "X-Forwarded-HTTPS" in favor of
Expand Down
36 changes: 19 additions & 17 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -89,8 +89,14 @@ sub listen {
return unless $args->{tls};
croak "IO::Socket::SSL 1.84 required for TLS support" unless TLS;

weaken $self;
my $tls = $self->{tls} = {
SSL_cert_file => $args->{tls_cert} || $CERT,
SSL_error_trap => sub {
return unless my $handle = delete $self->{handles}{shift()};
$self->reactor->remove($handle);
close $handle;
},
SSL_honor_cipher_order => 1,
SSL_key_file => $args->{tls_key} || $KEY,
SSL_startHandshake => 0,
Expand All @@ -104,32 +110,28 @@ sub listen {
sub start {
my $self = shift;
weaken $self;
$self->reactor->io(
$self->{handle} => sub { $self->_accept for 1 .. $self->multi_accept });
$self->reactor->io($self->{handle} => sub { $self->_accept });
}

sub stop { $_[0]->reactor->remove($_[0]{handle}) }

sub _accept {
my $self = shift;

return unless my $handle = $self->{handle}->accept;
$handle->blocking(0);
# Greedy accept
for (1 .. $self->multi_accept) {
return unless my $handle = $self->{handle}->accept;
$handle->blocking(0);

# Disable Nagle's algorithm
setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
# Disable Nagle's algorithm
setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;

# Start TLS handshake
return $self->emit_safe(accept => $handle) unless my $tls = $self->{tls};
weaken $self;
$tls->{SSL_error_trap} = sub {
return unless my $handle = delete $self->{handles}{shift()};
$self->reactor->remove($handle);
close $handle;
};
return unless $handle = IO::Socket::SSL->start_SSL($handle, %$tls);
$self->reactor->io($handle => sub { $self->_tls($handle) });
$self->{handles}{$handle} = $handle;
# Start TLS handshake
$self->emit_safe(accept => $handle) and next unless my $tls = $self->{tls};
next unless $handle = IO::Socket::SSL->start_SSL($handle, %$tls);
$self->reactor->io($handle => sub { $self->_tls($handle) });
$self->{handles}{$handle} = $handle;
}
}

sub _tls {
Expand Down

0 comments on commit 21bbb91

Please sign in to comment.