lib/Myriad/RPC/Client/Implementation/Memory.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Myriad/Service/Remote.pm ↑ Summary lib/Myriad/RPC/Client/Implementation/Redis.pm »
1 package Myriad::RPC::Client::Implementation::Memory;
2  
3 1 ▶use Myriad::Class extends => qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '1.001'; # VERSION
6 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Myriad::RPC::Client::Implementation::Memory
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =cut
19  
20 1 ▶use Myriad::Util::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 ▶use Myriad::RPC::Message;
Subroutine BEGIN: called CC=1 SCAR=2.6
22  
23 has $transport;
24 has $whoami;
25 has $current_id;
26 has $subscription;
27 has $pending_requests;
28 has $started;
29  
30 BUILD {
31 $whoami = Myriad::Util::UUID::uuid();
32 $current_id = 0;
33 $pending_requests = {};
34 }
35  
36 method configure (%args) {
37 $transport = delete $args{transport} if $args{transport};
38 }
39  
40 method is_started() {
41 return defined $started ? $started : Myriad::Exception::InternalError->new(message => '->start was not called')->throw;
42 }
43  
44 async method start {
45 $started = $self->loop->new_future(label => 'rpc_client_subscription');
46 my $sub = await $transport->subscribe($whoami);
47 $subscription = $sub->each(sub {
48 try {
49 my $payload = $_;
50 my $message = Myriad::RPC::Message::from_json($payload);
51 if(my $pending = delete $pending_requests->{$message->message_id}) {
52 return $pending->done($message);
53 }
54 } catch ($e) {
55 $log->warnf('failed to parse rpc response due %s', $e);
56 }
57 })->completed();
58  
59 $started->done('started');
60  
61 await $subscription;
62 }
63  
64 async method stop {
65 $subscription->done();
66 }
67  
68 async method call_rpc ($service, $method, %args) {
69 my $pending = $self->loop->new_future(label => "rpc_request::${service}::{$method}");
70  
71 my $deadline = time + 5;
72 my $message_id = $current_id++;
73  
74 my $request = Myriad::RPC::Message->new(
75 rpc => $method,
76 who => $whoami,
77 deadline => $deadline,
78 message_id => $message_id,
79 args => \%args,
80 );
81  
82 $pending_requests->{$message_id} = $pending;
83 await $self->is_started();
84 await $transport->add_to_stream("service.$service.rpc/$method", $request->as_hash->%*);
85  
86 try {
87 my $message = await Future->wait_any(
88 $self->loop->timeout_future(at => $deadline),
89 $pending
90 );
91 return $message->response->{response};
92 } catch ($e) {
93 if ($e =~ /Timeout/) {
94 $e = Myriad::Exception::RPC::Timeout->new(reason => 'deadline is due');
95 } else {
96 $e = Myriad::Exception::InternalError->new(reason => $e) unless blessed $e && $e->does('Myriad::Exception');
97 }
98 $pending->fail($e);
99 delete $pending_requests->{$message_id};
100 $e->throw();
101 }
102 }
103  
104 method _add_to_loop ($loop) {
105 $self->start->retain();
106 $self->next::method($loop);
107 }
108  
109 1;
110  
111 =head1 AUTHOR
112  
113 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
114  
115 See L<Myriad/CONTRIBUTORS> for full details.
116  
117 =head1 LICENSE
118  
119 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
120  
« lib/Myriad/Service/Remote.pm ↑ Summary lib/Myriad/RPC/Client/Implementation/Redis.pm »