Skip to content

Commit

Permalink
generate multi_accept value only once
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 17, 2015
1 parent 1c1a662 commit 6d2ef7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -16,7 +16,7 @@ use constant DEBUG => $ENV{MOJO_IOLOOP_DEBUG} || 0;

has max_accepts => 0;
has max_connections => 1000;
has multi_accept => 50;
has multi_accept => sub { shift->max_connections > 50 ? 50 : 1 };
has reactor => sub {
my $class = Mojo::Reactor::Poll->detect;
warn "-- Reactor initialized ($class)\n" if DEBUG;
Expand Down Expand Up @@ -142,8 +142,7 @@ sub start {
sub stop { _instance(shift)->reactor->stop }

sub stop_gracefully {
my $self = _instance(shift);
$self->_not_accepting;
my $self = _instance(shift)->_not_accepting;
$self->{stop} ||= $self->recurring(1 => \&_stop);
}

Expand Down Expand Up @@ -178,9 +177,8 @@ sub _maybe_accepting {
return if $self->{accepting} || $self->_limit;

# Check if multi-accept is desirable
my $multi = $self->multi_accept;
$multi = 1 if $self->max_connections < $multi;
$_->multi_accept($multi)->start for values %{$self->{acceptors} || {}};
my $acceptors = $self->{acceptors} || {};
$_->multi_accept($self->multi_accept)->start for values %$acceptors;
$self->{accepting} = 1;
}

Expand Down Expand Up @@ -347,7 +345,8 @@ C<1000>.
my $multi = $loop->multi_accept;
$loop = $loop->multi_accept(100);
Number of connections to accept at once, defaults to C<50>.
Number of connections to accept at once, defaults to C<50> or C<1>, depending
if the value of L</"max_connections"> is smaller than C<50>.
=head2 reactor
Expand Down
14 changes: 14 additions & 0 deletions t/mojo/ioloop.t
Expand Up @@ -23,6 +23,20 @@ $ENV{MOJO_REACTOR} = 'MyReactor';
$loop = Mojo::IOLoop->new;
is ref $loop->reactor, 'MyReactor', 'right class';

# Defaults
$loop = Mojo::IOLoop->new;
is $loop->max_connections, 1000, 'right default';
is $loop->multi_accept, 50, 'right default';
$loop = Mojo::IOLoop->new(max_connections => 51);
is $loop->max_connections, 51, 'right value';
is $loop->multi_accept, 50, 'right value';
$loop = Mojo::IOLoop->new(max_connections => 10);
is $loop->max_connections, 10, 'right value';
is $loop->multi_accept, 1, 'right value';
$loop = Mojo::IOLoop->new(multi_accept => 10);
is $loop->max_connections, 1000, 'right value';
is $loop->multi_accept, 10, 'right value';

# Double start
my $err;
Mojo::IOLoop->next_tick(
Expand Down

0 comments on commit 6d2ef7a

Please sign in to comment.