Skip to content

Instantly share code, notes, and snippets.

@zigorou
Created November 13, 2012 16:57
Show Gist options
  • Save zigorou/4066978 to your computer and use it in GitHub Desktop.
Save zigorou/4066978 to your computer and use it in GitHub Desktop.
nekokak.psgi
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