Skip to content

Commit

Permalink
deprecated many methods in Mojo::IOLoop and fixed multiple drain call…
Browse files Browse the repository at this point in the history
…back bugs
  • Loading branch information
kraih committed Nov 15, 2011
1 parent 8029e75 commit 5c9d0fd
Show file tree
Hide file tree
Showing 37 changed files with 1,027 additions and 1,319 deletions.
12 changes: 11 additions & 1 deletion Changes
@@ -1,9 +1,19 @@
This file documents the revision history for Perl extension Mojolicious.

2.27 2011-11-12 00:00:00
2.27 2011-11-15 00:00:00
- Deprecated Mojo::IOLoop->connect in favor of Mojo::IOLoop->client.
- Deprecated Mojo::IOLoop->listen in favor of Mojo::IOLoop->server.
- Deprecated Mojo::IOLoop->connection_timeout in favor of
Mojo::IOLoop->timeout.
- Deprecated Mojo::IOLoop->write in favor of
Mojo::IOLoop::Stream->write.
- Deprecated on_* methods in Mojo::IOLoop.
- Added EXPERIMENTAL is_readable method to Mojo::IOLoop::Stream.
- Added EXPERIMENTAL charset method to Mojo::Content.
- Added EXPERIMENTAL write event to Mojo::IOLoop::Stream.
- Improved documentation.
- Improved CSS of some built-in templates.
- Fixed multiple drain callback bugs.

2.26 2011-11-10 00:00:00
- Added EXPERIMENTAL upgrade event to Mojo::Asset::Memory.
Expand Down
97 changes: 55 additions & 42 deletions examples/connect-proxy.pl
Expand Up @@ -12,50 +12,63 @@
my $c = {};

# Minimal connect proxy server to test TLS tunneling
Mojo::IOLoop->listen(
port => 3000,
on_read => sub {
my ($loop, $client, $chunk) = @_;
if (my $server = $c->{$client}->{connection}) {
return $loop->write($server, $chunk);
}
$c->{$client}->{client} //= '';
$c->{$client}->{client} .= $chunk if defined $chunk;
if ($c->{$client}->{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $c->{$client}->{client};
$c->{$client}->{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
my $address = $1;
my $port = $2 || 80;
my $server = $loop->connect(
address => $address,
port => $port,
on_connect => sub {
my ($loop, $server) = @_;
say "Forwarding to $address:$port.";
$c->{$client}->{connection} = $server;
$loop->write($client,
"HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
},
on_read => sub {
my ($loop, $server, $chunk) = @_;
$loop->write($client, $chunk);
},
on_error => sub {
shift->drop($client);
delete $c->{$client};
Mojo::IOLoop->server(
{port => 3000} => sub {
my ($loop, $stream, $client) = @_;
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
if (my $server = $c->{$client}->{connection}) {
return Mojo::IOLoop->stream($server)->write($chunk);
}
$c->{$client}->{client} //= '';
$c->{$client}->{client} .= $chunk;
if ($c->{$client}->{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $c->{$client}->{client};
$c->{$client}->{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
my $address = $1;
my $port = $2 || 80;
my $server;
$server = Mojo::IOLoop->client(
{address => $address, port => $port} => sub {
my ($loop, $stream, $error) = @_;
if ($error) {
say "Connection error for $address:$port: $error";
Mojo::IOLoop->drop($client);
return delete $c->{$client};
}
say "Forwarding to $address:$port.";
$c->{$client}->{connection} = $server;
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
Mojo::IOLoop->stream($client)->write($chunk);
}
);
$stream->on(
close => sub {
Mojo::IOLoop->drop($client);
delete $c->{$client};
}
);
Mojo::IOLoop->stream($client)
->write("HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
}
);
}
);
}
else { Mojo::IOLoop->drop($client) }
}
else { $loop->drop($client) }
}
},
on_error => sub {
my ($self, $client) = @_;
shift->drop($c->{$client}->{connection})
if $c->{$client}->{connection};
delete $c->{$client};
);
$stream->on(
close => sub {
Mojo::IOLoop->drop($c->{$client}->{connection})
if $c->{$client}->{connection};
delete $c->{$client};
}
);
}
) or die "Couldn't create listen socket!\n";

Expand Down
15 changes: 8 additions & 7 deletions examples/flash-policy-server.pl
Expand Up @@ -24,13 +24,14 @@
EOF

# Flash policy server
Mojo::IOLoop->listen(
port => 843,
on_read => sub {
my ($loop, $id) = @_;

# Write XML
$loop->write($id, $xml, sub { shift->drop($id) });
Mojo::IOLoop->server(
{port => 843} => sub {
my ($loop, $stream, $id) = @_;
$stream->on(
read => sub {
shift->write($xml, sub { Mojo::IOLoop->drop($id) });
}
);
}
) or die "Couldn't create listen socket!\n";

Expand Down
56 changes: 25 additions & 31 deletions examples/microhttpd.pl
Expand Up @@ -13,44 +13,38 @@
my $buffer = {};

# Minimal ioloop example demonstrating how to cheat at HTTP benchmarks :)
Mojo::IOLoop->listen(
port => 3000,
on_accept => sub {
my ($loop, $id) = @_;

# Initialize buffer
Mojo::IOLoop->server(
{port => 3000} => sub {
my ($loop, $stream, $id) = @_;
$buffer->{$id} = '';
},
on_read => sub {
my ($loop, $id, $chunk) = @_;

# Append chunk to buffer
$buffer->{$id} .= $chunk;

# Check if we got start line and headers (no body support)
if (index($buffer->{$id}, "\x0d\x0a\x0d\x0a") >= 0) {

# Clean buffer
delete $buffer->{$id};

# Write a minimal HTTP response
# (the "Hello World!" message has been optimized away!)
$loop->write($id => "HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
}
},
on_error => sub {
my ($self, $id) = @_;

# Clean buffer
delete $buffer->{$id};
$stream->on(
read => sub {
my ($stream, $chunk) = @_;

# Append chunk to buffer
$buffer->{$id} .= $chunk;

# Check if we got start line and headers (no body support)
if (index($buffer->{$id}, "\x0d\x0a\x0d\x0a") >= 0) {

# Clean buffer
delete $buffer->{$id};

# Write a minimal HTTP response
# (the "Hello World!" message has been optimized away!)
$stream->write("HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
}
}
);
$stream->on(close => sub { delete $buffer->{$id} });
}
) or die "Couldn't create listen socket!\n";

print <<'EOF';
Starting server on port 3000.
Try something like "ab -c 30 -n 100000 -k http://127.0.0.1:3000/" for testing.
On a MacBook Pro 13" this results in about 20k req/s.
On a MacBook Pro 13" this results in about 15k req/s.
EOF

# Start loop
Expand Down
5 changes: 1 addition & 4 deletions lib/Mojo/Command.pm
Expand Up @@ -10,7 +10,7 @@ use Carp 'croak';
use Mojo::Server;
use Mojo::Template;
use Mojo::Loader;
use Mojo::Util qw/b64_decode camelize decamelize/;
use Mojo::Util qw/b64_decode decamelize/;

has hint => <<"EOF";
Expand Down Expand Up @@ -199,9 +199,6 @@ sub run {
my $module;
for my $namespace (@{$self->namespaces}) {
last if $module = _command("${namespace}::$name");

# DEPRECATED in Smiling Face With Sunglasses!
last if $module = _command("${namespace}::" . camelize $name);
}

# Command missing
Expand Down
30 changes: 0 additions & 30 deletions lib/Mojo/DOM.pm
Expand Up @@ -47,19 +47,6 @@ sub new {
return $self;
}

# DEPRECATED in Smiling Face With Sunglasses!
sub add_after {
warn "Mojo::DOM->add_after is DEPRECATED in favor of Mojo::DOM->append!\n";
shift->append(@_);
}

# DEPRECATED in Smiling Face With Sunglasses!
sub add_before {
warn
"Mojo::DOM->add_before is DEPRECATED in favor of Mojo::DOM->prepend!\n";
shift->prepend(@_);
}

sub all_text {
my ($self, $trim) = @_;
return $self->_text($self->tree, 1, defined $trim ? $trim : 1);
Expand Down Expand Up @@ -161,14 +148,6 @@ sub find {
return Mojo::Collection->new(@$results);
}

# DEPRECATED in Smiling Face With Sunglasses!
sub inner_xml {
warn <<EOF;
Mojo::DOM->inner_xml is DEPRECATED in favor of Mojo::DOM->content_xml!
EOF
shift->content_xml(@_);
}

sub namespace {
my $self = shift;

Expand Down Expand Up @@ -270,15 +249,6 @@ sub replace_content {
return $self;
}

# DEPRECATED in Smiling Face With Sunglasses!
sub replace_inner {
warn <<EOF;
Mojo::DOM->replace_inner is DEPRECATED in favor of
Mojo::DOM->replace_content!
EOF
shift->content_xml(@_);
}

sub root {
my $self = shift;

Expand Down

0 comments on commit 5c9d0fd

Please sign in to comment.