blib/lib/DBIx/Timeout.pm

filter: Statementstmt 33.9% 19 / 56 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 55.5% 5 / 9 Podpod 100% 1 / 1 totaltotal 29.4% 25 / 85 CC 9 SCAR 30.0
CC
9
Cov
26%
_fork_child
42.0
call_with_timeout
20.0
__ANON__
2.0
CRAP 20.1
↑ Summary
1 package DBIx::Timeout;
2  
3 3 require 5.008; # v5.8.0+ needed for safe signals
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '1.01';
9  
10 3 ▶use Params::Validate qw(validate CODEREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use POSIX qw(_exit);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $TIMEOUT_EXIT_CODE = 29;
15  
16 0 ▶sub call_with_timeout {
Subroutine call_with_timeout: not called CC=4 SCAR=30.0
Pod documented
17 0 my $pkg = shift;
18  
19 0 my %args =
20 validate(@_,
21 {dbh => {isa => 'DBI::db'},
22 code => {type => CODEREF},
23 timeout => 1
24 });
25 0 my ($dbh, $code, $timeout) = @args{('dbh', 'code', 'timeout')};
26  
27 0 my $child_pid = $pkg->_fork_child($dbh, $timeout);
28  
29 # run code, trapping error since it may have come from the timeout
30 # connection killing
31 0 eval { $code->() };
32 0 my $err = $@;
33  
34 # signal the child that processing is done - will wake up from
35 # sleep if timeout didn't already pass. It's ok if this fails,
36 # that means the child is probably already done.
37 0 kill USR1 => $child_pid;
38  
39 # reap the child, examining exit code to determine if timeout fired
40 0 ▶ if (waitpid $child_pid, 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid $child_pid, 0) { } 0 0 0
41 0 my $exit_code = $? >> 8;
42 0 ▶ if ($exit_code == $TIMEOUT_EXIT_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == $DBIx::Timeout::TIMEOUT_EXIT_CODE) 0 0 0
43 0 return 0;
44 }
45 } else {
46 0 croak("waitpid() failed: $!");
47 }
48  
49 # the error wasn't a timeout, rethrow
50 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
51  
52 # everything is all right
53 0 return 1;
54 }
55  
56 # forks off the child process
57 0 ▶sub _fork_child {
Subroutine _fork_child: not called CC=6 SCAR=37.4
58 0 my ($pkg, $dbh, $timeout) = @_;
59  
60 # pull a list of active handles for use after the fork
61 0 my %drivers = DBI->installed_drivers();
62 my @active_dbh =
63 0 ▶ grep { $_ and $_->isa('DBI::db') and $_->{Active} }
Condition0%0/6
$_ && $_->isa('DBI::db') && $_->{'Active'}
!ll&&!rl&&r
000
$_ and $_->isa("DBI::db")
!ll&&!rl&&r
000
MC/DC0%0/3
$_ && $_->isa('DBI::db') && $_->{'Active'}
$_ $_->isa("DBI::db") $_->{'Active'}
Truth table0%0/4
A and B && C
A= $_B= $_->isa("DBI::db")C= $_->{'Active'}
ABCresult
0XX0
10X0
1100
1111
64 0 map { @{$_->{ChildHandles}} } values %drivers;
65  
66 # do the fork, return in the parent
67 0 my $child_pid = fork();
68 0 ▶ croak("Failed to fork(): $!") unless defined $child_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_pid 0 0 0
69 0 ▶ return $child_pid if $child_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $child_pid 0 0 0
70  
71 # do the dance needed to keep open DBI connections from causing
72 # errors when this child exits
73 0 foreach my $active_dbh (@active_dbh) {
74 0 $active_dbh->{InactiveDestroy} = 1;
75 }
76  
77 # setup a (safe) signal handler for USR1 which will exit early
78 # from sleep()
79 0 ▶ local $SIG{USR1} = sub { _exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
80  
81 # now running in the child, sleep for $timeout seconds
82 0 sleep $timeout;
83  
84 # turn off USR1 handler, signalling now won't kill us in the
85 # middle of killing the parent's thread
86 0 $SIG{USR1} = 'IGNORE';
87  
88 # woke up, time to kill parent's thread
89 0 $pkg->_kill_connection($dbh);
90  
91 # tell the parent what happened (use POSIX::_exit() to make sure
92 # the parent really gets the message - otherwise END blocks can
93 # change the exit code)
94 0 _exit($TIMEOUT_EXIT_CODE);
95 }
96  
97  
98 # MySQL specific thread-killer
99 0 ▶sub _kill_connection {
Subroutine _kill_connection: not called CC=1 SCAR=6.9
100 0 my ($self, $dbh) = @_;
101  
102 0 my $thread_id = $dbh->{thread_id};
103  
104 0 my $new_dbh = $dbh->clone();
105 0 $new_dbh->{InactiveDestroy} = 0;
106 0 $new_dbh->do("KILL $thread_id");
107 }
108  
109  
110 3 1;
111  
112 __END__
↑ Summary