Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 31, 2013
1 parent af152b8 commit 554f222
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -55,11 +55,10 @@ sub client {
$self = $self->singleton unless ref $self;

# Make sure timers are running
$self->_timers;
$self->_recurring;

my $id = $self->_id;
my $c = $self->{connections}{$id} ||= {};
my $client = $c->{client} = Mojo::IOLoop::Client->new;
my $id = $self->_id;
my $client = $self->{connections}{$id}{client} = Mojo::IOLoop::Client->new;
weaken $client->reactor($self->reactor)->{reactor};

weaken $self;
Expand Down Expand Up @@ -98,12 +97,7 @@ sub generate_port { Mojo::IOLoop::Server->generate_port }
sub is_running { (ref $_[0] ? $_[0] : $_[0]->singleton)->reactor->is_running }
sub one_tick { (ref $_[0] ? $_[0] : $_[0]->singleton)->reactor->one_tick }

sub recurring {
my ($self, $after, $cb) = @_;
$self = $self->singleton unless ref $self;
weaken $self;
return $self->reactor->recurring($after => sub { $self->$cb });
}
sub recurring { shift->_timer(recurring => @_) }

sub remove {
my ($self, $id) = @_;
Expand Down Expand Up @@ -144,31 +138,20 @@ sub stream {
my ($self, $stream) = @_;
$self = $self->singleton unless ref $self;

# Connect stream with reactor
if (ref $stream) {

# Enforce connection limit (randomize to improve load balancing)
$self->max_connections(0)
if defined $self->{accepts}
&& ($self->{accepts} -= int(rand 2) + 1) <= 0;
# Find stream for id
return ($self->{connections}{$stream} || {})->{stream} unless ref $stream;

# Release accept mutex
$self->_not_accepting;
# Release accept mutex
$self->_not_accepting;

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

# Find stream for id
return undef unless my $c = $self->{connections}{$stream};
return $c->{stream};
return $self->_stream($stream, $self->_id);
}

sub timer {
my ($self, $after, $cb) = @_;
$self = $self->singleton unless ref $self;
weaken $self;
return $self->reactor->timer($after => sub { $self->$cb });
}
sub timer { shift->_timer(timer => @_) }

sub _accepting {
my $self = shift;
Expand Down Expand Up @@ -204,7 +187,7 @@ sub _not_accepting {
my $self = shift;

# Make sure timers are running
$self->_timers;
$self->_recurring;

# Release accept mutex
return unless delete $self->{accepting};
Expand All @@ -214,6 +197,12 @@ sub _not_accepting {
$_->stop for values %{$self->{acceptors} || {}};
}

sub _recurring {
my $self = shift;
$self->{accept} ||= $self->recurring($self->accept_interval => \&_accepting);
$self->{stop} ||= $self->recurring(1 => \&_stop);
}

sub _remove {
my ($self, $id) = @_;

Expand All @@ -240,7 +229,7 @@ sub _stream {
my ($self, $stream, $id) = @_;

# Make sure timers are running
$self->_timers;
$self->_recurring;

# Connect stream with reactor
$self->{connections}{$id}{stream} = $stream;
Expand All @@ -252,10 +241,11 @@ sub _stream {
return $id;
}

sub _timers {
my $self = shift;
$self->{accept} ||= $self->recurring($self->accept_interval => \&_accepting);
$self->{stop} ||= $self->recurring(1 => \&_stop);
sub _timer {
my ($self, $method, $after, $cb) = @_;
$self = $self->singleton unless ref $self;
weaken $self;
return $self->reactor->$method($after => sub { $self->$cb });
}

1;
Expand Down

0 comments on commit 554f222

Please sign in to comment.