Skip to content

Commit

Permalink
improved load balancing between Hypnotoad worker processes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 5, 2012
1 parent 060c1a4 commit 5fa2485
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.03 2012-07-06
- Improved load balancing between Hypnotoad worker processes.
- Improved Hypnotoad log messages.
- Improved documentation.
- Improved tests.
Expand Down
11 changes: 7 additions & 4 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -137,9 +137,11 @@ sub server {
my $id = $self->stream($stream);
$self->$cb($stream, $id);

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

# Stop listening
$self->_not_listening;
Expand Down Expand Up @@ -397,7 +399,8 @@ this callback are not captured.
The maximum number of connections this loop is allowed to accept before
shutting down gracefully without interrupting existing connections, defaults
to C<0>. Setting the value to C<0> will allow this loop to accept new
connections indefinitely.
connections indefinitely. Note that half of this value can be subtracted
randomly to improve load balancing between multiple server processes.
=head2 C<max_connections>
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -460,8 +460,9 @@ L<Mojolicious::Guides::Cookbook/"Hypnotoad"> for examples.
Maximum number of connections a worker is allowed to accept before stopping
gracefully, defaults to C<1000>. Setting the value to C<0> will allow workers
to accept new connections indefinitely. Note that worker processes stop
sending heartbeat messages once this limit has been reached.
to accept new connections indefinitely. Note that half of this value can be
subtracted randomly to improve load balancing, and that worker processes will
stop sending heartbeat messages once the limit has been reached.
=head2 C<backlog>
Expand Down

0 comments on commit 5fa2485

Please sign in to comment.