| 1 | package Myriad::RPC::Client::Implementation::Redis; | ||
| 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::Redis - microservice RPC client abstraction | ||
| 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::Implementation::Redis qw(stream_name_from_service); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | use Myriad::RPC::Message; | ||
| 23 | |||
| 24 | use constant DEFAULT_RPC_TIMEOUT_SECONDS => 30; | ||
| 25 | |||
| 26 | has $redis; | ||
| 27 | has $subscription; | ||
| 28 | has $pending_requests; | ||
| 29 | has $whoami; | ||
| 30 | has $current_id; | ||
| 31 | has $started; | ||
| 32 | |||
| 33 | BUILD { | ||
| 34 | $pending_requests = {}; | ||
| 35 | $whoami = Myriad::Util::UUID::uuid(); | ||
| 36 | $current_id = 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | method configure (%args) { | ||
| 40 | $redis = delete $args{redis} if $args{redis}; | ||
| 41 | } | ||
| 42 | |||
| 43 | method is_started() { | ||
| 44 | return defined $started ? $started : Myriad::Exception::InternalError->new(message => '->start was not called')->throw; | ||
| 45 | } | ||
| 46 | |||
| 47 | async method start() { | ||
| 48 | $started = $self->loop->new_future(label => 'rpc_client_subscription'); | ||
| 49 | my $sub = await $redis->subscribe($whoami); | ||
| 50 | $subscription = $sub->events->map('payload')->map(sub{ | ||
| 51 | try { | ||
| 52 | my $payload = $_; | ||
| 53 | $log->tracef('Received RPC response as %s', $payload); | ||
| 54 | |||
| 55 | my $message = Myriad::RPC::Message::from_json($payload); | ||
| 56 | |||
| 57 | if(my $pending = delete $pending_requests->{$message->message_id}) { | ||
| 58 | return $pending->done($message); | ||
| 59 | } | ||
| 60 | $log->tracef('No pending future for message %s', $message->message_id); | ||
| 61 | } catch ($e) { | ||
| 62 | $log->warnf('failed to parse rpc response due %s', $e); | ||
| 63 | } | ||
| 64 | })->completed; | ||
| 65 | |||
| 66 | $started->done('started'); | ||
| 67 | $log->tracef('Started RPC client subscription on %s', $whoami); | ||
| 68 | |||
| 69 | await $subscription; | ||
| 70 | } | ||
| 71 | |||
| 72 | async method call_rpc($service, $method, %args) { | ||
| 73 | my $pending = $self->loop->new_future(label => "rpc::request::${service}::${method}"); | ||
| 74 | |||
| 75 | my $message_id = $self->next_id; | ||
| 76 | my $timeout = delete $args{timeout} || DEFAULT_RPC_TIMEOUT_SECONDS; | ||
| 77 | my $deadline = time + $timeout; | ||
| 78 | |||
| 79 | my $request = Myriad::RPC::Message->new( | ||
| 80 | rpc => $method, | ||
| 81 | who => $whoami, | ||
| 82 | deadline => $deadline, | ||
| 83 | message_id => $message_id, | ||
| 84 | args => \%args, | ||
| 85 | ); | ||
| 86 | |||
| 87 | try { | ||
| 88 | await $self->is_started(); | ||
| 89 | |||
| 90 | $log->tracef('Sending rpc::request::%s::%s : %s', $service, $method, $request->as_hash); | ||
| 91 | my $stream_name = stream_name_from_service($service, $method); | ||
| 92 | $pending_requests->{$message_id} = $pending; | ||
| 93 | await $redis->xadd($stream_name => '*', $request->as_hash->%*); | ||
| 94 | |||
| 95 | # The subscription loop will parse the message for us | ||
| 96 | my $message = await Future->wait_any($self->loop->timeout_future(after => $timeout), $pending); | ||
| 97 | |||
| 98 | return $message->response->{response}; | ||
| 99 | } catch ($e) { | ||
| 100 | if ($e =~ /Timeout/) { | ||
| 101 | $e = Myriad::Exception::RPC::Timeout->new(reason => 'deadline is due'); | ||
| 102 | } else { | ||
| 103 | $e = Myriad::Exception::InternalError->new(reason => $e) unless blessed $e && $e->does('Myriad::Exception'); | ||
| 104 | } | ||
| 105 | $pending->fail($e); | ||
| 106 | delete $pending_requests->{$message_id}; | ||
| 107 | $e->throw(); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | async method stop { | ||
| 112 | $subscription->done(); | ||
| 113 | } | ||
| 114 | |||
| 115 | method next_id { | ||
| 116 | return $current_id++; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | =head1 AUTHOR | ||
| 122 | |||
| 123 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 124 | |||
| 125 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 126 | |||
| 127 | =head1 LICENSE | ||
| 128 | |||
| 129 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 130 | |||