Skip to content

Commit

Permalink
fix bug in Mojo::IOLoop where the accept limit was applied too broadly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 30, 2015
1 parent 92f9903 commit 9d08386
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,10 +1,11 @@

5.76 2015-01-30
5.76 2015-01-31
- Increased default max_message_size from 10MB to 16MB in Mojo::Message.
- Reduced default max_line_size from 10KB to 8KB in Mojo::Headers and
Mojo::Message.
- Improved Hypnotoad load balancing by calling srand() after starting a new
worker in Mojo::Server::Prefork.
- Fixed bug in Mojo::IOLoop where the accept limit was applied to broadly.
- Fixed bug in Mojo::DOM::CSS where combinators needed to be surrounded by
whitespace.

Expand Down
23 changes: 11 additions & 12 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -41,7 +41,6 @@ sub acceptor {
my $id = $self->_id;
$self->{acceptors}{$id} = $acceptor;
weaken $acceptor->reactor($self->reactor)->{reactor};
$self->{accepts} = $self->max_accepts if $self->max_accepts;

# Allow new acceptor to get picked up
$self->_not_accepting;
Expand Down Expand Up @@ -119,6 +118,16 @@ sub server {
weaken $self;
$server->on(
accept => sub {

# Release accept mutex
$self->_not_accepting;

# Enforce connection limit (randomize to improve load balancing)
if (my $max = $self->max_accepts) {
$self->{accepts} //= $max - int rand $max / 2;
$self->max_connections(0) if ($self->{accepts} -= 1) <= 0;
}

my $stream = Mojo::IOLoop::Stream->new(pop);
$self->$cb($stream, $self->stream($stream));
}
Expand All @@ -140,18 +149,8 @@ sub stop { _instance(shift)->reactor->stop }

sub stream {
my ($self, $stream) = (_instance(shift), @_);

# Find stream for id
return ($self->{connections}{$stream} || {})->{stream} unless ref $stream;

# Release accept mutex
$self->_not_accepting;

# Enforce connection limit (randomize to improve load balancing)
$self->max_connections(0)
if defined $self->{accepts} && ($self->{accepts} -= int(rand 2) + 1) <= 0;

return $self->_stream($stream, $self->_id);
return $self->_stream($stream => $self->_id);
}

sub timer { shift->_timer(timer => @_) }
Expand Down

0 comments on commit 9d08386

Please sign in to comment.