Commit 4a9e525c by PotatoGim

mojolicious: AnyEvent를 사용한 요청당 스태시 예제 구현

parent a296c986
#!/usr/bin/env perl
#===========================================================================
# FILE: per_req_stash_with_async.pl
# USAGE: ./test.pl
# DESCRIPTION: Example for Per-Request-Stash with Async-way in Mojo.
# VERSION: 0.1
# OPTIONS: ---
# NOTES: ---
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# COPYRIGHT: Copyleft(c) 2018 by Ji-Hyeon Gim.
# LICENSE: Artistic License 2.0
# DATE: 2018년 01월 10일 11시 10분 23초
#===========================================================================
use strict;
use warnings;
use utf8;
our $AUTHOR = "cpan:potatogim";
our $VERSION = "0.1";
use Mojolicious::Lite;
use Mojo::IOLoop;
use EV;
use AnyEvent;
use Data::UUID::MT;
use Mojo::UserAgent;
use Data::Dumper;
$Data::Dumper::Terse = 1;
our $UG = Data::UUID::MT->new();
our %PRS = (); # Per-Request-Stash
hook after_build_tx => sub {
my ($tx, $app) = @_;
print "AFTER BUILD TX\n";
$tx->req->on(
progress => sub {
my $req = shift;
$req->headers->is_finished || return;
my $rid = $UG->create_string();
$req->headers->header('X-RID' => $rid);
$req->unsubscribe('progress');
$PER_REQ_STASH{$rid} = time;
print "PER_REQ_STASH: ${\Dumper(\%PER_REQ_STASH)}\n";
}
);
};
hook before_routes => sub {
my $c = shift;
print "BEFORE ROUTES\n";
print "- REQ-HEADERS: ${\Dumper($c->req->headers)}";
my $rid = $c->req->headers->header('X-RID');
$c->res->headers->header('X-RID' => $rid);
};
hook after_dispatch => sub {
my $c = shift;
print "AFTER DISPATCH\n";
print "- RES-HEADERS: ${\Dumper($c->res->headers)}";
my $rid = $c->res->headers->header('X-RID');
$c->res->headers->remove('X-RID');
delete($PER_REQ_STASH{$rid});
print "- PER_REQ_STASH: ${\Dumper(\%PER_REQ_STASH)}\n";
};
get '/' => sub {
my $c = shift;
my $condvar = AnyEvent->condvar;
$condvar->begin(
sub {
print "\nAll requests has done!\n";
$condvar->send();
}
);
for (my $i=0; $i<3; $i++)
{
print "Request $i...\n";
$condvar->begin();
$c->ua->get(
'http://127.0.0.1:3000/other' => sub
{
my ($ua, $tx) = @_;
my $index = $i;
my $body = $tx->result->body;
chomp($body);
$body =~ s/(^\s+|\s+$)//g;
printf("%s\n", $body);
$condvar->end();
}
);
}
$condvar->end();
$condvar->recv();
$c->render(text => 'Hello Mojo!');
};
get '/other' => sub {
my $c = shift;
my $rid = $c->req->headers->header("X-RID");
Mojo::IOLoop->timer(
int(rand(10)) => sub
{
$c->render(text => "RID: $rid");
}
);
};
app->start;
=encoding utf8
=head1 NAME
per_req_stash_with_async.pl - Example for Per-Request-Stash with Async-way in Mojo.
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 AUTHOR
Ji-Hyeon Gim E<lt>potatogim@potatogim.netE<gt>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyleft(c) 2018 by Ji-Hyeon Gim.
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=head1 DATE
2018년 01월 10일 11시 10분 23초
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment