| 1 | package Myriad::RPC::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::Implementation::Memory - microservice RPC in-memory implementation. | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | 1 | ▶ | use Sys::Hostname qw(hostname); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use Syntax::Keyword::Try qw( try :experimental(typed) ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | 1 | ▶ | use Role::Tiny::With; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | 1 | ▶ | use Myriad::Exception::General; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 0 | ▶ | use Myriad::RPC::Message; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | |||
| 26 | with 'Myriad::Role::RPC'; | ||
| 27 | |||
| 28 | has $transport; | ||
| 29 | |||
| 30 | has $group_name; | ||
| 31 | method group_name { $group_name //= 'processors' } | ||
| 32 | |||
| 33 | |||
| 34 | has $should_shutdown; | ||
| 35 | has $rpc_list; | ||
| 36 | has $processing; | ||
| 37 | |||
| 38 | method rpc_list { $rpc_list }; | ||
| 39 | |||
| 40 | method configure(%args) { | ||
| 41 | $transport = delete $args{transport} if exists $args{transport}; | ||
| 42 | $rpc_list //= []; | ||
| 43 | $processing //={}; | ||
| 44 | $self->next::method(%args); | ||
| 45 | } | ||
| 46 | |||
| 47 | =head1 METHODS | ||
| 48 | |||
| 49 | =head2 start | ||
| 50 | |||
| 51 | Start waiting for new requests to fill in the internal requests queue. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | async method start () { | ||
| 56 | $should_shutdown //= $self->loop->new_future(label => 'rpc::memory::shutdown_future')->without_cancel; | ||
| 57 | |||
| 58 | while (1) { | ||
| 59 | await &fmap_void($self->$curry::curry(async method ($rpc) { | ||
| 60 | unless ($rpc->{group}) { | ||
| 61 | my $pending_messages = await $transport->pending_stream_by_consumer($rpc->{stream}, $self->group_name, hostname()); | ||
| 62 | await $self->process_stream_messages(rpc => $rpc, messages => $pending_messages) if %$pending_messages; | ||
| 63 | await $transport->create_consumer_group($rpc->{stream}, $self->group_name, 0, 1); | ||
| 64 | $rpc->{group} = 1; | ||
| 65 | } | ||
| 66 | my $messages = await $transport->read_from_stream_by_consumer($rpc->{stream}, $self->group_name, hostname()); | ||
| 67 | await $self->process_stream_messages(rpc => $rpc, messages => $messages) if %$messages; | ||
| 68 | }), foreach => [ $self->rpc_list->@* ], concurrent => scalar $self->rpc_list->@*); | ||
| 69 | await Future->wait_any($should_shutdown, $self->loop->delay_future(after => 0.1)); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 process_stream_messages | ||
| 74 | |||
| 75 | Process and emit received messages, while making sure we respond to them. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | async method process_stream_messages (%args) { | ||
| 80 | |||
| 81 | my $rpc = $args{rpc}; | ||
| 82 | my $messages = $args{messages}; | ||
| 83 | |||
| 84 | for my $id (sort keys $messages->%*) { | ||
| 85 | my $message; | ||
| 86 | $processing->{$rpc->{stream}}->{$id} = $self->loop->new_future(label => "rpc::response::$rpc->{stream}::$id"); | ||
| 87 | try { | ||
| 88 | $messages->{$id}->{transport_id} = $id; | ||
| 89 | $message = Myriad::RPC::Message::from_hash($messages->{$id}->%*); | ||
| 90 | $rpc->{sink}->emit($message); | ||
| 91 | } catch ($e) { | ||
| 92 | if (blessed $e && $e->isa('Myriad::Exception::RPC::BadEncoding')) { | ||
| 93 | $log->warnf('Recived a dead message that we cannot parse, going to drop it.'); | ||
| 94 | $log->tracef("message was: %s", $messages->{$id}); | ||
| 95 | await $self->drop($rpc->{stream}, $id); | ||
| 96 | } else { | ||
| 97 | my ($service) = $rpc->{stream} =~ /service.(.*).rpc\//; | ||
| 98 | await $self->reply_error($service, $message, $e); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | my %pending = $processing->{$rpc->{stream}}->%*; | ||
| 103 | await $self->loop->delay_future(after => 0.1); | ||
| 104 | if ( keys %pending ) { | ||
| 105 | my @done = await Future->needs_all(values %pending); | ||
| 106 | delete $processing->{$rpc->{stream}}{$_} for @done; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 create_from_sink | ||
| 111 | |||
| 112 | Register and RPC call and save a reference to its L<Ryu::Sink>. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | method create_from_sink (%args) { | ||
| 117 | my $sink = $args{sink} // die 'need a sink'; | ||
| 118 | my $method = $args{method} // die 'need a method name'; | ||
| 119 | my $service = $args{service} // die 'need a service'; | ||
| 120 | |||
| 121 | push $rpc_list->@*, { | ||
| 122 | sink => $sink, | ||
| 123 | stream => $self->stream_name($service, $method), | ||
| 124 | group => 0 | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head2 stop | ||
| 129 | |||
| 130 | Gracefully stop the RPC processing. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | async method stop () { | ||
| 135 | $should_shutdown->done() unless $should_shutdown->is_ready; | ||
| 136 | } | ||
| 137 | |||
| 138 | =head2 reply_success | ||
| 139 | |||
| 140 | Reply to the requester with a success message. | ||
| 141 | |||
| 142 | In this implementation it's done by resolving the L<Future> calling C<done>. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | async method reply_success ($service, $message, $response) { | ||
| 147 | my $stream = $self->stream_name($service, $message->rpc); | ||
| 148 | $message->response = { response => $response }; | ||
| 149 | await $transport->publish($message->who, $message->as_json); | ||
| 150 | await $transport->ack_message($stream, $self->group_name, $message->transport_id); | ||
| 151 | $processing->{$stream}->{$message->transport_id}->done($message->transport_id) unless $processing->{$stream}->{$message->transport_id}->is_done; | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 reply_error | ||
| 155 | |||
| 156 | Reply to the requester with a failure message. | ||
| 157 | |||
| 158 | In this implementation it's done by resolving the L<Future> calling C<fail>. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | async method reply_error ($service, $message, $error) { | ||
| 163 | my $stream = $self->stream_name($service, $message->rpc); | ||
| 164 | $message->response = { error => { category => $error->category, message => $error->message, reason => $error->reason } }; | ||
| 165 | await $transport->publish($message->who, $message->as_json); | ||
| 166 | await $transport->ack_message($stream, $self->group_name, $message->transport_id); | ||
| 167 | $processing->{$stream}->{$message->transport_id}->done($message->transport_id) unless $processing->{$stream}->{$message->transport_id}->is_done; | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 drop | ||
| 171 | |||
| 172 | Drop the request because we can't reply to the requester. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | async method drop ($stream, $id) { | ||
| 177 | await $transport->ack_message($stream, $self->group_name, $id); | ||
| 178 | $processing->{$stream}->{$id}->done($id) unless $processing->{$stream}->{$id}->is_done; | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 stream_name | ||
| 182 | |||
| 183 | Get the stream name of the service the current template is | ||
| 184 | |||
| 185 | service.$service_name.rpc/$method | ||
| 186 | |||
| 187 | it takes: | ||
| 188 | |||
| 189 | =over 4 | ||
| 190 | |||
| 191 | =item * L<service> - the name of service | ||
| 192 | |||
| 193 | =item * L<method> - the name of the method | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | method stream_name ($service, $method) { | ||
| 200 | return "service.$service.rpc/$method"; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | =head1 AUTHOR | ||
| 206 | |||
| 207 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 208 | |||
| 209 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 210 | |||
| 211 | =head1 LICENSE | ||
| 212 | |||
| 213 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 214 | |||