Skip to content

Commit

Permalink
improved accept performance of all built-in servers by up to 1000% wi…
Browse files Browse the repository at this point in the history
…th the EV backend
  • Loading branch information
kraih committed Sep 24, 2011
1 parent b2c8b99 commit 9230aaf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Changes
Expand Up @@ -9,10 +9,10 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL profile helper.
- Added EXPERIMENTAL binary support to Mojo::Transaction::WebSocket.
- Updated WebSocket implementation to ietf-15.
- Improved accept performance of all built-in servers by up to 1000%
with the EV backend.
- Improved documentation.
- Improved CSS of some built-in templates.
- Improved accept performance of the standalone daemon with EV
backend by up to 1000%.
- Fixed CSS of built-in exception template.
- Fixed close event bug in Mojo::IOLoop.
- Fixed small redirect_to bug. (judofyr, sri)
Expand Down
11 changes: 7 additions & 4 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -388,12 +388,15 @@ sub _listening {
return if $self->{listening};
my $servers = $self->{servers} ||= {};
return unless keys %$servers;
my $i = keys %{$self->{connections}};
return unless $i < $self->max_connections;
my $i = keys %{$self->{connections}};
my $max = $self->max_connections;
return unless $i < $max;
if (my $cb = $self->on_lock) { return unless $self->$cb(!$i) }

# Start listening
$_->resume for values %$servers;
# Try to guess the ideal number of accepts and start listening
my $accepts = $self->{accepts};
$accepts = 10 unless defined $accepts && $accepts > 0 && $accepts < 10;
$_->resume($max > 1 ? $accepts : 1) for values %$servers;
$self->{listening} = 1;
}

Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -81,8 +81,6 @@ sub _connect {
# IPv6 needs an early start
$handle->connect if IPV6;
}

# Non-blocking
$handle->blocking(0);

# Disable Nagle's algorithm
Expand Down
13 changes: 6 additions & 7 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -122,6 +122,7 @@ sub listen {
$reuse = ",$reuse" if length $ENV{MOJO_REUSE};
$ENV{MOJO_REUSE} .= "$reuse:$fd";
}
$handle->blocking(0);
$self->{handle} = $handle;

# TLS
Expand Down Expand Up @@ -167,19 +168,17 @@ sub pause {
}

sub resume {
my $self = shift;
my ($self, $accepts) = @_;
weaken $self;
$self->iowatcher->add($self->{handle},
on_readable => sub { $self->_accept });
on_readable => sub { $self->_accept for 1 .. $accepts });
}

sub _accept {
my $self = shift;

# Accept
my $handle = $self->{handle}->accept;

# Non-blocking
return unless my $handle = $self->{handle}->accept;
$handle->blocking(0);

# Disable Nagle's algorithm
Expand Down Expand Up @@ -274,7 +273,7 @@ Mojo::IOLoop::Server - IOLoop socket server
$server->listen(port => 3000);
# Start and stop accepting connections
$server->resume;
$server->resume(1);
$server->pause;
=head1 DESCRIPTION
Expand Down Expand Up @@ -364,7 +363,7 @@ Stop accepting connections.
=head2 C<resume>
$server->resume;
$server->resume(10);
Start accepting connections.
Expand Down

0 comments on commit 9230aaf

Please sign in to comment.