Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved connection_timeout method in Mojo::IOLoop by allowing it to …
…be called as a class method
  • Loading branch information
kraih committed Sep 25, 2011
1 parent 77e10ce commit b51d9ed
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -12,6 +12,8 @@ This file documents the revision history for Perl extension Mojolicious.
- Changed default upgrade timeout of Hypnotoad from 30 to 60 seconds.
- Improved accept performance of all built-in servers by up to 1000%
with the EV backend.
- Improved connection_timeout method in Mojo::IOLoop by allowing it
to be called as a class method.
- Improved documentation.
- Improved CSS of some built-in templates.
- Fixed CSS of built-in exception template.
Expand Down
12 changes: 12 additions & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -132,6 +132,7 @@ sub connect {

sub connection_timeout {
my ($self, $id, $timeout) = @_;
$self = $self->singleton unless ref $self;
return unless my $c = $self->{connections}->{$id};
$c->{timeout} = $timeout and return $self if defined $timeout;
$c->{timeout};
Expand Down Expand Up @@ -338,10 +339,12 @@ sub timer {
sub trigger {
my ($self, $cb) = @_;
$self = $self->singleton unless ref $self;

my $t = Mojo::IOLoop::Trigger->new;
$t->ioloop($self);
weaken $t->{ioloop};
$t->once(done => $cb) if $cb;

return $t;
}

Expand All @@ -364,13 +367,20 @@ sub write {

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

# Timer
return $self unless my $watcher = $self->iowatcher;
return $self if $watcher->cancel($id);

# Listen socket
if (delete $self->{servers}->{$id}) { delete $self->{listening} }

# Connection
else {
delete(($self->{connections}->{$id} || {})->{stream});
delete $self->{connections}->{$id};
}

return $self;
}

Expand Down Expand Up @@ -668,6 +678,8 @@ Path to the TLS key file.
=head2 C<connection_timeout>
my $timeout = Mojo::IOLoop->connection_timeout($id);
$loop = Mojo::IOLoop->connection_timeout($id => 45);
my $timeout = $loop->connection_timeout($id);
$loop = $loop->connection_timeout($id => 45);
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -28,7 +28,7 @@ get '/' => {text => 'works'};
my $timeout = undef;
get '/timeout' => sub {
my $self = shift;
Mojo::IOLoop->singleton->connection_timeout($self->tx->connection => '0.5');
Mojo::IOLoop->connection_timeout($self->tx->connection => '0.5');
$self->on_finish(sub { $timeout = 1 });
$self->render_later;
};
Expand Down
7 changes: 3 additions & 4 deletions t/mojo/websocket.t
Expand Up @@ -55,9 +55,8 @@ websocket '/' => sub {

# GET /something/else
get '/something/else' => sub {
my $self = shift;
my $timeout =
Mojo::IOLoop->singleton->connection_timeout($self->tx->connection);
my $self = shift;
my $timeout = Mojo::IOLoop->connection_timeout($self->tx->connection);
$self->render(text => "${timeout}failed!");
};

Expand All @@ -69,7 +68,7 @@ websocket '/socket' => sub {
sub {
my $self = shift;
$self->send_message(
Mojo::IOLoop->singleton->connection_timeout($self->tx->connection));
Mojo::IOLoop->connection_timeout($self->tx->connection));
$self->finish;
}
);
Expand Down

0 comments on commit b51d9ed

Please sign in to comment.