Manager/Service/Handle.pm

filter: Statementstmt 83.6% 128 / 153 Branchbran 67.4% 58 / 86 Conditioncond 52.6% 10 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 95.6% 22 / 23 Podpod 90.0% 9 / 10 totaltotal 76.1% 230 / 302 CC 57 SCAR 29.6
CC
57
Cov
76%
_gone_pending_peers
90.0
await_all_responses
28.5
_have_gone_pending_peers
16.6
CRAP 19.3
« Manager/Service/State.pm ↑ Summary Manager/Service.pm »
1 package IPC::Manager::Service::Handle;
2 207 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 207 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 207 our $VERSION = '0.000035';
6  
7 207 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 207 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 207 ▶use Test2::Util::UUID qw/gen_uuid/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 207 ▶use IPC::Manager::Util qw/tinysleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 207 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 207 with 'IPC::Manager::Role::Service::Select';
16 207 with 'IPC::Manager::Role::Service::Requests';
17  
18 207 use Object::HashBase qw{
19 <service_name
20 <name
21 <ipcm_info
22  
23 interval
24  
25 <child_pid
26  
27 +client
28  
29 +request_callbacks
30 +requests
31  
32 +buffer
33  
34 +spawn
35 207 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 141 ▶sub _set_child_pid { $_[0]->{+CHILD_PID} = $_[1] }
Subroutine _set_child_pid: called CC=1 SCAR=0.0
38  
39 sub init {
40 164 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=16.1
Pod undocumented
41  
42 164 $self->clear_servicerequests_fields;
43 164 $self->clear_serviceselect_fields;
44  
45 164 ▶ croak "'service_name' is a required attribute" unless $self->{+SERVICE_NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"service_name"} 1 163 164
46 163 ▶ croak "'ipcm_info' is a required attribute" unless $self->{+IPCM_INFO};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ipcm_info"} 1 162 163
47  
48 162 ▶ $self->{+INTERVAL} //= 0.2;
Condition100%2/2
$self->{"interval"} //= "0.2"
l!l
8154
MC/DC100%1/1
$self->{"interval"} //= "0.2"
$self->{"interval"}
Truth table100%2/2
A
A= $self->{"interval"}
Aresult
11
00
49  
50 162 ▶ $self->{+NAME} //= gen_uuid();
Condition66%2/3
$self->{"name"} //= gen_uuid()
l!l&&r!l&&!r
142200
MC/DC0%0/2
$self->{"name"} //= gen_uuid()
$self->{"name"} gen_uuid()
Truth table66%2/3
A //= B
A= $self->{"name"}B= gen_uuid()
ABresult
1X1
011
000
51 }
52  
53 sub DESTROY {
54 164 ▶ my $self = shift;
Subroutine DESTROY: called CC=3 SCAR=11.0
55  
56 # Disconnect the client before the spawn is destroyed, otherwise the
57 # spawn's unspawn() may remove shared resources (e.g. SysV semaphores)
58 # that the client's disconnect/write_stats still needs.
59 164 ▶ if (my $client = delete $self->{+CLIENT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = delete $self->{"client"}) 149 15 164
60 149 local $@;
61 149 ▶ eval { $client->disconnect; 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $client->disconnect; 1 } } or warn $@ 0 149 149
62 }
63 }
64  
65 sub select_handles {
66 140 ▶ my $self = shift;
Subroutine select_handles: called CC=2 SCAR=6.9
Pod documented
67 140 my $client = $self->client;
68 140 ▶ return $client->have_handles_for_select ? $client->handles_for_select : ();
Branch: 1 branch
BranchTrueFalseTotal
$client->have_handles_for_select ? : 70 70 140
69 }
70  
71 sub ready {
72 144 ▶ my $self = shift;
Subroutine ready: called CC=1 SCAR=0.0
Pod documented
73 144 my ($timeout) = @_;
74 144 $self->client->peer_active($self->{+SERVICE_NAME}, $timeout);
75 }
76  
77 sub client {
78 1863 ▶ my $self = shift;
Subroutine client: called CC=2 SCAR=6.9
Pod documented
79 1863 ▶ return $self->{+CLIENT} if $self->{+CLIENT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"client"} 1723 140 1863
80  
81 140 require IPC::Manager;
82 140 return $self->{+CLIENT} = IPC::Manager->connect($self->{+NAME}, $self->{+IPCM_INFO});
83 }
84  
85 sub service_pid {
86 28 ▶ my $self = shift;
Subroutine service_pid: called CC=1 SCAR=0.0
Pod documented
87 28 $self->client->peer_pid($self->service_name);
88 }
89  
90 sub sync_request {
91 66 ▶ my $self = shift;
Subroutine sync_request: called CC=1 SCAR=0.0
Pod documented
92 66 my ($peer, $payload, $timeout) = @_;
93 66 my $id = $self->send_request($peer, $payload);
94 66 return $self->await_response($id, $timeout);
95 }
96  
97 # Returns true if the named peer is still considered live for the purpose
98 # of expecting a response. Protocols that advertise suspend_supported let
99 # peers suspend cleanly (pidfile dropped) or restart under a new pid while
100 # the registration stays in place — a missing pid or a pid mismatch is
101 # therefore not a failure on its own, only full unregistration is.
102 # Protocols without suspend treat a dead or missing pid as a permanent loss.
103 sub _pending_peer_active {
104 417 ▶ my $self = shift;
Subroutine _pending_peer_active: called CC=5 SCAR=16.1
105 417 my ($peer, $pid) = @_;
106  
107 417 my $client = $self->client;
108  
109 417 ▶ return $client->peer_exists($peer) ? 1 : 0 if $client->suspend_supported;
Branch: 2 branches
BranchTrueFalseTotal
if $client->suspend_supported 289 128 417
$client->peer_exists($peer) ? : 258 31 289
110 128 ▶ return $client->pid_is_running($pid) ? 1 : 0 if defined $pid;
Branch: 2 branches
BranchTrueFalseTotal
if defined $pid 127 1 128
$client->pid_is_running($pid) ? : 96 31 127
111 1 return $client->peer_active($peer);
112 }
113  
114 sub await_response {
115 132 ▶ my $self = shift;
Subroutine await_response: called CC=9 SCAR=22.0
Pod documented
116 132 my ($id, $timeout) = @_;
117  
118 132 my $interval = $self->{+INTERVAL};
119 132 ▶ my $deadline = defined($timeout) ? time + $timeout : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 2 130 132
120  
121 132 while (1) {
122 422 my @out = $self->get_response($id);
123 422 ▶ return $out[0] if @out;
Branch: 1 branch
BranchTrueFalseTotal
if @out 64 358 422
124  
125 358 my ($peer, $pid) = $self->pending_response_peer($id);
126 358 ▶ if ($peer && !$self->_pending_peer_active($peer, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer and not $self->_pending_peer_active($peer, $pid)) 63 295 358
Condition66%2/3
$peer and not $self->_pending_peer_active($peer, $pid)
!ll&&!rl&&r
029563
MC/DC50%1/2
$peer and not $self->_pending_peer_active($peer, $pid)
$peer not $self->_pending_peer_active($peer, $pid)
Truth table66%2/3
A and not(B)
A= $peerB= not $self->_pending_peer_active($peer, $pid)
ABresult
0X0
100
111
127 # Race: the peer may have sent its response and then exited
128 # before we polled for it. The peer-active check now reports
129 # gone, but the response can still be sitting in the transport
130 # waiting to be ingested. Do one final non-blocking drain and
131 # re-check before declaring the request lost.
132 63 $self->poll(0);
133 63 @out = $self->get_response($id);
134 63 ▶ return $out[0] if @out;
Branch: 1 branch
BranchTrueFalseTotal
if @out 60 3 63
135  
136 3 croak "peer '$peer' went away while awaiting response '$id'";
137 }
138  
139 295 my $wait = $interval;
140 295 ▶ if (defined $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadline) 18 277 295
141 18 my $remaining = $deadline - time;
142 18 ▶ croak "await_response: timed out after ${timeout}s waiting for response '$id'"
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 2 16 18
143 if $remaining <= 0;
144  
145 16 ▶ $wait = $remaining if $remaining < $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining < $wait 2 14 16
146 }
147  
148 293 $self->poll($wait);
149 }
150 }
151  
152 sub await_all_responses {
153 24 ▶ my $self = shift;
Subroutine await_all_responses: called CC=9 SCAR=33.5
Pod documented
154 24 my ($timeout) = @_;
155  
156 24 ▶ my $deadline = defined($timeout) ? time + $timeout : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 0 24 24
157  
158 24 ▶ while ($self->have_pending_responses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->have_pending_responses) 35 24 59
159 35 ▶ if ($self->_have_gone_pending_peers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_have_gone_pending_peers) 0 35 35
160 # Drain once more in case responses raced peer disappearance.
161 0 $self->poll(0);
162 0 ▶ last unless $self->have_pending_responses;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->have_pending_responses 0 0 0
163  
164 0 my @gone = $self->_gone_pending_peers;
165 0 ▶ if (@gone) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gone) 0 0 0
166 0 my %seen;
167 0 my @uniq = grep { !$seen{$_}++ } map { $_->[0] } @gone;
168 0 croak "peer(s) '" . join(", ", @uniq) . "' went away while awaiting responses";
169 }
170 }
171  
172 35 my $wait = $self->{+INTERVAL};
173 35 ▶ if (defined $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadline) 0 35 35
174 0 my $remaining = $deadline - time;
175 0 ▶ croak "await_all_responses: timed out after ${timeout}s with pending responses"
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 0 0
176 if $remaining <= 0;
177 0 ▶ $wait = $remaining if $remaining < $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining < $wait 0 0 0
178 }
179  
180 35 $self->poll($wait);
181 }
182  
183 24 return;
184 }
185  
186 # Returns a list of [peer, pid] tuples for pending responses whose peer is
187 # no longer considered active.
188 sub _gone_pending_peers {
189 0 ▶ my $self = shift;
Subroutine _gone_pending_peers: not called CC=9 SCAR=45.0
190  
191 0 my @gone;
192 0 ▶ for my $entry (values %{$self->{_RESPONSES} // {}}) {
Condition0%0/2
$self->{"_RESPONSES"} // {}
l!l
00
MC/DC0%0/1
$self->{"_RESPONSES"} // {}
$self->{"_RESPONSES"}
Truth table0%0/2
A
A= $self->{"_RESPONSES"}
Aresult
11
00
193 0 ▶ next if defined $entry->{response};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"response"} 0 0 0
194 push @gone, [$entry->{peer}, $entry->{pid}]
195 0 ▶ unless $self->_pending_peer_active($entry->{peer}, $entry->{pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_pending_peer_active($entry->{"peer"}, $entry->{"pid"}) 0 0 0
196 }
197 0 ▶ for my $entry (values %{$self->{_RESPONSE_HANDLER} // {}}) {
Condition0%0/2
$self->{"_RESPONSE_HANDLER"} // {}
l!l
00
MC/DC0%0/1
$self->{"_RESPONSE_HANDLER"} // {}
$self->{"_RESPONSE_HANDLER"}
Truth table0%0/2
A
A= $self->{"_RESPONSE_HANDLER"}
Aresult
11
00
198 0 ▶ next unless defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 0 0
199 push @gone, [$entry->{peer}, $entry->{pid}]
200 0 ▶ unless $self->_pending_peer_active($entry->{peer}, $entry->{pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_pending_peer_active($entry->{"peer"}, $entry->{"pid"}) 0 0 0
201 }
202 0 return @gone;
203 }
204  
205 # Boolean fast path for "is any pending response's peer gone?". Short-
206 # circuits on the first hit so we skip _pending_peer_active probes (which
207 # can hit the FS / DB) for every other pending response. Use this when
208 # the caller only needs the yes/no; collect names via _gone_pending_peers
209 # only after this confirms there is at least one.
210 sub _have_gone_pending_peers {
211 35 ▶ my $self = shift;
Subroutine _have_gone_pending_peers: called CC=9 SCAR=28.1
212  
213 35 ▶ for my $entry (values %{$self->{_RESPONSES} // {}}) {
Condition50%1/2
$self->{"_RESPONSES"} // {}
l!l
035
MC/DC0%0/1
$self->{"_RESPONSES"} // {}
$self->{"_RESPONSES"}
Truth table50%1/2
A
A= $self->{"_RESPONSES"}
Aresult
11
00
214 0 ▶ next if defined $entry->{response};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"response"} 0 0 0
215 0 ▶ return 1 unless $self->_pending_peer_active($entry->{peer}, $entry->{pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_pending_peer_active($entry->{"peer"}, $entry->{"pid"}) 0 0 0
216 }
217 35 ▶ for my $entry (values %{$self->{_RESPONSE_HANDLER} // {}}) {
Condition50%1/2
$self->{"_RESPONSE_HANDLER"} // {}
l!l
350
MC/DC0%0/1
$self->{"_RESPONSE_HANDLER"} // {}
$self->{"_RESPONSE_HANDLER"}
Truth table50%1/2
A
A= $self->{"_RESPONSE_HANDLER"}
Aresult
11
00
218 59 ▶ next unless defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 59 59
219 59 ▶ return 1 unless $self->_pending_peer_active($entry->{peer}, $entry->{pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_pending_peer_active($entry->{"peer"}, $entry->{"pid"}) 0 59 59
220 }
221 35 return 0;
222 }
223  
224 sub messages {
225 11 ▶ my $self = shift;
Subroutine messages: called CC=2 SCAR=6.9
Pod documented
226 11 my $messages = delete $self->{+BUFFER};
227 11 ▶ return unless $messages;
Branch: 1 branch
BranchTrueFalseTotal
unless $messages 6 5 11
228 5 return @$messages;
229 }
230  
231 sub poll {
232 391 ▶ my $self = shift;
Subroutine poll: called CC=12 SCAR=24.9
Pod documented
233 391 my ($timeout) = @_;
234  
235 391 my $client = $self->client;
236  
237 391 my @messages;
238 391 ▶ if (my $select = $self->select) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $select = $self->select) { } 77 314 391
239 77 ▶ if ($select->can_read($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($timeout)) 77 0 77
240 77 @messages = $client->get_messages;
241 }
242 }
243 else {
244 314 ▶ my $start = $timeout ? time : 0;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 281 33 314
245  
246 314 while (1) {
247 588 @messages = $client->get_messages;
248 588 ▶ last if @messages;
Branch: 1 branch
BranchTrueFalseTotal
if @messages 78 510 588
249  
250 510 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 510 0 510
251 510 ▶ last unless $timeout; # timeout is 0
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout 3 507 510
252  
253 507 my $delta = time - $start;
254 507 ▶ last if $delta >= $timeout
Branch: 1 branch
BranchTrueFalseTotal
if $delta >= $timeout 230 277 507
255 }
256  
257 277 tinysleep($self->{+INTERVAL});
258 }
259 }
260  
261 388 ▶ return unless @messages;
Branch: 1 branch
BranchTrueFalseTotal
unless @messages 233 155 388
262  
263 # handle the messages
264 # Split into regular message to go in the buffer and handle responses
265 155 for my $msg (@messages) {
266 168 my $c = $msg->content;
267  
268 168 ▶ if (ref($c) eq 'HASH' && $c->{ipcm_response_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH' and $c->{'ipcm_response_id'}) { } 164 4 168
Condition66%2/3
ref $c eq "HASH" and $c->{"ipcm_response_id"}
!ll&&!rl&&r
04164
MC/DC50%1/2
ref $c eq "HASH" and $c->{"ipcm_response_id"}
ref $c eq "HASH" $c->{"ipcm_response_id"}
Truth table66%2/3
A and B
A= ref $c eq "HASH"B= $c->{"ipcm_response_id"}
ABresult
0X0
100
111
269 164 $self->handle_response($c, $msg);
270 }
271 else {
272 4 push @{$self->{+BUFFER}} => $msg;
273 }
274 }
275  
276 155 return scalar @messages;
277 }
278  
279 207 1;
280  
281 __END__
« Manager/Service/State.pm ↑ Summary Manager/Service.pm »