Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move nonblocking/streaming check to the middleware akin to Writer
- Loading branch information
Showing
3 changed files
with
33 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Tatsumaki::Middleware::Blocking; | ||
use strict; | ||
use base qw(Plack::Middleware); | ||
use Carp (); | ||
use Plack::Util; | ||
|
||
# Run asnynchronous Tatsumaki app in a blocking mode. See also Middleware::Writer | ||
sub call { | ||
my($self, $env) = @_; | ||
|
||
my $caller_supports_streaming = $env->{'psgi.streaming'}; | ||
$env->{'psgi.streaming'} = Plack::Util::TRUE; | ||
|
||
my $res = $self->app->($env); | ||
return $res if $caller_supports_streaming; | ||
|
||
if (ref $res eq 'CODE') { | ||
$env->{'psgi.errors'}->print("psgi.nonblocking is off: running $env->{PATH_INFO} in a blocking mode\n"); | ||
$res->(sub { $res = shift }); | ||
$env->{'tatsumaki.block'}->(); | ||
} | ||
|
||
unless (defined $res->[2]) { | ||
Carp::croak("stream_write is not supported on this server"); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
1; |