Tiny.pm

filter: Statementstmt 80.9% 369 / 456 Branchbran 65.6% 193 / 294 Conditioncond 49.3% 40 / 81 MC/DCmcdc 25.0% 12 / 48 Subroutinesub 88.6% 39 / 44 Podpod 22.7% 5 / 22 totaltotal 69.6% 658 / 945 CC 180 SCAR 29.5
CC
180
Cov
72%
worker
44.5
rpc_stopped
39.2
client
38.7
CRAP 19.2
↑ Summary Tiny/Async.pm »
1 # Lightweight client for the RPC-Switch json-rpc request multiplexer
2 #
3 # see: RPC::Switch: https://github.com/a6502/rpc-switch
4 # RPC::Switch::Client: https://metacpan.org/pod/RPC::Switch::Client
5 #
6 package RPC::Switch::Client::Tiny;
7  
8 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use RPC::Switch::Client::Tiny::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use RPC::Switch::Client::Tiny::Netstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use RPC::Switch::Client::Tiny::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use RPC::Switch::Client::Tiny::SessionCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 21 our $VERSION = '1.69';
21  
22 235 ▶sub new {
Subroutine new: called CC=11 SCAR=25.2
Pod documented
23 235 my ($class, %args) = @_;
24 235 ▶ my $s = $args{sock} or croak __PACKAGE__ . " expects sock";
Branch: 1 branch
BranchTrueFalseTotal
my $s = $args{"sock"} or croak("RPC::Switch::Client::Tiny expects sock") 235 0 235
25 235 ▶ unless ($^O eq 'MSWin32') { # cpantester: strawberry perl does not support blocking() call
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 235 0 235
26 235 ▶ defined(my $b = $s->blocking()) or croak __PACKAGE__ . " bad socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $b = $s->blocking) or croak('RPC::Switch::Client::Tiny' . " bad socket: $!") 235 0 235
27 235 ▶ unless ($b) { croak __PACKAGE__ . " nonblocking socket not supported"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($b) 19 216 235
28 }
29 0 ▶ unless (exists $args{who}) { croak __PACKAGE__ . " expects who"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"who"}) 0 216 216
30 216 my $self = bless {
31 %args,
32 id => 1, # next request id
33 state => '', # last rpcswitch.type
34 reqs => {}, # outstanding requests
35 channels => {}, # open rpcswitch channels
36 methods => {}, # defined worker methods
37 announced => {}, # announced worker methods
38 msglimit => 999999, # max netstring size
39 }, $class;
40 216 ▶ if (ref($self->{sock}) eq 'IO::Socket::SSL') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'sock'} eq 'IO::Socket::SSL') { } 1 215 216
41 1 ▶ $self->{auth_method} = 'clientcert' unless exists $self->{auth_method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"auth_method"} 1 0 1
42 1 ▶ $self->{token} = $self->{who} unless exists $self->{token}; # should be optional for clientcert
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"token"} 1 0 1
43 } else {
44 215 ▶ $self->{auth_method} = 'password' unless exists $self->{auth_method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"auth_method"} 0 215 215
45 }
46 216 ▶ $self->{json_utf8} = $self->{client_encoding_utf8} ? {} : {utf8 => 1};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'client_encoding_utf8'} ? : 38 178 216
47 216 return $self;
48 }
49  
50 76 ▶sub rpc_error {
Subroutine rpc_error: called CC=1 SCAR=0.0
Pod undocumented
51 76 return RPC::Switch::Client::Tiny::Error->new(@_);
52 }
53  
54 868 ▶sub rpc_send {
Subroutine rpc_send: called CC=4 SCAR=14.7
Pod undocumented
55 868 my ($self, $msg) = @_;
56 868 my $s = $self->{sock};
57 868 my $len = length($s);
58 868 ▶ if ($self->{msglimit} && ($len > $self->{msglimit})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"msglimit"} and $len > $self->{"msglimit"}) 0 868 868
Condition33%1/3
$self->{"msglimit"} and $len > $self->{"msglimit"}
!ll&&!rl&&r
08680
MC/DC0%0/2
$self->{"msglimit"} and $len > $self->{"msglimit"}
$self->{"msglimit"} $len > $self->{"msglimit"}
Truth table33%1/3
A and B
A= $self->{"msglimit"}B= $len > $self->{"msglimit"}
ABresult
0X0
100
111
59 0 warn "rpc_send msglimit exceeded: $len > $self->{msglimit}";
60 0 return;
61 }
62 868 $msg->{jsonrpc} = '2.0';
63 868 my $str = to_json($msg, {canonical => 1, %{$self->{json_utf8}}});
64 868 ▶ $self->{trace_cb}->('SND', $msg) if $self->{trace_cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace_cb"} 117 751 868
65 868 return netstring_write($s, $str);
66 }
67  
68 472 ▶sub rpc_send_req {
Subroutine rpc_send_req: called CC=3 SCAR=11.0
Pod undocumented
69 472 my ($self, $method, $msg) = @_;
70 472 my $id = "$self->{id}"; $self->{id}++;
71 472 $msg->{id} = $id;
72 472 $msg->{method} = $method;
73 472 $self->{reqs}{$id} = $method;
74 472 ▶ $self->{state} = $method if $method =~ /^rpcswitch\./;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^rpcswitch\./ 320 152 472
75 472 ▶ $self->rpc_send($msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->rpc_send($msg) or return 467 5 472
76 467 return $id;
77 }
78  
79 176 ▶sub rpc_send_call {
Subroutine rpc_send_call: called CC=2 SCAR=7.6
Pod undocumented
80 176 my ($self, $method, $params, $reqauth) = @_;
81  
82 176 ▶ if (defined $reqauth) { # request authentication
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reqauth) { } 0 176 176
83 # Without the vcookie the rpcswitch does not validate
84 # the reqauth parameter.
85 # The vcookie 'eatme' value is hardcoded in the rpc-switch
86 # code and is called 'channel information version'.
87 #
88 0 return $self->rpc_send_req($method, {params => $params, rpcswitch => {vcookie => 'eatme', reqauth => $reqauth}});
89 } else {
90 176 return $self->rpc_send_req($method, {params => $params});
91 }
92 }
93  
94 850 ▶sub rpc_decode {
Subroutine rpc_decode: called CC=12 SCAR=29.9
Pod undocumented
95 850 my ($self, $msg) = @_;
96 850 my ($req, $rsp) = ('', '');
97  
98 850 ▶ unless (($msg->{jsonrpc} eq '2.0') && (exists $msg->{id} || exists $msg->{method})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->{"jsonrpc"} eq "2.0" and exists $msg->{"id"} || exists $msg->{"method"}) 0 850 850
Condition50%3/6
$msg->{"jsonrpc"} eq "2.0" and exists $msg->{"id"} || exists $msg->{"method"}
!ll&&!rl&&r
00850
exists $msg->{"id"} or exists $msg->{"method"}
l!l&&r!l&&!r
6781720
MC/DC0%0/3
$msg->{"jsonrpc"} eq "2.0" and exists $msg->{"id"} || exists $msg->{"method"}
$msg->{"jsonrpc"} eq "2.0" exists $msg->{"id"} exists $msg->{"method"}
Truth table50%2/4
A and B or C
A= $msg->{"jsonrpc"} eq "2.0"B= exists $msg->{"id"}C= exists $msg->{"method"}
ABCresult
0XX0
1000
11X1
1011
99 0 die rpc_error('jsonrpc', "bad json-rpc: ".to_json($msg, {canonical => 1}));
100 }
101 850 ▶ if (exists $msg->{method}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $msg->{'method'}) { } 421 429 850
elsif (not defined $msg->{'id'}) { } 0 429 429
elsif (not exists $self->{'reqs'}{$msg->{'id'}}) { } 0 429 429
102 421 $req = $msg->{method};
103 } elsif (!defined $msg->{id}) {
104 0 die rpc_error('jsonrpc', "bad response id: ".to_json($msg, {canonical => 1}));
105 } elsif (!exists $self->{reqs}{$msg->{id}}) {
106 0 die rpc_error('jsonrpc', "unknown response $msg->{id}: ".to_json($msg, {canonical => 1}));
107 } else {
108 429 $rsp = delete $self->{reqs}{$msg->{id}};
109  
110 429 ▶ if (exists $msg->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg->{"error"}) 0 429 429
111 0 die rpc_error('jsonrpc', "$rsp $msg->{id} response error: $msg->{error}{message}", {code => $msg->{error}{code}});
112 }
113 429 ▶ if (!exists $msg->{result}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $msg->{"result"}) 0 429 429
114 0 die rpc_error('rpcswitch', "$rsp $msg->{id} response error: result missing");
115 }
116 429 ▶ if ((ref($msg->{result}) ne 'ARRAY') && ($rsp ne 'rpcswitch.ping') && ($rsp ne 'rpcswitch.withdraw')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg->{"result"} ne "ARRAY" and $rsp ne "rpcswitch.ping" and $rsp ne "rpcswitch.withdraw") 0 429 429
Condition83%5/6
ref $msg->{"result"} ne "ARRAY" and $rsp ne "rpcswitch.ping" and $rsp ne "rpcswitch.withdraw"
!ll&&!rl&&r
42180
ref $msg->{"result"} ne "ARRAY" and $rsp ne "rpcswitch.ping"
!ll&&!rl&&r
402198
MC/DC0%0/3
ref $msg->{"result"} ne "ARRAY" and $rsp ne "rpcswitch.ping" and $rsp ne "rpcswitch.withdraw"
ref $msg->{"result"} ne "ARRAY" $rsp ne "rpcswitch.ping" $rsp ne "rpcswitch.withdraw"
Truth table75%3/4
A and B and C
A= ref $msg->{"result"} ne "ARRAY"B= $rsp ne "rpcswitch.ping"C= $rsp ne "rpcswitch.withdraw"
ABCresult
0XX0
10X0
1100
1111
117 0 die rpc_error('rpcswitch', "$rsp $msg->{id} bad response: $msg->{result}");
118 }
119 }
120 850 return ($req, $rsp);
121 }
122  
123 159 ▶sub rpc_worker_announce {
Subroutine rpc_worker_announce: called CC=6 SCAR=18.0
Pod undocumented
124 159 my ($self, $workername) = @_;
125  
126 # ignore repeated announce request or unfinished withdraw
127 #
128 159 ▶ return if (keys %{$self->{announced}});
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"announced"};} 29 130 159
129  
130 130 foreach my $method (keys %{$self->{methods}}) {
131 145 ▶ next if exists $self->{methods}{$method}{id}; # active announce/withdraw request
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"methods"}{$method}{"id"} 10 135 145
132  
133 135 my $params = {method => $method, workername => $workername, doc => $self->{methods}{$method}{doc}};
134 135 ▶ $params->{filter} = $self->{methods}{$method}{filter} if exists $self->{methods}{$method}{filter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"methods"}{$method}{"filter"} 0 135 135
135 135 my $id = $self->rpc_send_req('rpcswitch.announce', {params => $params});
136 135 ▶ die rpc_error('io', 'netstring_write') unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 135 135
137 135 $self->{methods}{$method}{id} = $id;
138 }
139 130 return;
140 }
141  
142 31 ▶sub rpc_worker_withdraw {
Subroutine rpc_worker_withdraw: called CC=5 SCAR=16.2
Pod undocumented
143 31 my ($self) = @_;
144  
145 # callers will get code -32006 'opposite end of channel gone'
146 # errors when the announcement is withdrawn.
147 #
148 31 foreach my $method (keys %{$self->{announced}}) {
149 16 ▶ next if exists $self->{methods}{$method}{id}; # active announce/withdraw request
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"methods"}{$method}{"id"} 8 8 16
150  
151 8 my $params = {method => $method};
152 8 ▶ $params->{filter} = $self->{methods}{$method}{filter} if exists $self->{methods}{$method}{filter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"methods"}{$method}{"filter"} 0 8 8
153 8 my $id = $self->rpc_send_req('rpcswitch.withdraw', {params => $params});
154 8 ▶ die rpc_error('io', 'netstring_write') unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 8 8
155 8 $self->{methods}{$method}{id} = $id;
156 }
157 31 return;
158 }
159  
160 570 ▶sub rpc_worker_flowcontrol {
Subroutine rpc_worker_flowcontrol: called CC=6 SCAR=17.9
Pod undocumented
161 570 my ($self, $workername) = @_;
162  
163 # need to be in connected auth state
164 570 ▶ return unless ($self->{state} && ($self->{state} ne 'rpcswitch.hello'));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} and $self->{"state"} ne "rpcswitch.hello" 132 438 570
Condition100%3/3
$self->{"state"} and $self->{"state"} ne "rpcswitch.hello"
!ll&&!rl&&r
6666438
MC/DC100%2/2
$self->{"state"} and $self->{"state"} ne "rpcswitch.hello"
$self->{"state"} $self->{"state"} ne "rpcswitch.hello"
Truth table100%3/3
A and B
A= $self->{"state"}B= $self->{"state"} ne "rpcswitch.hello"
ABresult
0X0
100
111
165  
166 438 ▶ if ($self->{flowcontrol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flowcontrol"}) 102 336 438
167 102 my $cnt = (scalar keys %{$self->{async}{jobs}}) + (scalar @{$self->{async}{jobqueue}});
168 #printf ">> flow: %d %d %d\n", $cnt, $self->{async}{max_async} * 2, $self->{async}{max_async};
169 102 ▶ if ($cnt >= $self->{async}{max_async} * 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt >= $self->{'async'}{'max_async'} * 2) { } 31 71 102
elsif ($cnt < $self->{'async'}{'max_async'}) { } 44 27 71
170 31 $self->rpc_worker_withdraw();
171 } elsif ($cnt < $self->{async}{max_async}) {
172 44 $self->rpc_worker_announce($workername);
173 }
174 }
175 438 return;
176 }
177  
178 19 ▶sub valid_worker_err {
Subroutine valid_worker_err: called CC=3 SCAR=11.4
Pod undocumented
179 19 my ($err) = @_;
180 19 ▶ $err = {text => $err} unless ref($err); # convert plain errors
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 0 19 19
181 19 ▶ $err->{class} = 'hard' unless exists $err->{class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $err->{"class"} 0 19 19
182 19 return $err;
183 }
184  
185 245 ▶sub rpcswitch_resp {
Subroutine rpcswitch_resp: called CC=1 SCAR=0.0
Pod undocumented
186 245 my ($rpcswitch) = @_;
187  
188 # Just the vcookie & vci-channel parameters are required by
189 # the rpcswitch. The worker_id field is optional, and might
190 # be set to the worker_id returned by the announce response.
191 #
192 245 $rpcswitch = {vcookie => $rpcswitch->{vcookie}, vci => $rpcswitch->{vci}};
193 #$rpcswitch = {vcookie => $rpcswitch->{vcookie}, vci => $rpcswitch->{vci}, worker_id => $rpcswitch->{worker_id}};
194 245 return $rpcswitch;
195 }
196  
197 228 ▶sub client {
Subroutine client: called CC=18 SCAR=36.6
Pod documented
198 228 my ($self, $msg, $method, $params, $reqauth) = @_;
199 228 my ($req, $rsp) = $self->rpc_decode($msg);
200  
201 228 ▶ if ($req eq 'rpcswitch.greetings') {
Branch: 6 branches
BranchTrueFalseTotal
if ($req eq 'rpcswitch.greetings') { } 38 190 228
elsif ($rsp eq 'rpcswitch.hello') { } 19 171 190
elsif ($rsp eq 'rpcswitch.ping') { } 19 152 171
elsif ($rsp eq $method) { } 133 19 152
elsif ($req eq 'rpcswitch.result') { } 0 19 19
elsif ($req eq 'rpcswitch.channel_gone') { } 19 0 19
202 38 ▶ my %token = $self->{token} ? (token => $self->{token}) : (); # should be optional for clientcert
Branch: 1 branch
BranchTrueFalseTotal
$self->{'token'} ? : 38 0 38
203 38 my $helloparams = {who => $self->{who}, %token, method => $self->{auth_method}};
204 38 $self->rpc_send_req('rpcswitch.hello', {params => $helloparams});
205 } elsif ($rsp eq 'rpcswitch.hello') {
206 19 ▶ if (!$msg->{result}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->{"result"}[0]) 0 19 19
207 0 die rpc_error('rpcswitch', "$rsp failed: $msg->{result}[1]");
208 }
209 19 $self->rpc_send_call($method, $params, $reqauth);
210 } elsif ($rsp eq 'rpcswitch.ping') {
211 19 return [$msg->{result}]; # ping complete
212 } elsif ($rsp eq $method) {
213 133 ▶ if (exists $msg->{rpcswitch}) { # internal rpcswitch methods have no channel
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg->{"rpcswitch"}) 133 0 133
214 133 $self->{channels}{$msg->{rpcswitch}{vci}} = 0; # wait for channel_gone
215 }
216 133 ▶ if ($msg->{result}[0] eq 'RES_WAIT') { # async worker notification (might use trace_cb to dump)
Branch: 3 branches
BranchTrueFalseTotal
if ($msg->{'result'}[0] eq 'RES_WAIT') { } 19 114 133
elsif ($msg->{'result'}[0] eq 'RES_ERROR') { } 19 95 114
elsif ($msg->{'result'}[0] eq 'RES_OK') { } 95 0 95
217 19 $self->{channels}{$msg->{rpcswitch}{vci}} = $msg->{result}[1]; # msg id
218 } elsif ($msg->{result}[0] eq 'RES_ERROR') { # worker error
219 19 my $e = valid_worker_err($msg->{result}[1]);
220 19 die rpc_error('worker', to_json($e), $e);
221 } elsif ($msg->{result}[0] eq 'RES_OK') {
222 95 return [@{$msg->{result}}[1..$#{$msg->{result}}]]; # client result[1..$]
223 }
224 } elsif ($req eq 'rpcswitch.result') {
225 0 my $channel = $msg->{rpcswitch}{vci};
226 0 ▶ if (($msg->{params}[0] eq 'RES_OK') && ($msg->{params}[1] eq $self->{channels}{$channel})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg->{'params'}[0] eq 'RES_OK' and $msg->{'params'}[1] eq $self->{'channels'}{$channel}) { } 0 0 0
elsif ($msg->{'params'}[0] eq 'RES_ERROR' and $msg->{'params'}[1] eq $self->{'channels'}{$channel}) { } 0 0 0
Condition0%0/3
$msg->{"params"}[0] eq "RES_OK" and $msg->{"params"}[1] eq $self->{"channels"}{$channel}
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->{"params"}[0] eq "RES_OK" and $msg->{"params"}[1] eq $self->{"channels"}{$channel}
$msg->{"params"}[0] eq "RES_OK" $msg->{"params"}[1] eq $self->{"channels"}{$channel}
Truth table0%0/3
A and B
A= $msg->{"params"}[0] eq "RES_OK"B= $msg->{"params"}[1] eq $self->{"channels"}{$channel}
ABresult
0X0
100
111
227 0 return [@{$msg->{params}}[2..$#{$msg->{params}}]]; # client result[2..$] (notification)
228 ▶ } elsif (($msg->{params}[0] eq 'RES_ERROR') && ($msg->{params}[1] eq $self->{channels}{$channel})) {
Condition0%0/3
$msg->{"params"}[0] eq "RES_ERROR" and $msg->{"params"}[1] eq $self->{"channels"}{$channel}
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->{"params"}[0] eq "RES_ERROR" and $msg->{"params"}[1] eq $self->{"channels"}{$channel}
$msg->{"params"}[0] eq "RES_ERROR" $msg->{"params"}[1] eq $self->{"channels"}{$channel}
Truth table0%0/3
A and B
A= $msg->{"params"}[0] eq "RES_ERROR"B= $msg->{"params"}[1] eq $self->{"channels"}{$channel}
ABresult
0X0
100
111
229 0 my $e = valid_worker_err($msg->{params}[2]);
230 0 die rpc_error('worker', to_json($e), $e);
231 }
232 0 die rpc_error('rpcswitch', "bad msg: $msg->{params}[0] $msg->{params}[1]");
233 } elsif ($req eq 'rpcswitch.channel_gone') {
234 19 my $channel = $msg->{params}{channel};
235 19 ▶ if (exists $self->{channels}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"channels"}{$channel}) 19 0 19
236 19 my $id = delete $self->{channels}{$channel};
237 19 die rpc_error('rpcswitch', "$req for request $id: $channel");
238 }
239 0 die rpc_error('rpcswitch', "$req for unknown request: $channel");
240 } else {
241 0 die rpc_error('rpcswitch', "unsupported msg: ".to_json($msg, {canonical => 1}));
242 }
243 76 return;
244 }
245  
246 622 ▶sub worker {
Subroutine worker: called CC=21 SCAR=38.0
Pod documented
247 622 my ($self, $msg, $workername) = @_;
248 622 my ($req, $rsp) = $self->rpc_decode($msg);
249  
250 622 ▶ if ($req eq 'rpcswitch.greetings') {
Branch: 7 branches
BranchTrueFalseTotal
if ($req eq 'rpcswitch.greetings') { } 115 507 622
elsif ($rsp eq 'rpcswitch.hello') { } 115 392 507
elsif ($rsp eq 'rpcswitch.announce') { } 135 257 392
elsif ($req eq 'rpcswitch.ping') { } 0 257 257
elsif (exists $self->{'announced'}{$req}) { } 249 8 257
elsif ($rsp eq 'rpcswitch.withdraw') { } 8 0 8
elsif ($req eq 'rpcswitch.channel_gone') { } 0 0 0
251 115 ▶ my %token = $self->{token} ? (token => $self->{token}) : (); # should be optional for clientcert
Branch: 1 branch
BranchTrueFalseTotal
$self->{'token'} ? : 115 0 115
252 115 my $helloparams = {who => $self->{who}, %token, method => $self->{auth_method}};
253 115 $self->rpc_send_req('rpcswitch.hello', {params => $helloparams});
254 } elsif ($rsp eq 'rpcswitch.hello') {
255 115 ▶ if (!$msg->{result}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->{"result"}[0]) 0 115 115
256 0 die rpc_error('rpcswitch', "$rsp failed: $msg->{result}[1]");
257 }
258 115 $self->rpc_worker_announce($workername);
259 } elsif ($rsp eq 'rpcswitch.announce') {
260 135 ▶ if (!$msg->{result}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->{"result"}[0]) 0 135 135
261 0 die rpc_error('rpcswitch', "$rsp failed: $msg->{result}[1]");
262 }
263 135 ▶ my ($method) = grep { exists $self->{methods}{$_}{id} && $self->{methods}{$_}{id} eq $msg->{id} } keys %{$self->{methods}};
Condition100%3/3
exists $self->{'methods'}{$_}{'id'} && $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
!ll&&!rl&&r
1515135
MC/DC100%2/2
exists $self->{'methods'}{$_}{'id'} && $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
exists $self->{'methods'}{$_}{'id'} $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
Truth table100%3/3
A && B
A= exists $self->{'methods'}{$_}{'id'}B= $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
ABresult
0X0
100
111
264 135 ▶ if (!defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 0 135 135
265 0 die rpc_error('rpcswitch', "unknown $rsp response $msg->{id}: $msg->{result}[1]");
266 }
267 # register announced method
268 135 $self->{announced}{$method}{cb} = $self->{methods}{$method}{cb};
269 135 $self->{announced}{$method}{worker_id} = $msg->{result}[1]{worker_id};
270 135 delete $self->{methods}{$method}{id};
271 } elsif ($req eq 'rpcswitch.ping') {
272 0 $self->rpc_send({id => $msg->{id}, result => 'pong!'});
273 } elsif (exists $self->{announced}{$req}) {
274 249 $msg->{rpcswitch}{worker_id} = $self->{announced}{$req}{worker_id}; # save worker_id for response
275  
276 249 $self->{channels}{$msg->{rpcswitch}{vci}} = 0; # wait for channel_gone
277  
278 249 ▶ if ($self->{async}) { # use async call for forked childs
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'async'}) { } 200 49 249
279 200 $self->{async}->msg_enqueue($msg);
280 } else {
281 49 my $rpcswitch = rpcswitch_resp($msg->{rpcswitch});
282 49 my @resp = eval { $self->{announced}{$req}{cb}->($msg->{params}, $msg->{rpcswitch}) };
283 49 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 15 34 49
284 15 $self->rpc_send({id => $msg->{id}, result => ['RES_ERROR', $@], rpcswitch => $rpcswitch});
285 } else {
286 34 $self->rpc_send({id => $msg->{id}, result => ['RES_OK', @resp], rpcswitch => $rpcswitch});
287 }
288 }
289 } elsif ($rsp eq 'rpcswitch.withdraw') {
290 # Note: the rpcswitch sends just a boolean result here
291 #
292 8 ▶ if (!$msg->{result}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->{"result"}) 0 8 8
293 0 die rpc_error('rpcswitch', "$rsp failed: $msg->{result}");
294 }
295 8 ▶ my ($method) = grep { exists $self->{methods}{$_}{id} && $self->{methods}{$_}{id} eq $msg->{id} } keys %{$self->{methods}};
Condition33%1/3
exists $self->{'methods'}{$_}{'id'} && $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
!ll&&!rl&&r
008
MC/DC0%0/2
exists $self->{'methods'}{$_}{'id'} && $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
exists $self->{'methods'}{$_}{'id'} $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
Truth table33%1/3
A && B
A= exists $self->{'methods'}{$_}{'id'}B= $self->{'methods'}{$_}{'id'} eq $msg->{'id'}
ABresult
0X0
100
111
296 8 ▶ if (!defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 0 8 8
297 0 die rpc_error('rpcswitch', "unknown $rsp response $msg->{id}: $msg->{result}");
298 }
299 # remove announced method
300 8 delete $self->{announced}{$method};
301 8 delete $self->{methods}{$method}{id};
302 } elsif ($req eq 'rpcswitch.channel_gone') {
303 0 my $channel = $msg->{params}{channel};
304 0 ▶ if ($self->{async}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"async"}) 0 0 0
305 0 ▶ my ($childs, $msgs) = $self->{async}->jobs_terminate('gone', sub { $_[0]->{rpcswitch}{vci} eq $channel });
Subroutine __ANON__: not called CC=1 SCAR=6.9
306 0 ▶ if (@$msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$msgs) 0 0 0
307 0 warn "worker removed queued messages on channel gone: ".join(' ', map { $_->{id} } @$msgs);
308 }
309 }
310 0 ▶ if (exists $self->{channels}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'channels'}{$channel}) { } 0 0 0
311 0 delete $self->{channels}{$channel};
312 } else {
313 0 warn "worker $req for unknown request: $channel";
314 }
315 } else {
316 0 warn "worker unsupported msg: ".to_json($msg, {canonical => 1});
317 }
318 622 return;
319 }
320  
321 35 ▶sub is_session_req {
Subroutine is_session_req: called CC=4 SCAR=15.2
Pod undocumented
322 35 my ($self, $params) = @_;
323 35 ▶ return unless $self->{sessioncache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sessioncache"} 14 21 35
324  
325 21 ▶ if (exists $params->{session} && exists $params->{session}{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"session"} and exists $params->{"session"}{"id"}) 21 0 21
Condition33%1/3
exists $params->{"session"} and exists $params->{"session"}{"id"}
!ll&&!rl&&r
0021
MC/DC0%0/2
exists $params->{"session"} and exists $params->{"session"}{"id"}
exists $params->{"session"} exists $params->{"session"}{"id"}
Truth table33%1/3
A and B
A= exists $params->{"session"}B= exists $params->{"session"}{"id"}
ABresult
0X0
100
111
326 21 return $params->{session};
327 }
328 0 return;
329 }
330  
331 35 ▶sub is_session_resp {
Subroutine is_session_resp: called CC=6 SCAR=22.1
Pod undocumented
332 35 my ($self, $params) = @_;
333 35 ▶ return unless $self->{sessioncache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sessioncache"} 14 21 35
334  
335 21 ▶ if ((ref($params) eq 'ARRAY') && ($params->[0] eq 'RES_OK') && ref($params->[2]) && exists $params->[2]->{set_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "ARRAY" and $params->[0] eq "RES_OK" and ref $params->[2] and exists $params->[2]{"set_session"}) 21 0 21
Condition33%3/9
ref $params eq "ARRAY" and $params->[0] eq "RES_OK" and ref $params->[2] and exists $params->[2]{"set_session"}
!ll&&!rl&&r
0021
ref $params eq "ARRAY" and $params->[0] eq "RES_OK" and ref $params->[2]
!ll&&!rl&&r
0021
ref $params eq "ARRAY" and $params->[0] eq "RES_OK"
!ll&&!rl&&r
0021
MC/DC0%0/4
ref $params eq "ARRAY" and $params->[0] eq "RES_OK" and ref $params->[2] and exists $params->[2]{"set_session"}
ref $params eq "ARRAY" $params->[0] eq "RES_OK" ref $params->[2] exists $params->[2]{"set_session"}
Truth table20%1/5
A and B and C and D
A= ref $params eq "ARRAY"B= $params->[0] eq "RES_OK"C= ref $params->[2]D= exists $params->[2]{"set_session"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
336 21 return $params->[2]->{set_session};
337 }
338 0 return;
339 }
340  
341 18 ▶sub child_handler {
Subroutine child_handler: called CC=13 SCAR=26.5
Pod undocumented
342 18 my ($self, $wr) = @_;
343  
344 # The child has to explicitly close the ssl-socket without shutdown.
345 # Otherwise the parent will get an EOF.
346 # see: https://metacpan.org/pod/IO::Socket::SSL#Common-Usage-Errors
347 #
348 18 ▶ if (ref($self->{sock}) eq 'IO::Socket::SSL') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'sock'} eq 'IO::Socket::SSL') { } 0 18 18
349 0 $self->{sock}->close(SSL_no_shutdown => 1);
350 } else {
351 18 close($self->{sock});
352 }
353 18 $self->{sock} = $wr;
354 18 delete $self->{trace_cb};
355 18 local $SIG{INT} = 'DEFAULT';
356 18 local $SIG{PIPE} = 'IGNORE'; # handle sigpipe via print/write result
357  
358 # When session handling is enabled a child might process
359 # more than one request with the same session_id.
360 #
361 18 while (1) {
362 24 my $b = eval { netstring_read($self->{sock}) };
363 24 ▶ unless ($b) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($b) 4 20 24
364 4 ▶ next if ($@ && ($@ =~ /^EINTR/)); # interrupted
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $@ =~ /^EINTR/ 0 4 4
Condition33%1/3
$@ and $@ =~ /^EINTR/
!ll&&!rl&&r
400
MC/DC0%0/2
$@ and $@ =~ /^EINTR/
$@ $@ =~ /^EINTR/
Truth table33%1/3
A and B
A= $@B= $@ =~ /^EINTR/
ABresult
0X0
100
111
365 4 ▶ die "worker child: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
366 4 last; # EOF
367 }
368 20 my $msg = eval { from_json($b, {%{$self->{json_utf8}}}) };
369 20 ▶ die "worker child: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
370  
371 # The client catches all possible die() calls, so that it is
372 # guaranteed to call exit either from here or from a signal handler.
373 #
374 20 my $params;
375 20 my $callback = $self->{methods}{$msg->{method}}{cb};
376 20 my @resp;
377 20 eval {
378 20 local $SIG{PIPE} = 'DEFAULT'; # reenable sigpipe for worker code
379 20 @resp = $callback->($msg->{params}, $msg->{rpcswitch});
380 };
381 20 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 20 20
382 0 $params = ['RES_ERROR', $msg->{id}, $err];
383 } else {
384 20 $params = ['RES_OK', $msg->{id}, @resp];
385 }
386 20 $b = eval { to_json($params, {%{$self->{json_utf8}}}) };
387 20 ▶ return 1 if $@; # signal die from json encode
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
388 20 my $res = netstring_write($self->{sock}, $b);
389 20 ▶ return 2 unless $res; # signal socket error
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 20 20
390  
391 20 ▶ last unless $self->is_session_resp($params) || $self->is_session_req($msg->{params});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_session_resp($params) or $self->is_session_req($msg->{"params"}) 14 6 20
Condition66%2/3
$self->is_session_resp($params) or $self->is_session_req($msg->{"params"})
l!l&&r!l&&!r
6014
MC/DC50%1/2
$self->is_session_resp($params) or $self->is_session_req($msg->{"params"})
$self->is_session_resp($params) $self->is_session_req($msg->{"params"})
Truth table66%2/3
A or B
A= $self->is_session_resp($params)B= $self->is_session_req($msg->{"params"})
ABresult
1X1
011
000
392 }
393 18 ▶ close($self->{sock}) or return 3; # signal errors like broken pipe
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"sock"} or return 3 17 1 18
394 17 return 0;
395 }
396  
397 178 ▶sub _worker_child_write {
Subroutine _worker_child_write: called CC=2 SCAR=7.0
398 178 my ($self, $child, $msg) = @_;
399  
400 178 my $b = to_json($msg, {canonical => 1, %{$self->{json_utf8}}});
401 178 my $res = netstring_write($child->{reader}, $b); # forward request to worker child
402 178 ▶ die rpc_error('io', 'netstring_write') unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 178 178
403 178 return;
404 }
405  
406 196 ▶sub _worker_child_get {
Subroutine _worker_child_get: called CC=7 SCAR=25.7
407 196 my ($self, $msg) = @_;
408  
409 # First try to reuse child for existing session
410 #
411 196 ▶ if (my $sessioncache = $self->{sessioncache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sessioncache = $self->{"sessioncache"}) 21 175 196
412 21 ▶ if (my $session_req = $self->is_session_req($msg->{params})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $session_req = $self->is_session_req($msg->{'params'})) { } 21 0 21
elsif ($sessioncache->{'session_persist_user'}) { } 0 0 0
413 21 ▶ if (my $child = $sessioncache->session_get($session_req->{id}, $msg->{id}, $msg->{rpcswitch}{vci})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $sessioncache->session_get($session_req->{"id"}, $msg->{"id"}, $msg->{"rpcswitch"}{"vci"})) 7 14 21
414 7 return $child;
415 }
416 } elsif ($sessioncache->{session_persist_user}) {
417 0 ▶ if (exists $msg->{params}{$sessioncache->{session_persist_user}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg->{"params"}{$sessioncache->{"session_persist_user"}}) 0 0 0
418 0 my $user = $msg->{params}{$sessioncache->{session_persist_user}};
419 0 ▶ if (my $child = $sessioncache->session_get_per_user($user, $msg->{id}, $msg->{rpcswitch}{vci})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $sessioncache->session_get_per_user($user, $msg->{"id"}, $msg->{"rpcswitch"}{"vci"})) 0 0 0
420 0 delete $child->{session}; # reused session will be added after session_resp
421 0 return $child;
422 }
423 }
424 }
425 }
426 189 my $child = $self->{async}->child_start($self, $msg->{id}, $msg->{rpcswitch}{vci});
427 171 return $child;
428 }
429  
430 588 ▶sub _worker_childs_dequeue_and_run {
Subroutine _worker_childs_dequeue_and_run: called CC=4 SCAR=14.3
431 588 my ($self) = @_;
432  
433 588 ▶ while (my $msg = $self->{async}->msg_dequeue()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->{"async"}->msg_dequeue) 196 570 766
434 196 my $id = $msg->{id};
435 196 my $rpcswitch_resp = rpcswitch_resp($msg->{rpcswitch});
436  
437 196 my $child = eval { $self->_worker_child_get($msg) };
438 178 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 178 178
439 0 $self->rpc_send({id => $id, result => ['RES_ERROR', $@], rpcswitch => $rpcswitch_resp});
440 } else {
441 178 eval { $self->_worker_child_write($child, $msg) };
442 178 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 178 178
443 0 $self->rpc_send({id => $id, result => ['RES_ERROR', $@], rpcswitch => $rpcswitch_resp});
444 0 $self->{async}->child_finish($child, 'error');
445 } else {
446 178 $self->rpc_send({id => $id, result => ['RES_WAIT', $id], rpcswitch => $rpcswitch_resp});
447 178 $self->{async}->job_add($child, $msg->{id}, {rpcswitch => $rpcswitch_resp});
448 }
449 }
450 }
451 570 return;
452 }
453  
454 169 ▶sub _worker_child_read_and_finish {
Subroutine _worker_child_read_and_finish: called CC=15 SCAR=31.3
455 169 my ($self, $child) = @_;
456  
457 169 my $res;
458 169 my $b = eval { netstring_read($child->{reader}) };
459 169 ▶ unless ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 155 14 169
460 14 ▶ my $err = $@ ? $@ : 'EOF';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 14 14
461 14 $res = $self->rpc_send({method => 'rpcswitch.result', params => ['RES_ERROR', $child->{id}, $err], rpcswitch => $child->{rpcswitch}});
462 14 $self->{async}->child_finish($child, 'error');
463 } else {
464 155 my $params = eval { from_json($b, {%{$self->{json_utf8}}}) };
465 155 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 155 155
466 0 $res = $self->rpc_send({method => 'rpcswitch.result', params => ['RES_ERROR', $child->{id}, $@], rpcswitch => $child->{rpcswitch}});
467 0 $self->{async}->child_finish($child, 'error');
468 } else {
469 155 $res = $self->rpc_send({method => 'rpcswitch.result', params => $params, rpcswitch => $child->{rpcswitch}});
470 155 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 155 155
471 0 my $err = "result msg limit exceeded: " . length($b);
472 0 $res = $self->rpc_send({method => 'rpcswitch.result', params => ['RES_ERROR', $child->{id}, $err], rpcswitch => $child->{rpcswitch}});
473 0 $self->{async}->child_finish($child, 'error');
474 0 return $res;
475 }
476  
477 155 ▶ if (my $sessioncache = $self->{sessioncache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sessioncache = $self->{"sessioncache"}) 15 140 155
478 15 ▶ if (my $set_session = $self->is_session_resp($params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set_session = $self->is_session_resp($params)) 15 0 15
479 15 ▶ if (!exists $child->{session} || ($child->{session}{id} ne $set_session->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $child->{"session"} or $child->{"session"}{"id"} ne $set_session->{"id"}) 9 6 15
Condition66%2/3
not exists $child->{"session"} or $child->{"session"}{"id"} ne $set_session->{"id"}
l!l&&r!l&&!r
906
MC/DC50%1/2
not exists $child->{"session"} or $child->{"session"}{"id"} ne $set_session->{"id"}
not exists $child->{"session"} $child->{"session"}{"id"} ne $set_session->{"id"}
Truth table66%2/3
not(A) or B
A= not exists $child->{"session"}B= $child->{"session"}{"id"} ne $set_session->{"id"}
ABresult
1X1
011
000
480 9 $child->{session} = $sessioncache->session_new($set_session);
481 9 $sessioncache->expire_insert($child->{session});
482 }
483 }
484  
485 15 ▶ if ($sessioncache->session_put($child)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sessioncache->session_put($child)) { } 10 5 15
elsif (my $idle_child = $sessioncache->session_get_per_user_idle($child)) { } 0 5 5
486 10 my $cnt = scalar keys %{$sessioncache->{active}};
487 10 ▶ if ($cnt > $sessioncache->{max_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > $sessioncache->{"max_session"}) 0 10 10
488 0 ▶ if ($child = $sessioncache->lru_dequeue()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child = $sessioncache->lru_dequeue) 0 0 0
489 0 $self->{async}->child_finish($child, 'lru');
490 }
491 }
492 10 $child = undef;
493 } elsif (my $idle_child = $sessioncache->session_get_per_user_idle($child)) {
494 # update idle user session with older session_id
495 #
496 0 $self->{async}->child_finish($idle_child, 'update');
497  
498 0 ▶ if ($sessioncache->session_put($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sessioncache->session_put($child)) 0 0 0
499 0 $child = undef;
500 }
501 }
502 }
503 155 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 145 10 155
504 145 $self->{async}->child_finish($child, 'done');
505 }
506 }
507 }
508 169 return $res;
509 }
510  
511 588 ▶sub _worker_sessions_expire {
Subroutine _worker_sessions_expire: called CC=3 SCAR=11.3
512 588 my ($self) = @_;
513 588 ▶ return unless $self->{sessioncache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sessioncache"} 527 61 588
514  
515 # If a job for the expired session is active, the session
516 # will be dropped when session_put() is called after the
517 # job completed.
518 #
519 61 ▶ while (my $child = $self->{sessioncache}->expired_dequeue()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $self->{"sessioncache"}->expired_dequeue) 0 61 61
520 0 $self->{async}->child_finish($child, 'expired');
521 }
522 61 return;
523 }
524  
525 1078 ▶sub rpc_timeout {
Subroutine rpc_timeout: called CC=3 SCAR=11.0
Pod undocumented
526 1078 my ($self, $call_timeout) = @_;
527  
528 1078 ▶ if ($call_timeout && (keys %{$self->{reqs}} > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_timeout and keys %{$$self{"reqs"};} > 0) 19 1059 1078
Condition66%2/3
$call_timeout and keys %{$$self{"reqs"};} > 0
!ll&&!rl&&r
1059019
MC/DC50%1/2
$call_timeout and keys %{$$self{"reqs"};} > 0
$call_timeout keys %{$$self{"reqs"};} > 0
Truth table66%2/3
A and B
A= $call_timeoutB= keys %{$$self{"reqs"};} > 0
ABresult
0X0
100
111
529 19 return $call_timeout; # for individual client call
530 }
531 1059 return $self->{timeout};
532 }
533  
534 1096 ▶sub rpc_stopped {
Subroutine rpc_stopped: called CC=7 SCAR=36.7
Pod undocumented
535 1096 my ($self) = @_;
536  
537 1096 ▶ if ($self->{stop}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stop"}) 0 1096 1096
538 0 ▶ if (($self->{stop} eq 'withdraw') && (keys %{$self->{announced}})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'stop'} eq 'withdraw' and keys %{$$self{'announced'};}) { } 0 0 0
elsif ($self->{'stop'} eq 'withdraw' and $self->{'async'} and keys %{$$self{'async'}{'jobs'};}) { } 0 0 0
Condition0%0/3
$self->{"stop"} eq "withdraw" and keys %{$$self{"announced"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"stop"} eq "withdraw" and keys %{$$self{"announced"};}
$self->{"stop"} eq "withdraw" keys %{$$self{"announced"};}
Truth table0%0/3
A and B
A= $self->{"stop"} eq "withdraw"B= keys %{$$self{"announced"};}
ABresult
0X0
100
111
539 0 return; # wait for withdraw to complete
540 0 ▶ } elsif (($self->{stop} eq 'withdraw') && $self->{async} && (keys %{$self->{async}{jobs}})) {
Condition0%0/6
$self->{"stop"} eq "withdraw" and $self->{"async"} and keys %{$$self{"async"}{"jobs"};}
!ll&&!rl&&r
000
$self->{"stop"} eq "withdraw" and $self->{"async"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"stop"} eq "withdraw" and $self->{"async"} and keys %{$$self{"async"}{"jobs"};}
$self->{"stop"} eq "withdraw" $self->{"async"} keys %{$$self{"async"}{"jobs"};}
Truth table0%0/4
A and B and C
A= $self->{"stop"} eq "withdraw"B= $self->{"async"}C= keys %{$$self{"async"}{"jobs"};}
ABCresult
0XX0
10X0
1100
1111
541 0 return; # wait for active jobs to complete
542 }
543 0 return 1;
544 }
545 }
546  
547 306 ▶sub rpc_handler {
Subroutine rpc_handler: called CC=25 SCAR=33.1
Pod undocumented
548 306 my ($self, $call_timeout, $handler, @handler_params) = @_;
549  
550 # returns response or throws rpc_error.
551 # returns undef when remote side cleanly closed connection with EOF.
552 #
553 306 ▶ while (!$self->rpc_stopped()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->rpc_stopped) 1096 0 1096
554 1096 my @pipes = ();
555 1096 ▶ if ($self->{async}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"async"}) 588 508 1096
556 588 $self->_worker_sessions_expire();
557 588 ▶ $self->_worker_childs_dequeue_and_run() unless $self->{stop};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"stop"} 588 0 588
558 570 $self->{async}->childs_reap(nonblock => 1);
559 570 $self->rpc_worker_flowcontrol(@handler_params);
560 570 @pipes = map { $_->{reader} } values %{$self->{async}{jobs}};
561 }
562 1078 my $timeout = $self->rpc_timeout($call_timeout);
563 # Unread data might be internally buffered in the SSL stack.
564 # To detect such buffering pending() need to be used.
565 #
566 1078 ▶ my $ssl_pending = (ref($self->{sock}) eq 'IO::Socket::SSL') && $self->{sock}->pending();
Condition66%2/3
ref $self->{'sock'} eq 'IO::Socket::SSL' && $self->{'sock'}->pending
!ll&&!rl&&r
107701
MC/DC50%1/2
ref $self->{'sock'} eq 'IO::Socket::SSL' && $self->{'sock'}->pending
ref $self->{'sock'} eq 'IO::Socket::SSL' $self->{'sock'}->pending
Truth table66%2/3
A && B
A= ref $self->{'sock'} eq 'IO::Socket::SSL'B= $self->{'sock'}->pending
ABresult
0X0
100
111
567  
568 1078 ▶ if (($timeout || @pipes) && !$ssl_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout || @pipes and not $ssl_pending) 426 652 1078
Condition100%6/6
$timeout || @pipes and not $ssl_pending
!ll&&!rl&&r
6511426
$timeout or @pipes
l!l&&r!l&&!r
326101651
MC/DC100%3/3
$timeout || @pipes and not $ssl_pending
$timeout @pipes not $ssl_pending
Truth table80%4/5
A or B and not(C)
A= $timeoutB= @pipesC= not $ssl_pending
ABCresult
00X0
1X00
0100
1X11
0111
569 426 my @ready = IO::Select->new(($self->{sock}, @pipes))->can_read($timeout);
570 426 ▶ next if (@ready == 0) && $!{EINTR}; # $! is not reset on success
Branch: 1 branch
BranchTrueFalseTotal
if @ready == 0 and $!{"EINTR"} 0 426 426
Condition66%2/3
@ready == 0 and $!{"EINTR"}
!ll&&!rl&&r
407190
MC/DC0%0/2
@ready == 0 and $!{"EINTR"}
@ready == 0 $!{"EINTR"}
Truth table66%2/3
A and B
A= @ready == 0B= $!{"EINTR"}
ABresult
0X0
100
111
571 426 ▶ die rpc_error('jsonrpc', 'receive timeout') unless (@ready > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @ready > 0 19 407 426
572  
573 407 foreach my $fh (@ready) {
574 465 ▶ if (($fh != $self->{sock}) && $self->{async}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh != $self->{"sock"} and $self->{"async"}) 169 296 465
Condition66%2/3
$fh != $self->{"sock"} and $self->{"async"}
!ll&&!rl&&r
2960169
MC/DC50%1/2
$fh != $self->{"sock"} and $self->{"async"}
$fh != $self->{"sock"} $self->{"async"}
Truth table66%2/3
A and B
A= $fh != $self->{"sock"}B= $self->{"async"}
ABresult
0X0
100
111
575 169 ▶ unless (exists $self->{async}{jobs}{$fh->fileno}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"async"}{"jobs"}{$fh->fileno}) 0 169 169
576 0 die rpc_error('io', "child pipe not found: ". $fh->fileno);
577 }
578 169 my $child = $self->{async}{jobs}{$fh->fileno};
579 169 $self->{async}->job_rem($child);
580 169 my $res = $self->_worker_child_read_and_finish($child);
581 }
582 }
583 407 ▶ next unless grep { $_ == $self->{sock} } @ready;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $self->{"sock"};} @ready 111 296 407
584 }
585  
586 # always block on full messages from rpcswitch
587 948 my $b = eval { netstring_read($self->{sock}) };
588 948 ▶ unless ($b) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($b) 97 851 948
589 97 ▶ next if ($@ && ($@ =~ /^EINTR/)); # check if stopped
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $@ =~ /^EINTR/ 0 97 97
Condition33%1/3
$@ and $@ =~ /^EINTR/
!ll&&!rl&&r
9700
MC/DC0%0/2
$@ and $@ =~ /^EINTR/
$@ $@ =~ /^EINTR/
Truth table33%1/3
A and B
A= $@B= $@ =~ /^EINTR/
ABresult
0X0
100
111
590 97 ▶ die rpc_error('io', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 97 97
591 97 return; # EOF
592 }
593 851 my $msg = eval { from_json($b, {%{$self->{json_utf8}}}) };
594 851 ▶ die rpc_error('jsonrpc', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 19 832 851
595 832 ▶ $self->{trace_cb}->('RCV', $msg) if $self->{trace_cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace_cb"} 93 739 832
596 832 my $res = eval { $handler->($self, $msg, @handler_params) };
597 832 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 38 794 832
598 38 ▶ die $err if ref($err); # forward error
Branch: 1 branch
BranchTrueFalseTotal
if ref $err 38 0 38
599 0 die rpc_error('io', $err);
600 }
601 794 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 115 679 794
602 115 return $res;
603 }
604 }
605 0 return; # STOP is checked by caller
606 }
607  
608 134 ▶sub work {
Subroutine work: called CC=17 SCAR=31.2
Pod documented
609 134 my ($self, $workername, $methods, $opts) = @_;
610  
611 # a write on a shutdown socket should never happen
612 #
613 0 ▶ local $SIG{'PIPE'} = sub { die "work[$$]: got PIPE!\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
614  
615 134 foreach my $method (keys %$methods) {
616 130 $self->{methods}{$method}{cb} = $methods->{$method}{cb};
617 130 ▶ $self->{methods}{$method}{doc} = (defined $methods->{$method}{doc}) ? $methods->{$method}{doc} : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $methods->{$method}{'doc'} ? : 49 81 130
618 130 ▶ $self->{methods}{$method}{filter} = $methods->{$method}{filter} if exists $methods->{$method}{filter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $methods->{$method}{"filter"} 0 130 130
619 }
620 134 ▶ $opts->{trace_cb} = $self->{trace_cb} if exists $self->{trace_cb};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"trace_cb"} 14 120 134
621 134 ▶ $self->{async} = RPC::Switch::Client::Tiny::Async->new(%$opts) if $opts->{max_async};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"max_async"} 66 68 134
622 134 ▶ $self->{flowcontrol} = $opts->{flowcontrol} if $opts->{flowcontrol};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"flowcontrol"} 0 134 134
623 134 ▶ $self->{sessioncache} = RPC::Switch::Client::Tiny::SessionCache->new(%$opts) if $opts->{max_session};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"max_session"} 8 126 134
624 134 $self->rpc_handler(0, \&worker, $workername);
625  
626 97 ▶ if ($self->{stop}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stop"}) 0 97 97
627 0 $self->rpc_worker_withdraw();
628 0 $self->{stop} = 'withdraw';
629  
630 # wait some time for withdraw & active jobs to complete
631 #
632 0 ▶ local $SIG{ALRM} = sub { warn "worker child stop timeout\n"; $self->{stop} = 'timeout'; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
633 0 alarm($self->{gracetime});
634 0 $self->rpc_handler(0, \&worker, $workername);
635 0 alarm(0);
636 0 die rpc_error('io', 'STOP');
637 }
638 97 ▶ if (my $async = $self->{async}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $async = $self->{"async"}) 48 49 97
639 # drop stored sessions
640 #
641 48 ▶ if (my $sessioncache = $self->{sessioncache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sessioncache = $self->{"sessioncache"}) 4 44 48
642 4 foreach my $session_id (keys %{$sessioncache->{active}}) {
643 3 ▶ if (my $child = $sessioncache->session_get($session_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $sessioncache->session_get($session_id)) 3 0 3
644 3 $async->child_finish($child, 'idle');
645 }
646 }
647 }
648 # reap remaining childs
649 #
650 48 ▶ if (keys %{$async->{finished}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$async{"finished"};}) 48 0 48
651 48 ▶ local $SIG{ALRM} = sub { warn "worker child wait timeout\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
652 48 alarm(1); # wait at most for one second
653 48 ▶ unless ($async->childs_reap()) { # blocking
Branch: 1 branch
BranchTrueFalseTotal
unless ($async->childs_reap) 0 48 48
654 0 $async->childs_reap(nonblock => 1); # continue nonblocking after timeout
655 }
656 48 alarm(0);
657 }
658  
659 # EOF is only an error here when there are outstanding requests
660 #
661 0 ▶ my ($stopped, $msgs) = $async->jobs_terminate('stopped', sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
662 48 my @childs = keys %{$async->{finished}};
663  
664 48 $async->childs_kill(); # don't wait here
665  
666 48 $async->{jobqueue} = [];
667 48 $async->{finished} = {};
668  
669 48 ▶ die rpc_error('io', 'eof while jobs active: '.join(' ', @childs)) if (@childs);
Branch: 1 branch
BranchTrueFalseTotal
if @childs 0 48 48
670 48 ▶ die rpc_error('io', 'eof while jobs queued: '.join(' ', @$msgs)) if (@$msgs);
Branch: 1 branch
BranchTrueFalseTotal
if @$msgs 0 48 48
671 }
672 97 return;
673 }
674  
675 171 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
676 171 my ($self, $method, $params, $opts) = @_;
677 171 my $reqauth = $opts->{reqauth};
678 171 my $call_timeout = $opts->{timeout};
679  
680 171 ▶ if ($self->{state} eq 'rpcswitch.hello') { # trigger rpc_send for consecutive requests
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "rpcswitch.hello") 133 38 171
681 133 $self->rpc_send_call($method, $params, $reqauth);
682 }
683 # EOF is an error here (response missing)
684 #
685 171 ▶ my $res = $self->rpc_handler($call_timeout, \&client, $method, $params, $reqauth) or die rpc_error('io', 'eof');
Branch: 1 branch
BranchTrueFalseTotal
my $res = $self->rpc_handler($call_timeout, \&RPC::Switch::Client::Tiny::client, $method, $params, $reqauth) or die rpc_error("io", "eof") 114 0 114
686 114 ▶ return wantarray() ? @$res : $res->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 19 95 114
687 }
688  
689 # stop() exits an active $client->work() worker handler.
690 #
691 # - work() dies with RPC::Switch::Client::Tiny::Error which
692 # might be {type => 'io', message => 'STOP'}, or any other
693 # error if a non-restartable system call was interrupted.
694 #
695 # - stop() makes no sense for call() (it has rpc_timeout)
696 # - the only way to call stop is from a signal handler.
697 # - if a signal handler is called, non-restartavle perl
698 # system call are interrupted and return $! == EINTR.
699 #
700 # -> this can break an active worker handler and result in
701 # a RES_ERROR-message to the caller if a non-restartable
702 # perl syscall is interrupted.
703 # -> for the async worker mode this should mostly work.
704 # (sysreadfull, rpc print & IO::Select are restartable).
705 # -> stop will just wait for a gracetime of 2 seconds
706 # for active jobs to complete. The remaining jobs
707 # are terminated.
708 #
709 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod undocumented
710 0 my ($self, $opts) = @_;
711 0 ▶ $self->{gracetime} = $opts->{gracetime} ? $opts->{gracetime} : 2;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'gracetime'} ? : 0 0 0
712 0 $self->{stop} = 'pending';
713 0 return;
714 }
715  
716 # The perl object destroy order is undefined, so $self->{sock}
717 # might already be destroyed and it makes no sense to try to
718 # send RES_ERROR messages for remaining childs.
719 # see: https://perldoc.perl.org/perlobj#Global-Destruction
720 #
721 # So just terminate remaining childs, and let init-process
722 # collect them instead of calling waitpid() here.
723 #
724 # TODO: perl will call DESTROY only when the process exits
725 # cleanly or calls exit(). If the process is killed, perl
726 # calls DESTROY only if a handler for the matching signal
727 # is installed, like: $SIG{'TERM'} = sub { exit; };
728 #
729 # -> so does it make sense to support DESTROY at all,
730 # if there are situations when it is not called?
731 #
732 197 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
733 197 my ($self) = @_;
734 197 ▶ $self->{async}->childs_kill() if $self->{async}; # don't wait here
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"async"} 47 150 197
735 }
736  
737 21 1;
738  
739 __END__
↑ Summary Tiny/Async.pm »