| 1 | package Net::Async::AMQP::RPC::Client; | ||||||||||||||||||||
| 2 | 1 | $Net::Async::AMQP::RPC::Client::VERSION = '2.000'; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use parent qw(Net::Async::AMQP::RPC::Base); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Net::Async::AMQP::RPC::Client - client RPC handling | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | version 2.000 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | Provides a client implementation for RPC handling. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =over 4 | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =item * Declare a new temporary queue | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =item * Start a consumer on the queue | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =item * Publish requests to the RPC exchange, using our (server-assigned) queue name as the reply_to address | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =item * Responses to our queue are matched against original requests using the correlation_id field | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =back | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.8
| |||||||||||||||||||||
| 37 | eval { | ||||||||||||||||||||
| 38 | 1 | require UUID::Tiny; | |||||||||||||||||||
| 39 | 0 | ▶ | *next_id = sub { UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1()) } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 40 | 0 | ▶ | } or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 1 | $log->warnf("No UUID::Tiny found, using custom fallback. Install the UUID::Tiny module to avoid this warning"); | |||||||||||||||||||
| 42 | # If we don't have a UUID implementation, get something that's | ||||||||||||||||||||
| 43 | # random-ish as a workaround | ||||||||||||||||||||
| 44 | 1 | my @chars = 'a'..'z'; | |||||||||||||||||||
| 45 | 1 | my $id = join '', | |||||||||||||||||||
| 46 | (map $chars[rand @chars], 1..8), | ||||||||||||||||||||
| 47 | (sprintf '%x', time), | ||||||||||||||||||||
| 48 | '000000' | ||||||||||||||||||||
| 49 | ; | ||||||||||||||||||||
| 50 | 0 | ▶ | *next_id = sub { ++$id } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 51 | 1 | } | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub request { | ||||||||||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 55 | 0 | my ($self, $type, $payload, %args) = @_; | |||||||||||||||||||
| 56 | 0 | my $id = $self->next_id; | |||||||||||||||||||
| 57 | Future->needs_all( | ||||||||||||||||||||
| 58 | $self->publisher_channel, | ||||||||||||||||||||
| 59 | $self->queue_name, | ||||||||||||||||||||
| 60 | $self->consumer, | ||||||||||||||||||||
| 61 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 62 | 0 | my ($ch, $queue_name) = @_; | |||||||||||||||||||
| 63 | 0 | $self->{pending_requests}{$id} = { | |||||||||||||||||||
| 64 | type => $type, | ||||||||||||||||||||
| 65 | future => my $f = $self->loop->new_future->set_label('RPC response for ' . $id), | ||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||
| 67 | 0 | $log->debugf( | |||||||||||||||||||
| 68 | "Publishing with correlation ID [%s] and reply_to [%s], type %s", | ||||||||||||||||||||
| 69 | $id, | ||||||||||||||||||||
| 70 | $queue_name, | ||||||||||||||||||||
| 71 | $type | ||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||
| 73 | $ch->publish( | ||||||||||||||||||||
| 74 | exchange => $self->exchange, | ||||||||||||||||||||
| 75 | routing_key => $self->routing_key, | ||||||||||||||||||||
| 76 | reply_to => $queue_name, | ||||||||||||||||||||
| 77 | delivery_mode => 2, # persistent | ||||||||||||||||||||
| 78 | correlation_id => $id, | ||||||||||||||||||||
| 79 | type => $type, | ||||||||||||||||||||
| 80 | payload => $payload, | ||||||||||||||||||||
| 81 | %args | ||||||||||||||||||||
| 82 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 83 | 0 | $f | |||||||||||||||||||
| 84 | 0 | }); | |||||||||||||||||||
| 85 | 0 | }); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 1 | my $json; | |||||||||||||||||||
| 89 | 0 | ▶ | sub json_request { | ||||||||||||||||||
|
Subroutine
json_request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 90 | 0 | my ($self, $cmd, $args) = @_; | |||||||||||||||||||
| 91 | 0 | ▶ | $json ||= do { | ||||||||||||||||||
|
Condition0%0/3
$json ||= do {
eval {
do {
require JSON::MaybeXS
}
} or die "->json_request requires the JSON::MaybeXS module, which could not be loaded:\n$@";
$json = "JSON::MaybeXS"->new
}
MC/DC0%0/2
$json ||= do {
eval {
do {
require JSON::MaybeXS
}
} or die "->json_request requires the JSON::MaybeXS module, which could not be loaded:\n$@";
$json = "JSON::MaybeXS"->new
}
$json
do {
eval {
do {
require JSON::MaybeXS
}
} or die "->json_request requires the JSON::MaybeXS module, which could not be loaded:\n$@";
$json = "JSON::MaybeXS"->new
}
Truth table0%0/3
A ||= B
A= $jsonB= do {
eval {
do {
require JSON::MaybeXS
}
} or die "->json_request requires the JSON::MaybeXS module, which could not be loaded:\n$@";
$json = "JSON::MaybeXS"->new
}
| |||||||||||||||||||||
| 92 | 0 | ▶ | eval { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | require JSON::MaybeXS; | |||||||||||||||||||
| 94 | } or die "->json_request requires the JSON::MaybeXS module, which could not be loaded:\n$@"; | ||||||||||||||||||||
| 95 | 0 | $json = JSON::MaybeXS->new; | |||||||||||||||||||
| 96 | }; | ||||||||||||||||||||
| 97 | $self->request( | ||||||||||||||||||||
| 98 | $cmd, | ||||||||||||||||||||
| 99 | $json->encode($args), | ||||||||||||||||||||
| 100 | content_type => 'application/json', | ||||||||||||||||||||
| 101 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 102 | 0 | my $data = shift; | |||||||||||||||||||
| 103 | eval { | ||||||||||||||||||||
| 104 | 0 | Future->done($json->decode($data)) | |||||||||||||||||||
| 105 | 0 | ▶ | } or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | Future->fail("Invalid JSON data: " . $data); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | }); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub process_message { | ||||||||||||||||||
|
Subroutine
process_message: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 112 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 113 | # $log->infof("Have message: %s", join ' ', %args); | ||||||||||||||||||||
| 114 | 0 | ▶ | if(my $item = $self->{pending_requests}{$args{id}}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | ▶ | if($item->{type} eq $args{type}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | $item->{future}->done($args{payload}) | ||||||||||||||||||||
| 117 | 0 | } else { | |||||||||||||||||||
| 118 | 0 | $log->errorf("Have pending item ID %s but type does not match: had %s, expecting %s", $args{id}, $args{type}, $item->{type}); | |||||||||||||||||||
| 119 | 0 | $item->{future}->fail("invalid type"); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||
| 122 | $log->errorf("No pending request for ID %s, type %s", $args{id}, $args{type}) | ||||||||||||||||||||
| 123 | 0 | } | |||||||||||||||||||
| 124 | 0 | return ''; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | sub queue { shift->client_queue } | ||||||||||||||||||
|
Subroutine
queue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||