another go at better async support
John Napiorkowski [Mon, 13 May 2013 15:09:06 +0000 (11:09 -0400)]
fixed spelling

lib/Catalyst/Engine.pm
lib/Catalyst/Response.pm
lib/Catalyst/Upgrading.pod

index 3b7c7fa..b8d29a4 100644 (file)
@@ -57,24 +57,17 @@ See L<Catalyst>.
 Finalize body.  Prints the response output as blocking stream if it looks like
 a filehandle, otherwise write it out all in one go.  If there is no body in
 the response, we assume you are handling it 'manually', such as for nonblocking
-style or asynchronous streaming responses.
-
-By default we do not close the writer object in case we are in an event loop
-and there is deferred activity.  However if you have some sloppy code that is
-closing over an unweakened context ($c) this could lead to the writer NEVER
-being closed.  In versions of Catalyst 5.90030 and older, we used to forcibly
-close the writer in this method, but we no longer do that since it prevented us
-from introducing proper asynchronous support in Catalyst core.  If you have old
-code that is leaking context but was otherwise working and you don't want to fix
-your memory leaks (is really the best idea) you can force enable the old
-behavior (and lose asynchronous support) by setting the global configuration key
-C<aggressively_close_writer_on_finalize_body> to true.  See L<Catalyst::Upgrading>
-for more if you have this issue.
+style or asynchronous streaming responses.  You do this by calling L<\write>
+several times (which sends HTTP headers if needed) or you close over L<\write_fh>.
+
+See L<Catalyst::Response\write> and L<Catalyst::Response\write_fh> for more.
 
 =cut
 
 sub finalize_body {
     my ( $self, $c ) = @_;
+    return if $c->response->has_write_fh;
+
     my $body = $c->response->body;
     no warnings 'uninitialized';
     if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) {
@@ -90,11 +83,9 @@ sub finalize_body {
         $self->write( $c, $body );
     }
 
-    if($c->config->{aggressively_close_writer_on_finalize_body}) {
-      my $res = $c->response;
-      $res->_writer->close;
-      $res->_clear_writer;
-    }
+    my $res = $c->response;
+    $res->_writer->close;
+    $res->_clear_writer;
 
     return;
 }
index 6dc661e..8559f36 100644 (file)
@@ -26,7 +26,25 @@ has _writer => (
     predicate => '_has_writer',
 );
 
-sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer }
+has write_fh => (
+  is=>'ro',
+  predicate=>'has_write_fh',
+  lazy_build=>1);
+
+  sub _build_write_fh {
+    my $self = shift;
+    $self->_context->finalize_headers unless
+      $self->finalized_headers;
+    $self->_writer;
+  };
+
+sub DEMOLISH {
+  my $self = shift;
+  return if $self->has_write_fh;
+  if($self->_has_writer) {
+    $self->_writer->close
+  }
+}
 
 has cookies   => (is => 'rw', default => sub { {} });
 has body      => (is => 'rw', default => undef);
@@ -246,6 +264,40 @@ $res->code is an alias for this, to match HTTP::Response->code.
 
 Writes $data to the output stream.
 
+=head2 $res->write_fh
+
+Returns a PSGI $writer object that has two methods, write and close.  You can
+close over this object for asynchronous and nonblocking applications.  For
+example (assuming you are using a supporting server, like L<Twiggy>
+
+    package AsyncExample::Controller::Root;
+
+    use Moose;
+
+    BEGIN { extends 'Catalyst::Controller' }
+
+    sub prepare_cb {
+      my $write_fh = pop;
+      return sub {
+        my $message = shift;
+        $write_fh->write("Finishing: $message\n");
+        $write_fh->close;
+      };
+    }
+
+    sub anyevent :Local :Args(0) {
+      my ($self, $c) = @_;
+      my $cb = $self->prepare_cb($c->res->write_fh);
+
+      my $watcher;
+      $watcher = AnyEvent->timer(
+        after => 5,
+        cb => sub {
+          $cb->(scalar localtime);
+          undef $watcher; # cancel circular-ref
+        });
+    }
+
 =head2 $res->print( @data )
 
 Prints @data to the output stream, separated by $,.  This lets you pass
index 1807053..d1dd519 100644 (file)
@@ -5,40 +5,12 @@ Catalyst::Upgrading - Instructions for upgrading to the latest Catalyst
 =head1 Upgrading to Catalyst 5.90040
 
 This version of L<Catalyst> offers some support for using L<AnyEvent> and
-L<IO::Async> event loops in your application.  In order to achieve this goal
-we needed to make some changes to the way the we finalize the HTTP response
-such that sloppy code that closed over $c and leaked memory will no longer
-work in some manner.  For example you might accidently have:
+L<IO::Async> event loops in your application.  These changes should work
+fine for most applications however if you are already trying to perform
+some streaming, minor changes in this area of the code might affect your
+functionality.
 
-    $c->stash(my_model => sub { $c->model->find(shift) });
-
-If you have old code that leaks memory in this way but otherwise seemed to
-work, it will no longer complete the response properly.
-
-If you don't want to fix your code, you can force the old behavior with the
-global configuration key C<aggressively_close_writer_on_finalize_body>.  This
-of course will still leave you with a leaky application and you lose the new
-event loop support, but your application will go back to completing its
-response output.  For example:
-
-    package MyApp::Web;
-
-    use Moose;
-    use Catalyst;
-
-    __PACKAGE__->config(
-      name => 'MyApp::Web',
-      enable_catalyst_header => 1,
-      disable_component_resolution_regex_fallback => 1,
-      aggressively_close_writer_on_finalize_body => 1,
-    );
-
-    __PACKAGE__->setup;
-
-See L<Catalyst::Component::ContextClosure> for help on how to close over the
-context safely, should you need to do this.  See L<CatalystX::LeakChecker>
-and L<Catalyst::Controller::LeakTracker> for help if you want to solve your
-memory leak issues.
+  TDB: more on streaming, transfer encoding chunked, etc.
 
 =head1 Upgrading to Catalyst 5.9