Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed small memory leak in Mojolicious::Plugin::TagHelpers
  • Loading branch information
kraih committed Jul 31, 2012
1 parent 79d2731 commit 222871e
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 477 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.16 2012-07-31
- Improved documentation.
- Fixed small memory leak in Mojolicious::Plugin::TagHelpers.

3.15 2012-07-28
- Improved Mojo::Base to load IO::Handle.
Expand Down
17 changes: 7 additions & 10 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -7,16 +7,7 @@ has parts => sub { [] };

sub new {
my $self = shift->SUPER::new(@_);

# Default content parser
$self->on(
read => sub {
my ($self, $chunk) = @_;
$self->{multipart} .= $chunk;
$self->_parse_multipart;
}
);

$self->on(read => \&_read);
return $self;
}

Expand Down Expand Up @@ -224,6 +215,12 @@ sub _parse_multipart_preamble {
return;
}

sub _read {
my ($self, $chunk) = @_;
$self->{multipart} .= $chunk;
$self->_parse_multipart;
}

1;

=head1 NAME
Expand Down
15 changes: 6 additions & 9 deletions lib/Mojo/Content/Single.pm
Expand Up @@ -9,15 +9,7 @@ has auto_upgrade => 1;

sub new {
my $self = shift->SUPER::new(@_);

# Default content parser
$self->{read} = $self->on(
read => sub {
my ($self, $chunk) = @_;
$self->asset($self->asset->add_chunk($chunk));
}
);

$self->{read} = $self->on(read => \&_read);
return $self;
}

Expand Down Expand Up @@ -62,6 +54,11 @@ sub parse {
return $multi->parse;
}

sub _read {
my ($self, $chunk) = @_;
$self->asset($self->asset->add_chunk($chunk));
}

1;

=head1 NAME
Expand Down
52 changes: 26 additions & 26 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -33,8 +33,8 @@ sub client {
my ($self, $cb) = (shift, pop);
$self = $self->singleton unless ref $self;

# Manage connections
$self->_manage;
# Make sure connection manager is running
$self->_manager;

# New client
my $id = $self->_id;
Expand Down Expand Up @@ -119,8 +119,8 @@ sub server {
my ($self, $cb) = (shift, pop);
$self = $self->singleton unless ref $self;

# Manage connections
$self->_manage;
# Make sure connection manager is running
$self->_manager;

# New server
my $id = $self->_id;
Expand Down Expand Up @@ -229,26 +229,26 @@ sub _listening {

sub _manage {
my $self = shift;
$self->{manager} ||= $self->recurring(
$self->accept_interval => sub {
my $self = shift;

# Start listening if possible
$self->_listening;

# Close connections gracefully
my $connections = $self->{connections} ||= {};
while (my ($id, $c) = each %$connections) {
$self->_remove($id)
if $c->{finish} && (!$c->{stream} || !$c->{stream}->is_writing);
}

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

# Start listening if possible
$self->_listening;

# Close connections gracefully
my $connections = $self->{connections} ||= {};
while (my ($id, $c) = each %$connections) {
$self->_remove($id)
if $c->{finish} && (!$c->{stream} || !$c->{stream}->is_writing);
}

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

sub _manager {
my $self = shift;
$self->{manager} ||= $self->recurring($self->accept_interval => \&_manage);
}

sub _not_listening {
Expand Down Expand Up @@ -283,8 +283,8 @@ sub _remove {
sub _stream {
my ($self, $stream, $id) = @_;

# Manage connections
$self->_manage;
# Make sure connection manager is running
$self->_manager;

# Connect stream with reactor
$self->{connections}{$id}{stream} = $stream;
Expand Down
23 changes: 10 additions & 13 deletions lib/Mojo/Log.pm
Expand Up @@ -26,19 +26,7 @@ my $LEVEL = {debug => 1, info => 2, warn => 3, error => 4, fatal => 5};

sub new {
my $self = shift->SUPER::new(@_);

# Default logger
$self->on(
message => sub {
my $self = shift;
return unless my $handle = $self->handle;
flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless defined $handle->syswrite($self->format(@_));
flock $handle, LOCK_UN;
}
);

$self->on(message => \&_message);
return $self;
}

Expand Down Expand Up @@ -78,6 +66,15 @@ sub log {

sub warn { shift->log(warn => @_) }

sub _message {
my $self = shift;
return unless my $handle = $self->handle;
flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless defined $handle->syswrite($self->format(@_));
flock $handle, LOCK_UN;
}

1;

=head1 NAME
Expand Down
28 changes: 14 additions & 14 deletions lib/Mojolicious.pm
Expand Up @@ -86,14 +86,7 @@ sub new {
$self->plugin($_) for qw(EPLRenderer EPRenderer RequestTimer PoweredBy);

# Exception handling
$self->hook(
around_dispatch => sub {
my ($next, $c) = @_;
local $SIG{__DIE__}
= sub { ref $_[0] ? CORE::die($_[0]) : Mojo::Exception->throw(@_) };
$c->render_exception($@) unless eval { $next->(); 1 };
}
);
$self->hook(around_dispatch => \&_exception);

# Reduced log output outside of development mode
$self->log->level('info') unless $mode eq 'development';
Expand Down Expand Up @@ -156,12 +149,7 @@ sub handler {

# Dispatcher
unless ($self->{dispatch}) {
$self->hook(
around_dispatch => sub {
my ($next, $c) = @_;
$c->app->dispatch($c);
}
);
$self->hook(around_dispatch => \&_dispatch);
$self->{dispatch}++;
}

Expand Down Expand Up @@ -203,6 +191,18 @@ sub start { ($ENV{MOJO_APP} = shift)->commands->start(@_) }

sub startup { }

sub _dispatch {
my ($next, $c) = @_;
$c->app->dispatch($c);
}

sub _exception {
my ($next, $c) = @_;
local $SIG{__DIE__}
= sub { ref $_[0] ? CORE::die($_[0]) : Mojo::Exception->throw(@_) };
$c->render_exception($@) unless eval { $next->(); 1 };
}

1;

=head1 NAME
Expand Down
82 changes: 41 additions & 41 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -33,52 +33,22 @@ sub register {
$app->helper(content => sub { shift->render_content(@_) });

# Add "content_for" helper
$app->helper(
content_for => sub {
my ($self, $name) = (shift, shift);
$self->render_content($name, $self->render_content($name), @_);
}
);
$app->helper(content_for => \&_content_for);

# Add "current_route" helper
$app->helper(
current_route => sub {
return '' unless my $endpoint = shift->match->endpoint;
return $endpoint->name unless @_;
return $endpoint->name eq shift;
}
);
$app->helper(current_route => \&_current_route);

# Add "dumper" helper
$app->helper(
dumper => sub { shift; Data::Dumper->new([@_])->Indent(1)->Terse(1)->Dump }
);
$app->helper(dumper => \&_dumper);

# Add "include" helper
$app->helper(
include => sub {
my $self = shift;
my $template = @_ % 2 ? shift : undef;
my $args = {@_};
$args->{template} = $template if defined $template;

# "layout" and "extends" can't be localized
my $layout = delete $args->{layout};
my $extends = delete $args->{extends};

# Localize arguments
my @keys = keys %$args;
local @{$self->stash}{@keys} = @{$args}{@keys};

return $self->render_partial(layout => $layout, extend => $extends);
}
);
$app->helper(include => \&_include);

# Add "memorize" helper
my %mem;
$app->helper(
memorize => sub {
shift;
my $self = shift;
return '' unless ref(my $cb = pop) eq 'CODE';
my ($name, $args)
= ref $_[0] eq 'HASH' ? (undef, shift) : (shift, shift || {});
Expand All @@ -101,12 +71,42 @@ sub register {
);

# Add "url_with" helper
$app->helper(
url_with => sub {
my $self = shift;
return $self->url_for(@_)->query($self->req->url->query->clone);
}
);
$app->helper(url_with => \&_url_with);
}

sub _content_for {
my ($self, $name) = (shift, shift);
$self->render_content($name, $self->render_content($name), @_);
}

sub _current_route {
return '' unless my $endpoint = shift->match->endpoint;
return $endpoint->name unless @_;
return $endpoint->name eq shift;
}

sub _dumper { shift; Data::Dumper->new([@_])->Indent(1)->Terse(1)->Dump }

sub _include {
my $self = shift;
my $template = @_ % 2 ? shift : undef;
my $args = {@_};
$args->{template} = $template if defined $template;

# "layout" and "extends" can't be localized
my $layout = delete $args->{layout};
my $extends = delete $args->{extends};

# Localize arguments
my @keys = keys %$args;
local @{$self->stash}{@keys} = @{$args}{@keys};

return $self->render_partial(layout => $layout, extend => $extends);
}

sub _url_with {
my $self = shift;
return $self->url_for(@_)->query($self->req->url->query->clone);
}

1;
Expand Down

0 comments on commit 222871e

Please sign in to comment.