Created
November 13, 2012 16:57
-
-
Save zigorou/4066978 to your computer and use it in GitHub Desktop.
nekokak.psgi
This file contains hidden or 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
use strict; | |
use warnings; | |
use AE; | |
use AnyEvent; | |
use HTTP::Status qw(:constants); | |
use JSON; | |
use Plack::Request; | |
use Plack::Response; | |
use Time::HiRes qw(tv_interval gettimeofday); | |
my $app = sub { | |
my $env = shift; | |
my $w; | |
return sub { | |
my ($responde, $sock) = @_; | |
my $writer = $responde->([ | |
HTTP_OK, | |
[ "Content-Type" => "application/json" ], | |
]); | |
my $cv = AnyEvent->condvar; | |
$cv->begin; | |
my $t0 = [gettimeofday]; | |
$w = AnyEvent->timer( | |
after => 3, | |
cb => sub { | |
my $elapsed = tv_interval($t0, [gettimeofday]); | |
$writer->write(encode_json({ | |
pid => $$, | |
elapsed => $elapsed, | |
})); | |
$cv->end; | |
undef $w; | |
}, | |
); | |
$cv->cb(sub { | |
my $cv = shift; | |
$writer->write("\n\n"); | |
}); | |
}; | |
}; | |
$app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment