lib/MojoX/HTTP/Async.pm

filter: Statementstmt 91.0% 294 / 323 Branchbran 65.6% 109 / 166 Conditioncond 57.7% 71 / 123 MC/DCmcdc 31.9% 23 / 72 Subroutinesub 93.3% 42 / 45 Podpod 100% 7 / 7 totaltotal 74.1% 546 / 736 CC 149 SCAR 29.0
CC
149
Cov
77%
_send_request
57.5
_connect
31.7
_check_for_errors
21.3
CRAP 18.3
↑ Summary
1 package MojoX::HTTP::Async;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 MojoX::HTTP::Async - The simple package to execute multiple parallel requests to the same host
8  
9 =head1 SYNOPSIS
10  
11 use MojoX::HTTP::Async ();
12 use Mojo::Message::Request ();
13 use Mojo::URL ();
14  
15 # creates new instance for async requests
16 # restricts max amount of simultaneously executed requests
17 my $ua = MojoX::HTTP::Async->new('host' => 'my-site.com', 'slots' => 4);
18  
19 # let's fill slots
20 $ua->add( '/page1.html?lang=en');
21 $ua->add( 'http://my-site.com/page2.html');
22 $ua->add( Mojo::URL->new("/page/03.html") );
23 $ua->add( Mojo::Message::Request->new() );
24  
25 # non-blocking requests processing
26 while ( $ua->not_empty() ) {
27 if (my $tx = $ua->next_response) { # returns an instance of Mojo::Transaction::HTTP class
28 print $tx->res->headers->to_string;
29 } else {
30 # do something else
31 }
32 }
33  
34 # blocking requests processing
35 while (my $tx = $ua->wait_for_next_response($timeout)) {
36 # do something here
37 }
38  
39 # how to process connect timeouts
40 if (my $error = $tx->req()->error()) {
41 say $error->{code};
42 say $error->{message};
43 }
44  
45 # how to process request timeouts and other errors sucn as broken pipes, etc
46 if (my $error = $tx->res()->error()) {
47 say $error->{code};
48 say $error->{message};
49 }
50  
51 # makes reconnection if either slot was timeouted or was inactive too long
52 $ua->refresh_connections();
53  
54 # closes everything
55 $ua->close_all();
56  
57 =head1 DESCRIPTION
58  
59 This library allows to make multiple HTTP/HTTPS request to the particular host in non-blocking mode.
60  
61 In comparison with C<HTTP::Async>, this library doesn't make a new connection on each request.
62  
63 And in comparison with C<Mojo::AsyncAwait>, it's more intuitive how to use it, and there is no any Singleton restrictions.
64  
65 The instance of this class can work only with one domain and scheme: either HTTP or HTTPS.
66  
67 =head1 LICENSE
68  
69 This module is distributed under terms of Artistic Perl 5 license.
70  
71 =cut
72  
73 25 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 25 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76 25 ▶use Socket qw/ inet_aton pack_sockaddr_in AF_INET SOCK_STREAM SOL_SOCKET SO_KEEPALIVE SO_OOBINLINE IPPROTO_TCP TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 #use IO::Socket::IP ();
78 25 ▶use IO::Socket::SSL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
79 25 ▶use Fcntl qw/ F_SETFL O_NONBLOCK FD_CLOEXEC O_NOINHERIT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 25 ▶use experimental qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 25 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 25 ▶use List::Util qw/ first /;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 25 ▶use Time::HiRes qw/ time /;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 25 ▶use Mojo::Message::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
85 25 ▶use Mojo::Message::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 25 ▶use Mojo::Transaction::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87 25 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
88 25 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 25 ▶use Errno qw / :POSIX /;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 25 our $VERSION = 0.14;
92  
93 25 ▶use constant {
Subroutine BEGIN: called CC=3 SCAR=11.7
94 25 ▶ IS_WIN => ($^O eq 'MSWin32') ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 25 25
$^O ne 'MSWin32' ? : 25 0 25
95 IS_NOT_WIN => ($^O ne 'MSWin32') ? 1 : 0,
96 25 };
97  
98 =head2 new($class, %opts)
99  
100 The class constructor.
101  
102 =over
103  
104 =item host
105  
106 It's the obligatory option.
107 Sets the name/address of remote host to be requested.
108  
109 =item port
110  
111 By default it's equal to 80.
112 Sets the port number of remote point.
113  
114 =item slots
115  
116 By default it's equal to 5.
117 Sets the maximum amount of slots.
118 These slot will be filled one by one if required.
119  
120 =item ssl
121  
122 By default it's equal to 0 (means HTTP).
123 Sets the scheme of requests: HTTP or HTTPS.
124  
125 =item ssl_opts
126  
127 It's a HashRef with options to control SSL Layer.
128 See C<IO::Socket::SSL> constructor arguments for details.
129  
130 =item connect_timeout
131  
132 By default it's equal to 1.
133 Sets connection timeout in seconds.
134  
135 If it's equal to 0, then there will be no timeout restrictions.
136  
137 =item request_timeout
138  
139 By default it's equal to 1.
140 Sets the time in seconds with granular accuracy as micro seconds.
141 The awaiting time of response will be limited with this value.
142  
143 In case of 0 value there will be no time restrictions.
144  
145 =item sol_socket
146  
147 It's a HashRef with socket options.
148 The possible keys are:
149  
150 B<so_keepalive> - enables TCP KeepAlive on socket.
151 The default value is 1 (means that option is enabled).
152  
153 =item B<sol_tcp>
154  
155 WARNING: These options can be unsupported on some OS platforms.
156  
157 It's a HashRef with socket TCP-options.
158  
159 If some key is absent in HashRef then system settings will be used.
160  
161 The supported keys are shown below:
162  
163 B<tcp_keepidle> - the time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes
164  
165 B<tcp_keepintvl> - the time (in seconds) between individual keepalive probes
166  
167 B<tcp_keepcnt> - the maximum number of keepalive probes TCP should send before dropping the connection.
168  
169 =item inactivity_conn_ts
170  
171 If last response was received C<inactivity_conn_ts> seconds or more ago,
172 then such slots will be destroyed.
173  
174 By default the value is 0 (disabled).
175  
176 =item debug
177  
178 Enables debug mode. The dbug messages will be printed in STDERR.
179 By default the value is 0 (disabled).
180  
181 =back
182  
183 =cut
184  
185 5 ▶sub new ($class, %opts) {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
186 5 ▶ croak("host is mandatory") if (! $opts{'host'});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"host"} 0 5 5
187 my $self = bless({
188 'slots' => 5,
189 'ssl' => 0,
190 'ssl_opts' => undef,
191 5 ▶ 'port' => $opts{'ssl'} ? 443 : 80,
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ssl'} ? : 1 4 5
192 'request_timeout' => 1, # 1 sec
193 'connect_timeout' => 1, # 1 sec
194 'sol_socket' => {
195 'so_keepalive' => 1,
196 },
197 'sol_tcp' => {},
198 'inactivity_conn_ts' => 0,
199 %opts,
200 '_conns' => [],
201 }, $class);
202 5 return $self;
203 }
204  
205 19 ▶sub _connect ($self, $slot, $proto, $peer_addr) {
Subroutine _connect: called CC=22 SCAR=34.6
206  
207 19 ▶ warn("Connecting\n") if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 19 19
208  
209 19 ▶ socket(my $socket, AF_INET, SOCK_STREAM, $proto) || croak("socket error: $!");
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, 2, 1, $proto or croak("socket error: $!") 19 0 19
210 19 ▶ connect($socket, $peer_addr) || croak("connect error: $!"); # in case of O_NONBLOCK it will return with EINPROGRESS
Branch: 1 branch
BranchTrueFalseTotal
connect $socket, $peer_addr or croak("connect error: $!") 19 0 19
211  
212 # When a constant is used in an expression, Perl replaces it with its value at compile time,
213 # and may then optimize the expression further. In particular, any code in an if (CONSTANT)
214 # block will be optimized away if the constant is false.
215 19 ▶ if (&IS_NOT_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (&IS_NOT_WIN) 19 0 19
216 19 ▶ fcntl($socket, F_SETFL, O_NONBLOCK | FD_CLOEXEC) || croak("fcntl error has occurred: $!");
Branch: 1 branch
BranchTrueFalseTotal
fcntl $socket, 4, 2049 or croak("fcntl error has occurred: $!") 19 0 19
217 }
218  
219 19 ▶ if (&IS_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (&IS_WIN) 0 19 19
220 0 $socket = IO::Socket::IP->new_from_fd(fileno($socket), '+<');
221 0 ▶ defined($socket->blocking(0)) or croak("can't set non-blocking state on socket: $!");
Branch: 1 branch
BranchTrueFalseTotal
defined $socket->blocking(0) or croak("can't set non-blocking state on socket: $!") 0 0 0
222 #$socket->sockopt(O_NOINHERIT, 1) or croak("fcntl error has occurred: $!"); # the same as SOCK_CLOEXEC
223 }
224  
225 19 ▶ my $sol_socket_opts = $self->{'sol_socket'} // {};
Condition50%1/2
$self->{"sol_socket"} // {}
l!l
190
MC/DC0%0/1
$self->{"sol_socket"} // {}
$self->{"sol_socket"}
Truth table50%1/2
A
A= $self->{"sol_socket"}
Aresult
11
00
226  
227 19 ▶ if (exists($sol_socket_opts->{'so_keepalive'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sol_socket_opts->{"so_keepalive"}) 17 2 19
228 17 ▶ setsockopt($socket, SOL_SOCKET, SO_KEEPALIVE, 1) || croak("setsockopt error has occurred while setting SO_KEEPALIVE: $!");
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $socket, 1, 9, 1 or croak("setsockopt error has occurred while setting SO_KEEPALIVE: $!") 17 0 17
229  
230 17 ▶ if ($sol_socket_opts->{'so_keepalive'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sol_socket_opts->{"so_keepalive"}) 17 0 17
231 17 ▶ my $sol_tcp_opts = $self->{'sol_tcp'} // {};
Condition50%1/2
$self->{"sol_tcp"} // {}
l!l
170
MC/DC0%0/1
$self->{"sol_tcp"} // {}
$self->{"sol_tcp"}
Truth table50%1/2
A
A= $self->{"sol_tcp"}
Aresult
11
00
232 17 state $SOL_TCP = &IPPROTO_TCP();
233  
234 17 ▶ if (exists($sol_tcp_opts->{'tcp_keepidle'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sol_tcp_opts->{"tcp_keepidle"}) 8 9 17
235 8 ▶ setsockopt($socket, $SOL_TCP, TCP_KEEPIDLE, $sol_tcp_opts->{'tcp_keepidle'}) || croak("setsockopt error has occurred while setting TCP_KEEPIDLE: $!");
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $socket, $SOL_TCP, 4, $sol_tcp_opts->{"tcp_keepidle"} or croak("setsockopt error has occurred while setting TCP_KEEPIDLE: $!") 8 0 8
236 }
237  
238 17 ▶ if (exists($sol_tcp_opts->{'tcp_keepintvl'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sol_tcp_opts->{"tcp_keepintvl"}) 8 9 17
239 8 ▶ setsockopt($socket, $SOL_TCP, TCP_KEEPINTVL, $sol_tcp_opts->{'tcp_keepintvl'}) || croak("setsockopt error has occurred while setting TCP_KEEPINTVL: $!");
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $socket, $SOL_TCP, 5, $sol_tcp_opts->{"tcp_keepintvl"} or croak("setsockopt error has occurred while setting TCP_KEEPINTVL: $!") 8 0 8
240 }
241  
242 17 ▶ if (exists($sol_tcp_opts->{'tcp_keepcnt'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sol_tcp_opts->{"tcp_keepcnt"}) 8 9 17
243 8 ▶ setsockopt($socket, $SOL_TCP, TCP_KEEPCNT, $sol_tcp_opts->{'tcp_keepcnt'}) || croak("setsockopt error has occurred while setting TCP_KEEPCNT: $!");
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $socket, $SOL_TCP, 6, $sol_tcp_opts->{"tcp_keepcnt"} or croak("setsockopt error has occurred while setting TCP_KEEPCNT: $!") 8 0 8
244 }
245 }
246 }
247  
248 19 $slot->{'connected_ts'} = time();
249 19 $slot->{'reader'} = $slot->{'writer'} = $slot->{'socket'} = $socket;
250 19 $slot->{'sock_no'} = fileno($socket);
251 19 ▶ if ($self->{'ssl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssl"}) 2 17 19
252 2 ▶ my $ssl_socket = IO::Socket::SSL->new_from_fd($socket, ($self->{'ssl_opts'} // {})->%*);
Condition50%1/2
$self->{"ssl_opts"} // {}
l!l
20
MC/DC0%0/1
$self->{"ssl_opts"} // {}
$self->{"ssl_opts"}
Truth table50%1/2
A
A= $self->{"ssl_opts"}
Aresult
11
00
253 2 ▶ croak("error=$!, ssl_error=" . $IO::Socket::SSL::SSL_ERROR) if (!$ssl_socket);
Branch: 1 branch
BranchTrueFalseTotal
unless $ssl_socket 0 2 2
254 2 $ssl_socket->blocking(0); # just to be sure
255 2 $slot->{'reader'} = $slot->{'writer'} = $ssl_socket;
256 }
257 }
258  
259 19 ▶sub _connect_slot ($self, $slot) {
Subroutine _connect_slot: called CC=4 SCAR=16.3
260 19 my $timeout = $self->{'connect_timeout'};
261  
262 19 ▶ if ($timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout > 0) { } 19 0 19
263 19 eval {
264 0 ▶ local $SIG{'ALRM'} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
265 19 alarm($timeout);
266 19 $self->_connect($slot, @{$self}{qw/ proto peer_addr /});
267 19 alarm(0);
268 };
269  
270 19 my $error = $@;
271  
272 19 alarm(0);
273  
274 19 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 19 19
275 0 ▶ croak($error) if ($error ne "alarm\n");
Branch: 1 branch
BranchTrueFalseTotal
if $error ne "alarm\n" 0 0 0
276 0 $self->_mark_request_as_timeouted($slot, 'Connect timeout');
277 }
278 } else {
279 0 $self->_connect($slot, @{$self}{qw/ proto peer_addr /});
280 }
281 }
282  
283 14 ▶sub _make_connections ($self, $amount) {
Subroutine _make_connections: called CC=5 SCAR=16.8
284  
285 14 my $host_addr = inet_aton($self->{'host'});
286 14 ▶ croak("can't call inet_aton") if (! $host_addr);
Branch: 1 branch
BranchTrueFalseTotal
unless $host_addr 0 14 14
287  
288 14 ▶ $self->{'peer_addr'} //= pack_sockaddr_in($self->{'port'}, $host_addr);
Condition66%2/3
$self->{"peer_addr"} //= pack_sockaddr_in($self->{"port"}, $host_addr)
l!l&&r!l&&!r
950
MC/DC0%0/2
$self->{"peer_addr"} //= pack_sockaddr_in($self->{"port"}, $host_addr)
$self->{"peer_addr"} pack_sockaddr_in($self->{"port"}, $host_addr)
Truth table66%2/3
A //= B
A= $self->{"peer_addr"}B= pack_sockaddr_in($self->{"port"}, $host_addr)
ABresult
1X1
011
000
289 14 ▶ $self->{'proto'} //= getprotobyname("tcp");
Condition33%1/3
$self->{"proto"} //= getprotobyname "tcp"
l!l&&r!l&&!r
0014
MC/DC0%0/2
$self->{"proto"} //= getprotobyname "tcp"
$self->{"proto"} getprotobyname "tcp"
Truth table33%1/3
A //= B
A= $self->{"proto"}B= getprotobyname "tcp"
ABresult
1X1
011
000
290  
291 14 for (1 .. $amount) {
292 14 my $slot = $self->_make_slot();
293 14 $self->_connect_slot($slot);
294 14 $self->_add_slot($slot);
295 }
296 }
297  
298 14 ▶sub _add_slot ($self, $slot) {
Subroutine _add_slot: called CC=2 SCAR=7.6
299 14 ▶ push($self->{'_conns'}->@*, $slot) if ($slot);
Branch: 1 branch
BranchTrueFalseTotal
if $slot 14 0 14
300 }
301  
302 14 ▶sub _make_slot ($self) {
Subroutine _make_slot: called CC=1 SCAR=0.0
303 return {
304 14 'reader' => undef,
305 'writer' => undef,
306 'socket' => undef,
307 'sock_no' => 0,
308 'is_busy' => 0,
309 'request' => undef,
310 'tx' => undef,
311 'exp_ts' => 0,
312 'tmp_response' => undef,
313 'reconnect_is_required' => 0,
314 'last_response_ts' => 0,
315 'connected_ts' => 0,
316 };
317 }
318  
319 72726 ▶sub _check_for_errors ($self, $socks2slots = {}, $error_handles = '', $reason = '') {
Subroutine _check_for_errors: called CC=11 SCAR=30.6
320  
321 72726 my $message = $reason;
322  
323 72726 ▶ if (!$message) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 72726 0 72726
324 72726 ▶ $message = ($!{'EPIPE'} || $!{'ECONNRESET'} || $!{'ECONNREFUSED'} || $!{'ECONNABORTED'}) ? 'Premature connection close' : 'Unknown error';
Branch: 1 branch
BranchTrueFalseTotal
$!{'EPIPE'} || $!{'ECONNRESET'} || $!{'ECONNREFUSED'} || $!{'ECONNABORTED'} ? : 0 72726 72726
Condition33%3/9
$!{"EPIPE"} or $!{"ECONNRESET"} or $!{"ECONNREFUSED"} or $!{"ECONNABORTED"}
l!l&&r!l&&!r
0072726
$!{"EPIPE"} or $!{"ECONNRESET"} or $!{"ECONNREFUSED"}
l!l&&r!l&&!r
0072726
$!{"EPIPE"} or $!{"ECONNRESET"}
l!l&&r!l&&!r
0072726
MC/DC0%0/4
$!{"EPIPE"} or $!{"ECONNRESET"} or $!{"ECONNREFUSED"} or $!{"ECONNABORTED"}
$!{"EPIPE"} $!{"ECONNRESET"} $!{"ECONNREFUSED"} $!{"ECONNABORTED"}
Truth table20%1/5
A or B or C or D
A= $!{"EPIPE"}B= $!{"ECONNRESET"}C= $!{"ECONNREFUSED"}D= $!{"ECONNABORTED"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
325 }
326  
327 72726 for my $slot_no (keys %$socks2slots) {
328 102214 ▶ if ( vec($error_handles, $slot_no, 1) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec($error_handles, $slot_no, 1) != 0) 0 102214 102214
329 0 my $slot = $socks2slots->{ $slot_no };
330 0 $self->_mark_response_as_broken($slot, 520, $message);
331 }
332 }
333 }
334  
335 35 ▶sub _get_free_slot ($self) {
Subroutine _get_free_slot: called CC=6 SCAR=17.9
336  
337 35 my $slot;
338 22 my %socks2slots = map { $_->{'sock_no'} => $_ }
339 41 ▶ grep { !$_->{'is_busy'} && $_->{'socket'} && !$_->{'reconnect_is_required'} }
Condition83%5/6
!$_->{'is_busy'} && $_->{'socket'} && !$_->{'reconnect_is_required'}
!ll&&!rl&&r
17222
not $_->{"is_busy"} and $_->{"socket"}
!ll&&!rl&&r
17024
MC/DC66%2/3
!$_->{'is_busy'} && $_->{'socket'} && !$_->{'reconnect_is_required'}
not $_->{"is_busy"} $_->{"socket"} !$_->{'reconnect_is_required'}
Truth table75%3/4
not(A) and B && not(C)
A= not $_->{"is_busy"}B= $_->{"socket"}C= !$_->{'reconnect_is_required'}
ABCresult
0XX0
10X0
1100
1111
340 35 $self->{'_conns'}->@*;
341  
342 35 ▶ if (%socks2slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (%socks2slots) 19 16 35
343  
344 19 local $!;
345 19 my $write_handles = '';
346  
347 19 vec($write_handles, $_, 1) = 1 for keys %socks2slots;
348  
349 19 my $error_handles = $write_handles;
350 19 my ($nfound, $timeleft) = select(undef, $write_handles, $error_handles, 0);
351  
352 19 $self->_check_for_errors(\%socks2slots, $error_handles, $!);
353  
354 19 ▶ if ($nfound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound) 19 0 19
355 19 ▶ my $slot_no = first { vec($write_handles, $_, 1) == 1 } keys %socks2slots;
Subroutine __ANON__: called CC=1 SCAR=0.0
356 19 $slot = $socks2slots{ $slot_no };
357 }
358 }
359  
360 35 return $slot;
361 }
362  
363 =head2 add ($self, $request_or_uri, $timeout = undef)
364  
365 Adds HTTP request into empty slot.
366  
367 If the request was successfully added, then it will return 1.
368 Otherwise it will return 0.
369  
370 The request can be not added into slot only in case, if there are no empty slots and new slot wasn't created due to
371 the limit of slot's amount had been reached (see C<new> and C<slots>.
372  
373 It's recommendable always to check result code of this method.
374  
375 Example:
376  
377 my $ua = MojoX::HTTP::Async->new('host' => 'my-host.com', 'slots' => 1);
378  
379 # let's occupy the only slot
380 $ua->add('/page1.html');
381  
382 # let's wait until it's become free again
383 while ( ! $ua->add('/page2.html') ) {
384 while (my $tx = $ua->wait_for_next_response() ) {
385 # do something here
386 }
387 }
388  
389 =over
390  
391 =item $request_or_uri
392  
393 It can be either an instance of C<Mojo::Message::Request> class, or an instance of C<Mojo::URL>.
394 It also can be a simple URI srtring.
395  
396 If the resourse contains the host, then it must be the same as in the constructor C<new>.
397  
398 Using of string with URI or an instance of C<Mojo::URL> class assumes that GET HTTP method will be used.
399  
400 =item $timeout
401  
402 Time in seconds. Can be fractional with microseconds tolerance.
403  
404 The C<request_timeout> from conmtrucor will be used by default.
405  
406 =back
407  
408 =cut
409  
410 22 ▶sub add ($self, $request_or_uri, $timeout = undef) {
Subroutine add: called CC=9 SCAR=22.0
Pod documented
411 22 my $status = 0;
412 22 my $slot = $self->_get_free_slot();
413  
414 22 ▶ if ( ! $slot && $self->{'slots'} > scalar($self->{'_conns'}->@*) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $slot and $self->{"slots"} > scalar @{$self->{"_conns"}}) 13 9 22
Condition100%3/3
not $slot and $self->{"slots"} > scalar @{$self->{"_conns"}}
!ll&&!rl&&r
6313
MC/DC100%2/2
not $slot and $self->{"slots"} > scalar @{$self->{"_conns"}}
not $slot $self->{"slots"} > scalar @{$self->{"_conns"}}
Truth table100%3/3
not(A) and B
A= not $slotB= $self->{"slots"} > scalar @{$self->{"_conns"}}
ABresult
0X0
100
111
415 13 $self->_make_connections(1);
416 13 $slot = $self->_get_free_slot();
417 }
418  
419 22 ▶ if ($slot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot) 19 3 22
420 19 my $request = $request_or_uri;
421 19 ▶ if ( !ref($request_or_uri) || ( blessed($request_or_uri) && $request_or_uri->isa('Mojo::URL') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $request_or_uri or builtin::blessed($request_or_uri) and $request_or_uri->isa("Mojo::URL")) 18 1 19
Condition83%5/6
not ref $request_or_uri or builtin::blessed($request_or_uri) and $request_or_uri->isa("Mojo::URL")
l!l&&r!l&&!r
1711
builtin::blessed($request_or_uri) and $request_or_uri->isa("Mojo::URL")
!ll&&!rl&&r
011
MC/DC66%2/3
not ref $request_or_uri or builtin::blessed($request_or_uri) and $request_or_uri->isa("Mojo::URL")
not ref $request_or_uri builtin::blessed($request_or_uri) $request_or_uri->isa("Mojo::URL")
Truth table75%3/4
not(A) or B and C
A= not ref $request_or_uriB= builtin::blessed($request_or_uri)C= $request_or_uri->isa("Mojo::URL")
ABCresult
1XX1
0111
00X0
0100
422 18 $request = Mojo::Message::Request->new();
423 18 $request->url()->parse($request_or_uri);
424 }
425 19 ▶ if ($request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request) 19 0 19
426 19 $self->_send_request($slot, $request, $timeout);
427 19 $status = 1;
428 }
429 }
430  
431 22 return $status;
432 }
433  
434 26 ▶sub _clear_slot ($self, $slot, $force = 0) {
Subroutine _clear_slot: called CC=4 SCAR=13.9
435 26 $slot->{'is_busy'} = 0;
436 26 $slot->{'exp_ts'} = 0;
437 26 $slot->{'tx'} = undef;
438 26 $slot->{'request'} = undef;
439 26 $slot->{'tmp_response'} = undef;
440 26 ▶ if ($force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force) 14 12 26
441 14 ▶ close($slot->{'socket'}) if $slot->{'socket'};
Branch: 1 branch
BranchTrueFalseTotal
if $slot->{"socket"} 14 0 14
442 14 $slot->{'socket'} = undef;
443 14 $slot->{'reader'} = undef;
444 14 $slot->{'writer'} = undef;
445 14 $slot->{'sock_no'} = 0;
446 14 $slot->{'reconnect_is_required'} = 0;
447 14 $slot->{'last_response_ts'} = 0;
448 14 $slot->{'connected_ts'} = 0;
449 }
450 }
451  
452 6 ▶sub _mark_slot_as_broken($self, $slot) {
Subroutine _mark_slot_as_broken: called CC=3 SCAR=12.7
453 6 $slot->{'reconnect_is_required'} = 1;
454 6 $slot->{'is_busy'} = 1;
455 6 ▶ $slot->{'request'} //= Mojo::Message::Request->new();
Condition33%1/3
$slot->{"request"} //= "Mojo::Message::Request"->new
l!l&&r!l&&!r
600
MC/DC0%0/2
$slot->{"request"} //= "Mojo::Message::Request"->new
$slot->{"request"} "Mojo::Message::Request"->new
Truth table33%1/3
A //= B
A= $slot->{"request"}B= "Mojo::Message::Request"->new
ABresult
1X1
011
000
456 $slot->{'tx'} //= Mojo::Transaction::HTTP->new(
457 6 ▶ 'req' => $slot->{'request'},
Condition33%1/3
$slot->{"tx"} //= "Mojo::Transaction::HTTP"->new("req", $slot->{"request"}, "res", "Mojo::Message::Response"->new)
l!l&&r!l&&!r
060
MC/DC0%0/2
$slot->{"tx"} //= "Mojo::Transaction::HTTP"->new("req", $slot->{"request"}, "res", "Mojo::Message::Response"->new)
$slot->{"tx"} "Mojo::Transaction::HTTP"->new("req", $slot->{"request"}, "res", "Mojo::Message::Response"->new)
Truth table33%1/3
A //= B
A= $slot->{"tx"}B= "Mojo::Transaction::HTTP"->new("req", $slot->{"request"}, "res", "Mojo::Message::Response"->new)
ABresult
1X1
011
000
458 'res' => Mojo::Message::Response->new()
459 );
460 }
461  
462 0 ▶sub _mark_request_as_broken ($self, $slot, $code = 520, $msg = 'Unknown Error') {
Subroutine _mark_request_as_broken: not called CC=3 SCAR=24.8
463 0 $self->_mark_slot_as_broken($slot);li
464 0 $slot->{'request'}->error({'message' => $msg, 'code' => $code});
465 }
466  
467 6 ▶sub _mark_response_as_broken ($self, $slot, $code = 520, $msg = 'Unknown Error') {
Subroutine _mark_response_as_broken: called CC=3 SCAR=11.0
468 6 $self->_mark_slot_as_broken($slot);
469  
470 6 my $res = $slot->{'tx'}->res();
471 6 $res->error({'message' => $msg, 'code' => $code});
472 6 $res->headers()->content_length(0);
473 6 $res->code($code);
474 6 $res->message($msg);
475 }
476  
477 0 ▶sub _mark_request_as_timeouted ($self, $slot, $message = 'Request timeout') {
Subroutine _mark_request_as_timeouted: not called CC=2 SCAR=17.9
478 0 $self->_mark_request_as_broken($slot, 524, $message);
479 }
480  
481 5 ▶sub _mark_response_as_timeouted ($self, $slot, $message = 'Request timeout') {
Subroutine _mark_response_as_timeouted: called CC=2 SCAR=6.9
482 5 $self->_mark_response_as_broken($slot, 524, $message);
483 }
484  
485 19 ▶sub _send_request ($self, $slot, $request, $timeout = undef) {
Subroutine _send_request: called CC=26 SCAR=40.5
486  
487 19 ▶ croak("slot is busy") if ($slot->{'is_busy'});
Branch: 1 branch
BranchTrueFalseTotal
if $slot->{"is_busy"} 0 19 19
488 19 ▶ croak("request object is obligatory") if (!$request);
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 19 19
489 19 ▶ croak('request must be a descendant of Mojo::Message::Request package') if (!$request->isa('Mojo::Message::Request'));
Branch: 1 branch
BranchTrueFalseTotal
unless $request->isa("Mojo::Message::Request") 0 19 19
490  
491 19 ▶ my $required_scheme = $self->{'ssl'} ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ssl'} ? : 2 17 19
492 19 my $url = $request->url();
493 19 my $uri = URI->new( $url );
494 19 my $scheme = $url->scheme();
495  
496 19 ▶ if ($scheme && $required_scheme ne $scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme and $required_scheme ne $scheme) 0 19 19
Condition33%1/3
$scheme and $required_scheme ne $scheme
!ll&&!rl&&r
1900
MC/DC0%0/2
$scheme and $required_scheme ne $scheme
$scheme $required_scheme ne $scheme
Truth table33%1/3
A and B
A= $schemeB= $required_scheme ne $scheme
ABresult
0X0
100
111
497 0 croak(sprintf("Wrong scheme in URI '%s'. It must correspond to the 'ssl' option", $uri->as_string()));
498 }
499  
500 19 ▶ if (! $uri->scheme()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme) 19 0 19
501 # URI::_generic doesn't have C<host> method, that's why we set the scheme by ourseleves to change C<$uri> type
502 19 $uri->scheme($required_scheme);
503 }
504  
505 19 ▶ if (my $host = $uri->host()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $uri->host) 0 19 19
506 0 ▶ if ($host ne $self->{'host'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host ne $self->{"host"}) 0 0 0
507 0 croak(sprintf("Wrong host in URI '%s'. It must be the same as it was specified in constructor: %s", $uri->as_string(), $self->{'host'}));
508 }
509 }
510  
511 19 ▶ $timeout //= $self->{'request_timeout'};
Condition33%1/3
$timeout //= $self->{"request_timeout"}
l!l&&r!l&&!r
0190
MC/DC0%0/2
$timeout //= $self->{"request_timeout"}
$timeout $self->{"request_timeout"}
Truth table33%1/3
A //= B
A= $timeoutB= $self->{"request_timeout"}
ABresult
1X1
011
000
512  
513 19 my $response = '';
514 19 state $default_ua_hdr = 'perl/' . __PACKAGE__;
515  
516 19 my $h = $request->headers();
517 19 ▶ $h->host($self->{'host'}) if (! $h->host() );
Branch: 1 branch
BranchTrueFalseTotal
unless $h->host 18 1 19
518 19 ▶ $h->user_agent($default_ua_hdr) if (! $h->user_agent() );
Branch: 1 branch
BranchTrueFalseTotal
unless $h->user_agent 18 1 19
519  
520 19 $slot->{'request'} = $request;
521 19 $slot->{'is_busy'} = 1;
522 19 ▶ $slot->{'exp_ts'} = ($timeout > 0) ? ( time() + $timeout ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$timeout > 0 ? : 19 0 19
523  
524 19 my $plain_request = $request->to_string();
525  
526 19 ▶ if ($self->{'ssl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl'}) { } 2 17 19
527 2 $slot->{'writer'}->print($plain_request);
528 } else {
529 17 my $socket = $slot->{'socket'};
530 17 my $msg_len = bytes::length($plain_request);
531 17 my $sent_bytes = 0;
532 17 my $attempts = 10;
533  
534 17 local $!;
535  
536 17 ▶ while ($sent_bytes < $msg_len && $attempts--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent_bytes < $msg_len and $attempts--) 17 17 34
Condition66%2/3
$sent_bytes < $msg_len and $attempts--
!ll&&!rl&&r
17017
MC/DC50%1/2
$sent_bytes < $msg_len and $attempts--
$sent_bytes < $msg_len $attempts--
Truth table66%2/3
A and B
A= $sent_bytes < $msg_lenB= $attempts--
ABresult
0X0
100
111
537 17 my $bytes = syswrite($socket, $plain_request, $msg_len, $sent_bytes);
538  
539 17 ▶ if ($! || ! defined($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! or not defined $bytes) 0 17 17
Condition33%1/3
$! or not defined $bytes
l!l&&r!l&&!r
0017
MC/DC0%0/2
$! or not defined $bytes
$! not defined $bytes
Truth table33%1/3
A or not(B)
A= $!B= not defined $bytes
ABresult
1X1
011
000
540 0 ▶ my $error = $! // 'Unknown error';
Condition0%0/2
$! // "Unknown error"
l!l
00
MC/DC0%0/1
$! // "Unknown error"
$!
Truth table0%0/2
A
A= $!
Aresult
11
00
541 0 $self->_mark_request_as_broken($slot, 520, $error);
542 0 return;
543 }
544  
545 17 $sent_bytes += $bytes;
546 17 ▶ $plain_request = substr($plain_request, $bytes) if $sent_bytes < $msg_len;
Branch: 1 branch
BranchTrueFalseTotal
if $sent_bytes < $msg_len 0 17 17
547 }
548  
549 17 ▶ if ($sent_bytes < $msg_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent_bytes < $msg_len) 0 17 17
550 0 ▶ my $error = $! // 'sent message is shorter than original';
Condition0%0/2
$! // "sent message is shorter than original"
l!l
00
MC/DC0%0/1
$! // "sent message is shorter than original"
$!
Truth table0%0/2
A
A= $!
Aresult
11
00
551 0 $self->_mark_request_as_broken($slot, 520, $error);
552 0 return;
553 }
554 }
555  
556 19 ▶ if ($slot->{'exp_ts'} && time() > $slot->{'exp_ts'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}) 0 19 19
Condition33%1/3
$slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
!ll&&!rl&&r
0190
MC/DC0%0/2
$slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
$slot->{"exp_ts"} &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
Truth table33%1/3
A and B
A= $slot->{"exp_ts"}B= &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
ABresult
0X0
100
111
557 0 $self->_mark_request_as_timeouted($slot);
558 }
559  
560 19 return;
561 }
562  
563 11 ▶sub _try_to_read ($self, $slot) {
Subroutine _try_to_read: called CC=15 SCAR=30.5
564  
565 11 ▶ return if $slot->{'tx'} || ! $slot->{'is_busy'};
Branch: 1 branch
BranchTrueFalseTotal
if $slot->{"tx"} or not $slot->{"is_busy"} 0 11 11
Condition33%1/3
$slot->{"tx"} or not $slot->{"is_busy"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$slot->{"tx"} or not $slot->{"is_busy"}
$slot->{"tx"} not $slot->{"is_busy"}
Truth table33%1/3
A or not(B)
A= $slot->{"tx"}B= not $slot->{"is_busy"}
ABresult
1X1
011
000
566  
567 11 my $reader = $slot->{'reader'};
568 11 ▶ my $response = $slot->{'tmp_response'} // Mojo::Message::Response->new();
Condition33%1/3
$slot->{"tmp_response"} // "Mojo::Message::Response"->new
l!l&&r!l&&!r
0110
MC/DC0%0/2
$slot->{"tmp_response"} // "Mojo::Message::Response"->new
$slot->{"tmp_response"} "Mojo::Message::Response"->new
Truth table33%1/3
A // B
A= $slot->{"tmp_response"}B= "Mojo::Message::Response"->new
ABresult
1X1
011
000
569  
570 11 ▶ $response->parse($_) while (<$reader>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $reader) and do { $response->parse($_) } 23 11 34
571  
572 11 ▶ if ($! && !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) { # not a "Resourse temporary unavailable" (no data)
Branch: 2 branches
BranchTrueFalseTotal
if ($! and not $!{'EAGAIN'} and not $!{'EWOULDBLOCK'}) { } 1 10 11
elsif ($response and $response->code) { } 6 4 10
Condition83%5/6
$! and not $!{"EAGAIN"} and not $!{"EWOULDBLOCK"}
!ll&&!rl&&r
1001
$! and not $!{"EAGAIN"}
!ll&&!rl&&r
191
MC/DC66%2/3
$! and not $!{"EAGAIN"} and not $!{"EWOULDBLOCK"}
$! not $!{"EAGAIN"} not $!{"EWOULDBLOCK"}
Truth table75%3/4
A and not(B) and not(C)
A= $!B= not $!{"EAGAIN"}C= not $!{"EWOULDBLOCK"}
ABCresult
0XX0
10X0
1100
1111
573 1 $self->_mark_response_as_broken($slot, 520, $!);
574 ▶ } elsif ($response && $response->code()) {
Condition66%2/3
$response and $response->code
!ll&&!rl&&r
046
MC/DC50%1/2
$response and $response->code
$response $response->code
Truth table66%2/3
A and B
A= $responseB= $response->code
ABresult
0X0
100
111
575  
576 6 my $content = $response->content();
577  
578 6 ▶ if ($content->is_finished()) { # this is required to support "Transfer-Encoding: chunked"
Branch: 1 branch
BranchTrueFalseTotal
if ($content->is_finished) { } 6 0 6
579 $slot->{'tx'} = Mojo::Transaction::HTTP->new(
580 6 'req' => $slot->{'request'},
581 'res' => $response
582 );
583 6 $slot->{'tmp_response'} = undef;
584 6 $slot->{'last_response_ts'} = time();
585 } else {
586 0 $slot->{'tmp_response'} = $response;
587 }
588  
589 6 ▶ $slot->{'reconnect_is_required'} = 1 if ($content->relaxed()); # responses that are terminated with a connection close
Branch: 1 branch
BranchTrueFalseTotal
if $content->relaxed 0 6 6
590 }
591  
592 11 ▶ if (! $slot->{'tx'} && ($slot->{'exp_ts'} && time() > $slot->{'exp_ts'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})) 0 11 11
Condition50%3/6
not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})
!ll&&!rl&&r
740
$slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
!ll&&!rl&&r
040
MC/DC0%0/3
not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})
not $slot->{"tx"} $slot->{"exp_ts"} &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
Truth table50%2/4
not(A) and B and C
A= not $slot->{"tx"}B= $slot->{"exp_ts"}C= &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
ABCresult
0XX0
10X0
1100
1111
593 0 $self->_mark_response_as_timeouted($slot);
594 }
595 }
596  
597 =head2 not_empty($self)
598  
599 Returns 1 if there even one slot is busy or slot contains a not processed response.
600 Otherwise the method returns 0.
601  
602 =cut
603  
604 72703 ▶sub not_empty ($self) {
Subroutine not_empty: called CC=5 SCAR=16.1
Pod documented
605  
606 72703 my $not_empty = scalar $self->{'_conns'}->@*;
607  
608 72703 for my $slot ($self->{'_conns'}->@*) {
609 128410 ▶ $not_empty-- if !$slot->{'is_busy'} && !$slot->{'tx'};
Branch: 1 branch
BranchTrueFalseTotal
unless $slot->{"is_busy"} or $slot->{"tx"} 26236 102174 128410
Condition66%2/3
$slot->{"is_busy"} or $slot->{"tx"}
l!l&&r!l&&!r
102174026236
MC/DC50%1/2
$slot->{"is_busy"} or $slot->{"tx"}
$slot->{"is_busy"} $slot->{"tx"}
Truth table66%2/3
A or B
A= $slot->{"is_busy"}B= $slot->{"tx"}
ABresult
1X1
011
000
610 }
611  
612 72703 ▶ return $not_empty ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$not_empty ? : 72695 8 72703
613 }
614  
615  
616 =head2 wait_for_next_response($self, $timeout = 0)
617  
618 Waits for first received response or time-outed request in any slot.
619 Returns the C<Mojo::Transaction::HTTP> instance with result.
620  
621 =over
622  
623 =item $timeout
624  
625 Period of time in seconds. Can be fractial with microsecond tollerance.
626 The response will be marked as time-outed after this time is out.
627  
628 The default value is 0, which means that request will have been blocked until the response is received.
629  
630 If all slots are empty, then C<undef> will be returned.
631  
632 =back
633  
634 =cut
635  
636 12 ▶sub wait_for_next_response ($self, $timeout = 0) {
Subroutine wait_for_next_response: called CC=8 SCAR=21.7
Pod documented
637  
638 12 my $response;
639 12 ▶ my $exp_ts = $timeout ? (time() + $timeout) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 12 0 12
640  
641 12 while (1) {
642 61561 ▶ last if ($exp_ts && time() >= $exp_ts); # awaiting process is time-outed
Branch: 1 branch
BranchTrueFalseTotal
if $exp_ts and &MojoX::HTTP::Async::time() >= $exp_ts 0 61561 61561
Condition33%1/3
$exp_ts and &MojoX::HTTP::Async::time() >= $exp_ts
!ll&&!rl&&r
0615610
MC/DC0%0/2
$exp_ts and &MojoX::HTTP::Async::time() >= $exp_ts
$exp_ts &MojoX::HTTP::Async::time() >= $exp_ts
Truth table33%1/3
A and B
A= $exp_tsB= &MojoX::HTTP::Async::time() >= $exp_ts
ABresult
0X0
100
111
643 61561 ▶ last if (($response = $self->next_response()) || !$self->not_empty());
Branch: 1 branch
BranchTrueFalseTotal
if $response = $self->next_response or not $self->not_empty 12 61549 61561
Condition100%3/3
$response = $self->next_response or not $self->not_empty
l!l&&r!l&&!r
7561549
MC/DC100%2/2
$response = $self->next_response or not $self->not_empty
$response = $self->next_response not $self->not_empty
Truth table100%3/3
A or not(B)
A= $response = $self->next_responseB= not $self->not_empty
ABresult
1X1
011
000
644 61549 ▶ select(undef, undef, undef, 1E-6) if (!$response); # sleep 1 microsecond
Branch: 1 branch
BranchTrueFalseTotal
unless $response 61549 0 61549
645 }
646  
647 12 return $response;
648 }
649  
650 =head2 next_response ($self)
651  
652 Returns an instance of C<Mojo::Transaction::HTTP> class.
653 If there is no response, it will return C<undef>.
654  
655 =cut
656  
657 72707 ▶sub next_response ($self) {
Subroutine next_response: called CC=2 SCAR=7.2
Pod documented
658 72707 ▶ return $self->_get_response_from_ready_slot() // $self->_get_response_from_slot();
Condition66%2/3
$self->_get_response_from_ready_slot // $self->_get_response_from_slot
l!l&&r!l&&!r
12072695
MC/DC50%1/2
$self->_get_response_from_ready_slot // $self->_get_response_from_slot
$self->_get_response_from_ready_slot $self->_get_response_from_slot
Truth table66%2/3
A // B
A= $self->_get_response_from_ready_slotB= $self->_get_response_from_slot
ABresult
1X1
011
000
659 }
660  
661 72695 ▶sub _get_response_from_slot ($self) {
Subroutine _get_response_from_slot: called CC=2 SCAR=7.6
662  
663 72695 my $tx;
664 72695 ▶ my $slot = first { $_->{'tx'} } $self->{'_conns'}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
665  
666 72695 ▶ if ($slot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot) 0 72695 72695
667 0 $tx = $slot->{'tx'};
668 0 $self->_clear_slot($slot, $slot->{'reconnect_is_required'});
669 }
670  
671 72695 return $tx;
672 }
673  
674 72707 ▶sub _get_response_from_ready_slot ($self) {
Subroutine _get_response_from_ready_slot: called CC=14 SCAR=26.5
675  
676 72707 my $tx;
677 102183 my %socks2slots = map { $_->{'sock_no'} => $_ }
678 128418 ▶ grep { ! $_->{'tx'} && ! $_->{'reconnect_is_required'} && $_->{'is_busy'} }
Condition83%5/6
!($_->{'tx'} || $_->{'reconnect_is_required'}) && $_->{'is_busy'}
!ll&&!rl&&r
626229102183
$_->{'tx'} || $_->{'reconnect_is_required'}
l!l&&r!l&&!r
06128412
MC/DC66%2/3
!($_->{'tx'} || $_->{'reconnect_is_required'}) && $_->{'is_busy'}
$_->{'tx'} $_->{'reconnect_is_required'} $_->{'is_busy'}
Truth table75%3/4
not(A || B) && C
A= $_->{'tx'}B= $_->{'reconnect_is_required'}C= $_->{'is_busy'}
ABCresult
1XX0
01X0
0000
0011
679 72707 $self->{'_conns'}->@*;
680  
681 72707 ▶ if (%socks2slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (%socks2slots) 72702 5 72707
682  
683 72702 local $!;
684 72702 my $read_handles = '';
685  
686 72702 vec($read_handles, $_, 1) = 1 for keys %socks2slots;
687  
688 72702 my $error_handles = $read_handles;
689 72702 my ($nfound, $timeleft) = select($read_handles, undef, $error_handles, 0);
690  
691 72702 $self->_check_for_errors(\%socks2slots, $error_handles, $!);
692  
693 72702 for my $sock_no (keys %socks2slots) {
694 102182 my $slot = $socks2slots{ $sock_no };
695 102182 ▶ if ( $nfound && vec($read_handles, $sock_no, 1) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound and vec($read_handles, $sock_no, 1) == 1) { } 11 102171 102182
Condition100%3/3
$nfound and vec($read_handles, $sock_no, 1) == 1
!ll&&!rl&&r
102169211
MC/DC100%2/2
$nfound and vec($read_handles, $sock_no, 1) == 1
$nfound vec($read_handles, $sock_no, 1) == 1
Truth table100%3/3
A and B
A= $nfoundB= vec($read_handles, $sock_no, 1) == 1
ABresult
0X0
100
111
696 11 $self->_try_to_read($slot);
697 11 ▶ next if ! $slot->{'tx'};
Branch: 1 branch
BranchTrueFalseTotal
unless $slot->{"tx"} 4 7 11
698 7 ▶ next if ! $slot->{'is_busy'};
Branch: 1 branch
BranchTrueFalseTotal
unless $slot->{"is_busy"} 0 7 7
699 7 $tx = $slot->{'tx'};
700 } else {
701 102171 ▶ if (!$slot->{'tx'} && ($slot->{'exp_ts'} && time() > $slot->{'exp_ts'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})) 5 102166 102171
Condition66%4/6
not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})
!ll&&!rl&&r
01021665
$slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
!ll&&!rl&&r
01021665
MC/DC33%1/3
not $slot->{"tx"} and ($slot->{"exp_ts"} and &MojoX::HTTP::Async::time() > $slot->{"exp_ts"})
not $slot->{"tx"} $slot->{"exp_ts"} &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
Truth table50%2/4
not(A) and B and C
A= not $slot->{"tx"}B= $slot->{"exp_ts"}C= &MojoX::HTTP::Async::time() > $slot->{"exp_ts"}
ABCresult
0XX0
10X0
1100
1111
702 5 $self->_mark_response_as_timeouted($slot);
703 5 $tx = $slot->{'tx'};
704 }
705 }
706  
707 102178 ▶ if ($tx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx) 12 102166 102178
708 12 $self->_clear_slot($slot, 0);
709 12 last;
710 }
711 }
712 }
713  
714 72707 return $tx;
715 }
716  
717 =head2 refresh_connections ($self)
718  
719 Closes connections in slots in the following cases:
720  
721 1. The slot was marked as timeouted
722 2. The "inactivity_conn_ts" was set and the connection was expired
723 3. There are some errors in socket (for example: Connection reset by peer, Broken pipe, etc)
724  
725 Returns the amount of made reconnections.
726  
727 =cut
728  
729 5 ▶sub refresh_connections ($self) {
Subroutine refresh_connections: called CC=12 SCAR=25.5
Pod documented
730  
731 5 my $n = 0;
732 5 my $now = time();
733 5 ▶ my $keep_ts = $self->{'inactivity_conn_ts'} // 0;
Condition50%1/2
$self->{"inactivity_conn_ts"} // 0
l!l
50
MC/DC0%0/1
$self->{"inactivity_conn_ts"} // 0
$self->{"inactivity_conn_ts"}
Truth table50%1/2
A
A= $self->{"inactivity_conn_ts"}
Aresult
11
00
734  
735 5 ▶ if (scalar $self->{'_conns'}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"_conns"}}) 5 0 5
736  
737 5 local $!;
738 5 my $error_handles = '';
739 5 my %socks2slots = map { $_->{'sock_no'} => $_ } $self->{'_conns'}->@*;
740  
741 5 vec($error_handles, $_, 1) = 1 for keys %socks2slots;
742 5 select(undef, undef, $error_handles, 0);
743  
744 5 $self->_check_for_errors(\%socks2slots, $error_handles, $!); # broken connections will be marked as required to reconnect
745 }
746  
747 5 for my $i (reverse( 0 .. $#{ $self->{'_conns'} })) {
748 9 my $slot = $self->{'_conns'}->[$i];
749 9 ▶ my $slot_exp_ts = ($slot->{'last_response_ts'} || $slot->{'connected_ts'}) + $keep_ts;
Condition33%1/3
$slot->{'last_response_ts'} || $slot->{'connected_ts'}
l!l&&r!l&&!r
090
MC/DC0%0/2
$slot->{'last_response_ts'} || $slot->{'connected_ts'}
$slot->{'last_response_ts'} $slot->{'connected_ts'}
Truth table33%1/3
A || B
A= $slot->{'last_response_ts'}B= $slot->{'connected_ts'}
ABresult
1X1
011
000
750 9 ▶ my $is_outdated = $keep_ts && $slot_exp_ts <= $now;
Condition100%3/3
$keep_ts && $slot_exp_ts <= $now
!ll&&!rl&&r
243
MC/DC100%2/2
$keep_ts && $slot_exp_ts <= $now
$keep_ts $slot_exp_ts <= $now
Truth table100%3/3
A && B
A= $keep_tsB= $slot_exp_ts <= $now
ABresult
0X0
100
111
751  
752 9 ▶ warn("Outdated\n") if $self->{'debug'} && $is_outdated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} and $is_outdated 0 9 9
Condition33%1/3
$self->{"debug"} and $is_outdated
!ll&&!rl&&r
900
MC/DC0%0/2
$self->{"debug"} and $is_outdated
$self->{"debug"} $is_outdated
Truth table33%1/3
A and B
A= $self->{"debug"}B= $is_outdated
ABresult
0X0
100
111
753  
754 9 ▶ if ($slot->{'reconnect_is_required'} || $is_outdated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot->{"reconnect_is_required"} or $is_outdated) 5 4 9
Condition100%3/3
$slot->{"reconnect_is_required"} or $is_outdated
l!l&&r!l&&!r
234
MC/DC100%2/2
$slot->{"reconnect_is_required"} or $is_outdated
$slot->{"reconnect_is_required"} $is_outdated
Truth table100%3/3
A or B
A= $slot->{"reconnect_is_required"}B= $is_outdated
ABresult
1X1
011
000
755 5 ▶ warn("Going to reconnect\n") if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 5 5
756 5 $self->_clear_slot($slot, 1);
757 5 $self->_connect_slot($slot);
758 5 $n++;
759 }
760 }
761  
762 5 return $n;
763 }
764  
765 =head2 close_all ($self)
766  
767 Closes all opened connections and resets all slots with requests.
768  
769 =cut
770  
771 5 ▶sub close_all ($self) {
Subroutine close_all: called CC=2 SCAR=6.9
Pod documented
772 5 $self->_clear_slot($_, 1) for $self->{'_conns'}->@*;
773 5 $self->{'_conns'} = [];
774 5 return;
775 }
776  
777 =head2 DESTROY($self)
778  
779 The class destructor.
780  
781 Closes all opened sockets.
782  
783 =cut
784  
785 5 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=5 SCAR=16.7
786 5 my $in_use = 0;
787 5 ▶ while ( my $slot = shift($self->{'_conns'}->@*) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $slot = shift @{$$self{"_conns"}}) 5 5 10
788 5 ▶ $in_use++ if ($slot->{'is_busy'});
Branch: 1 branch
BranchTrueFalseTotal
if $slot->{"is_busy"} 0 5 5
789 5 ▶ $slot->{'socket'}->close() if ($slot->{'socket'});
Branch: 1 branch
BranchTrueFalseTotal
if $slot->{"socket"} 5 0 5
790 }
791 5 ▶ warn ref($self) ." object destroyed but still in use" if $in_use;
Branch: 1 branch
BranchTrueFalseTotal
if $in_use 0 5 5
792 }
793  
794 25 1;
795 __END__
↑ Summary