Async-Redis
view release on metacpan or search on metacpan
examples/pagi-chat/lib/ChatApp/HTTP.pm view on Meta::CPAN
warn "API error: $err";
return Future->done({ status => 500, data => { error => 'Internal error' } });
});
my $body = $JSON->encode($result->{data});
await $send->({
type => 'http.response.start',
status => $result->{status},
headers => [
['content-type', 'application/json; charset=utf-8'],
['content-length', length($body)],
],
});
await $send->({
type => 'http.response.body',
body => $body,
});
}
examples/pagi-chat/lib/ChatApp/HTTP.pm view on Meta::CPAN
open my $fh, '<:raw', $file_path or return await _send_500($send);
local $/;
$content = <$fh>;
close $fh;
}
await $send->({
type => 'http.response.start',
status => 200,
headers => [
['content-type', $content_type],
['content-length', length($content)],
],
});
await $send->({
type => 'http.response.body',
body => $content,
});
}
async sub _send_404 {
my ($send) = @_;
my $body = '{"error":"Not found"}';
await $send->({
type => 'http.response.start',
status => 404,
headers => [['content-type', 'application/json']],
});
await $send->({ type => 'http.response.body', body => $body });
}
async sub _send_500 {
my ($send) = @_;
my $body = '{"error":"Internal server error"}';
await $send->({
type => 'http.response.start',
status => 500,
headers => [['content-type', 'application/json']],
});
await $send->({ type => 'http.response.body', body => $body });
}
1;
examples/slow-redis/app.pl view on Meta::CPAN
$error = $@;
my $elapsed = sprintf("%.3f", time() - $start);
# Handle errors gracefully
if ($error) {
warn "[slow-redis] Worker $worker error: $error\n";
await $send->({
type => 'http.response.start',
status => 500,
headers => [['content-type', 'text/plain']],
});
await $send->({
type => 'http.response.body',
body => "Redis error: $error\n",
});
return;
}
# Build response
my $body = <<"EOF";
examples/slow-redis/app.pl view on Meta::CPAN
Run multiple concurrent requests - they should all complete in ~1 second total!
Test with:
for i in 1 2 3 4 5; do curl -s http://localhost:5001/ & done; wait
EOF
await $send->({
type => 'http.response.start',
status => 200,
headers => [
['content-type', 'text/plain; charset=utf-8'],
['x-worker-pid', "$worker"],
['x-elapsed', $elapsed],
],
});
await $send->({
type => 'http.response.body',
body => $body,
});
}
examples/slow-redis/app.pl view on Meta::CPAN
};
$error = $@;
my $elapsed = sprintf("%.6f", time() - $start);
if ($error) {
warn "[slow-redis] Worker $worker error: $error\n";
await $send->({
type => 'http.response.start',
status => 500,
headers => [['content-type', 'text/plain']],
});
await $send->({
type => 'http.response.body',
body => "Redis error: $error\n",
});
return;
}
my $body = "Fast: worker=$worker redis_time=$seconds.$microseconds elapsed=${elapsed}s\n";
await $send->({
type => 'http.response.start',
status => 200,
headers => [['content-type', 'text/plain']],
});
await $send->({
type => 'http.response.body',
body => $body,
});
}
# Lifespan handler
async sub _handle_lifespan {
my ($scope, $receive, $send) = @_;
( run in 2.147 seconds using v1.01-cache-2.11-cpan-524268b4103 )