Skip to content

Commit

Permalink
deprecated Mojo::IOLoop->drop in favor of Mojo::IOLoop->remove and re…
Browse files Browse the repository at this point in the history
…named Mojo::Reactor->drop to Mojo::Reactor->remove
  • Loading branch information
kraih committed Mar 22, 2012
1 parent e07e3f0 commit 37a73eb
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 157 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.65 2012-03-22 00:00:00
- Deprecated Mojo::IOLoop->drop in favor of Mojo::IOLoop->remove.
- Renamed Mojo::Reactor->drop to Mojo::Reactor->remove.
- Added one_tick method to Mojo::Reactor and Mojo::Reactor::EV.
- Removed experimental status from Mojo::Reactor.
- Removed experimental status from Mojo::Reactor::EV.
Expand Down
8 changes: 4 additions & 4 deletions examples/connect-proxy.pl
Expand Up @@ -32,7 +32,7 @@
my ($loop, $err, $stream) = @_;
if ($err) {
say "Connection error for $address:$port: $err";
Mojo::IOLoop->drop($client);
Mojo::IOLoop->remove($client);
return delete $c->{$client};
}
say "Forwarding to $address:$port.";
Expand All @@ -45,7 +45,7 @@
);
$stream->on(
close => sub {
Mojo::IOLoop->drop($client);
Mojo::IOLoop->remove($client);
delete $c->{$client};
}
);
Expand All @@ -56,12 +56,12 @@
);
}
}
else { Mojo::IOLoop->drop($client) }
else { Mojo::IOLoop->remove($client) }
}
);
$stream->on(
close => sub {
Mojo::IOLoop->drop($c->{$client}->{connection})
Mojo::IOLoop->remove($c->{$client}->{connection})
if $c->{$client}->{connection};
delete $c->{$client};
}
Expand Down
2 changes: 1 addition & 1 deletion examples/flash-policy-server.pl
Expand Up @@ -26,7 +26,7 @@
my ($loop, $stream, $id) = @_;
$stream->on(
read => sub {
shift->write($xml, sub { Mojo::IOLoop->drop($id) });
shift->write($xml, sub { Mojo::IOLoop->remove($id) });
}
);
}
Expand Down
70 changes: 38 additions & 32 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -85,11 +85,10 @@ sub delay {
return $delay;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub drop {
my ($self, $id) = @_;
$self = $self->singleton unless ref $self;
if (my $c = $self->{connections}->{$id}) { return $c->{finish} = 1 }
$self->_drop($id);
warn "Mojo::IOLoop->drop is DEPRECATED in favor of Mojo::IOLoop->remove!\n";
shift->remove(@_);
}

sub generate_port { Mojo::IOLoop::Server->generate_port }
Expand All @@ -112,6 +111,13 @@ sub recurring {
return $self->reactor->recurring($after => sub { $self->$cb });
}

sub remove {
my ($self, $id) = @_;
$self = $self->singleton unless ref $self;
if (my $c = $self->{connections}->{$id}) { return $c->{finish} = 1 }
$self->_remove($id);
}

# "Fat Tony is a cancer on this fair city!
# He is the cancer and I am the… uh… what cures cancer?"
sub server {
Expand Down Expand Up @@ -213,35 +219,18 @@ sub _cleaner {
$self->_listening;
my $connections = $self->{connections} ||= {};
while (my ($id, $c) = each %$connections) {
$self->_drop($id)
$self->_remove($id)
if $c->{finish} && (!$c->{stream} || !$c->{stream}->is_writing);
}

# Graceful shutdown
$self->_drop(delete $self->{cleaner})
$self->_remove(delete $self->{cleaner})
unless keys(%$connections) || keys(%{$self->{servers}});
$self->stop if $self->max_connections == 0 && keys %$connections == 0;
}
);
}

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

# Timer
return unless my $reactor = $self->reactor;
return if $reactor->drop($id);

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

# Connection (stream needs to be deleted first)
else {
delete(($self->{connections}->{$id} || {})->{stream});
delete $self->{connections}->{$id};
}
}

sub _id {
my $self = shift;
my $id;
Expand Down Expand Up @@ -279,6 +268,23 @@ sub _not_listening {
$_->stop for values %{$self->{servers} || {}};
}

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

# Timer
return unless my $reactor = $self->reactor;
return if $reactor->remove($id);

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

# Connection (stream needs to be deleted first)
else {
delete(($self->{connections}->{$id} || {})->{stream});
delete $self->{connections}->{$id};
}
}

1;
__END__
Expand Down Expand Up @@ -323,7 +329,7 @@ Mojo::IOLoop - Minimalistic reactor for non-blocking TCP clients and servers
# Add a timer
Mojo::IOLoop->timer(5 => sub {
my $loop = shift;
$loop->drop($id);
$loop->remove($id);
});
# Start loop if necessary
Expand Down Expand Up @@ -473,14 +479,6 @@ event L<Mojo::IOLoop::Delay/"finish"> if optional callback is provided.
# Wait for events if necessary
$delay->wait unless Mojo::IOLoop->is_running;
=head2 C<drop>
Mojo::IOLoop->drop($id);
$loop->drop($id);
Drop anything with an id. Connections will be dropped gracefully by allowing
them to finish writing all data in their write buffers.
=head2 C<generate_port>
my $port = Mojo::IOLoop->generate_port;
Expand Down Expand Up @@ -517,6 +515,14 @@ amount of time in seconds.
my $loop2 = Mojo::IOLoop->new;
Mojo::IOLoop->recurring(0 => sub { $loop2->one_tick });
=head2 C<remove>
Mojo::IOLoop->remove($id);
$loop->remove($id);
Remove anything with an id. Connections will be dropped gracefully by
allowing them to finish writing all data in their write buffers.
=head2 C<server>
my $id = Mojo::IOLoop->server(port => 3000, sub {...});
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -38,9 +38,9 @@ sub connect {
sub _cleanup {
my $self = shift;
return unless my $reactor = $self->{reactor};
$reactor->drop(delete $self->{delay}) if $self->{delay};
$reactor->drop(delete $self->{timer}) if $self->{timer};
$reactor->drop(delete $self->{handle}) if $self->{handle};
$reactor->remove(delete $self->{delay}) if $self->{delay};
$reactor->remove(delete $self->{timer}) if $self->{timer};
$reactor->remove(delete $self->{handle}) if $self->{handle};
}

sub _connect {
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -80,7 +80,7 @@ sub DESTROY {
defined $_ and -w $_ and unlink $_ for $self->{cert}, $self->{key};
return unless my $reactor = $self->{reactor};
$self->stop if $self->{handle};
$reactor->drop($_) for values %{$self->{handles}};
$reactor->remove($_) for values %{$self->{handles}};
}

# "And I gave that man directions, even though I didn't know the way,
Expand Down Expand Up @@ -161,7 +161,7 @@ sub start {

sub stop {
my $self = shift;
$self->reactor->drop($self->{handle});
$self->reactor->remove($self->{handle});
}

sub _accept {
Expand All @@ -179,7 +179,7 @@ sub _accept {
weaken $self;
$tls->{SSL_error_trap} = sub {
return unless my $handle = delete $self->{handles}->{shift()};
$self->reactor->drop($handle);
$self->reactor->remove($handle);
close $handle;
};
return unless $handle = IO::Socket::SSL->start_SSL($handle, %$tls);
Expand Down Expand Up @@ -210,7 +210,7 @@ sub _tls {

# Accepted
if ($handle->accept_SSL) {
$self->reactor->drop($handle);
$self->reactor->remove($handle);
delete $self->{handles}->{$handle};
return $self->emit_safe(accept => $handle);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -26,9 +26,9 @@ sub close {

# Cleanup
return unless my $reactor = $self->{reactor};
$reactor->drop(delete $self->{timer}) if $self->{timer};
$reactor->remove(delete $self->{timer}) if $self->{timer};
return unless my $handle = delete $self->{handle};
$reactor->drop($handle);
$reactor->remove($handle);

# Close
close $handle;
Expand Down Expand Up @@ -82,7 +82,7 @@ sub stop {
# about it."
sub steal_handle {
my $self = shift;
$self->reactor->drop($self->{handle});
$self->reactor->remove($self->{handle});
return delete $self->{handle};
}

Expand Down
34 changes: 17 additions & 17 deletions lib/Mojo/Reactor.pm
Expand Up @@ -17,13 +17,6 @@ sub detect {
return 'Mojo::Reactor';
}

sub drop {
my ($self, $drop) = @_;
return delete shift->{timers}->{shift()} unless ref $drop;
$self->_poll->remove($drop);
return delete $self->{io}->{fileno $drop};
}

sub io {
my ($self, $handle, $cb) = @_;
$self->{io}->{fileno $handle} = {cb => $cb};
Expand Down Expand Up @@ -69,7 +62,7 @@ sub one_tick {
if ($after <= time - ($t->{started} || $t->{recurring} || 0)) {

# Normal timer
if ($t->{started}) { $self->drop($id) }
if ($t->{started}) { $self->remove($id) }

# Recurring timer
elsif ($after && $t->{recurring}) { $t->{recurring} += $after }
Expand All @@ -85,6 +78,13 @@ sub one_tick {

sub recurring { shift->_timer(pop, after => pop, recurring => time) }

sub remove {
my ($self, $remove) = @_;
return delete shift->{timers}->{shift()} unless ref $remove;
$self->_poll->remove($remove);
return delete $self->{io}->{fileno $remove};
}

sub start {
my $self = shift;
return if $self->{running}++;
Expand Down Expand Up @@ -154,7 +154,7 @@ Mojo::Reactor - Minimalistic low level event reactor
# Add a timer
$reactor->timer(15 => sub {
my $reactor = shift;
$reactor->drop($handle);
$reactor->remove($handle);
say 'Timeout!';
});
Expand All @@ -172,11 +172,11 @@ L<IO::Poll> and the foundation of L<Mojo::IOLoop>.
$ENV{MOJO_REACTOR} ||= 'Mojo::Reactor::MyLoop';
sub drop {...}
sub io {...}
sub is_running {...}
sub one_tick {...}
sub recurring {...}
sub remove {...}
sub start {...}
sub stop {...}
sub timer {...}
Expand Down Expand Up @@ -217,13 +217,6 @@ implements the following new ones.
Detect and load the best reactor implementation available, will try the value
of the C<MOJO_REACTOR> environment variable or L<Mojo::Reactor::EV>.
=head2 C<drop>
my $success = $reactor->drop($handle);
my $success = $reactor->drop($id);
Drop handle or timer.
=head2 C<io>
$reactor = $reactor->io($handle => sub {...});
Expand Down Expand Up @@ -267,6 +260,13 @@ amount of time in seconds.
# Invoke as soon as possible
$reactor->recurring(0 => sub { say 'Reactor tick.' });
=head2 C<remove>
my $success = $reactor->remove($handle);
my $success = $reactor->remove($id);
Remove handle or timer.
=head2 C<start>
$reactor->start;
Expand Down

0 comments on commit 37a73eb

Please sign in to comment.