blib/lib/DBIO/Storage/PoolBase.pm

filter: Statementstmt 92.6% 682 / 736 Branchbran 72.3% 298 / 412 Conditioncond 59.0% 180 / 305 MC/DCmcdc 40.6% 72 / 177 Subroutinesub 92.0% 93 / 101 Podpod 100% 12 / 12 totaltotal 76.7% 1337 / 1743 CC 329 SCAR 26.6
CC
329
Cov
80%
_reset_then_hand_on
65.9
_report_hand_over_death
56.0
shutdown
31.2
CRAP 14.3
« blib/lib/DBIO/Test/Kubernetes.pm ↑ Summary blib/lib/DBIO/Row.pm »
1 package DBIO::Storage::PoolBase;
2 # ABSTRACT: Shared connection pool mechanics for async DBIO drivers
3  
4 140 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 140 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 140 our $VERSION = '0.900007';
8  
9 140 ▶use base 'DBIO::Storage::Pool';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 140 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 140 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 140 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 140 ▶use DBIO::Componentised ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 140 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 140 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 140 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 140 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21  
22 679 ▶sub new {
Subroutine new: called CC=23 SCAR=31.4
Pod documented
23 679 my ($class, %args) = @_;
24 croak('conninfo or conninfo_provider required')
25 679 ▶ unless $args{conninfo} || $args{conninfo_provider};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"conninfo"} or $args{"conninfo_provider"} 18 661 679
Condition100%3/3
$args{"conninfo"} or $args{"conninfo_provider"}
l!l&&r!l&&!r
51214918
MC/DC100%2/2
$args{"conninfo"} or $args{"conninfo_provider"}
$args{"conninfo"} $args{"conninfo_provider"}
Truth table100%3/3
A or B
A= $args{"conninfo"}B= $args{"conninfo_provider"}
ABresult
1X1
011
000
26  
27 # A size that is false is no size: the default. Any other has to be a
28 # positive integer. acquire spawns while the pool counts fewer connections
29 # than max_size, so a negative or non-numeric one never spawns: every acquire
30 # is queued for a release nobody can make (karr core k270).
31 661 ▶ my $size = $args{size} || 5;
Condition100%2/2
$args{'size'} || 5
l!l
63328
MC/DC100%1/1
$args{'size'} || 5
$args{'size'}
Truth table100%2/2
A
A= $args{'size'}
Aresult
11
00
32 661 ▶ unless (!ref $size && $size =~ /\A[1-9][0-9]*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $size and $size =~ /\A[1-9][0-9]*\z/) 17 644 661
Condition100%3/3
not ref $size and $size =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
215644
MC/DC100%2/2
not ref $size and $size =~ /\A[1-9][0-9]*\z/
not ref $size $size =~ /\A[1-9][0-9]*\z/
Truth table100%3/3
not(A) and B
A= not ref $sizeB= $size =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
33 17 ▶ my $got = ref $size ? 'a ' . ref($size) . ' reference'
Branch: 2 branches
BranchTrueFalseTotal
ref $size ? : 2 15 17
&Scalar::Util::looks_like_number($size) ? : 10 5 15
34 : Scalar::Util::looks_like_number($size) ? $size
35 : 'a value that is no number';
36 17 DBIO::Exception->throw("Pool size must be a positive integer, got $got");
37 }
38  
39 644 my $storage = $args{storage};
40 644 my %limits;
41 644 for my $name (qw( acquire_timeout max_waiters reset_timeout )) {
42 1914 ▶ my $given = exists $args{$name} ? $args{$name}
Branch: 2 branches
BranchTrueFalseTotal
exists $args{$name} ? : 19 1895 1914
builtin::blessed($storage) && $storage->can('async_option') ? : 156 1739 1895
Condition100%3/3
builtin::blessed($storage) and $storage->can("async_option")
!ll&&!rl&&r
1589150156
MC/DC100%2/2
builtin::blessed($storage) and $storage->can("async_option")
builtin::blessed($storage) $storage->can("async_option")
Truth table100%3/3
A and B
A= builtin::blessed($storage)B= $storage->can("async_option")
ABresult
0X0
100
111
43 : Scalar::Util::blessed($storage) && $storage->can('async_option')
44 ? $storage->async_option($name)
45 : undef;
46 1914 ▶ $class->assert_limit_option($name => $given) if defined $given;
Branch: 1 branch
BranchTrueFalseTotal
if defined $given 23 1891 1914
47 1902 $limits{$name} = $given;
48 }
49  
50 my $self = bless {
51 conninfo => $args{conninfo},
52 conninfo_provider => $args{conninfo_provider},
53 max_size => $size,
54 0 ▶ on_error => $args{on_error} || sub { warn "Pool error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 _connections => [],
56 _idle => [],
57 _waiters => [],
58 _spawning => {},
59 _leases => {},
60 _lease_seq => 0,
61 _shutdown => 0,
62 _owner_pid => $$,
63 acquire_timeout => $limits{acquire_timeout} // 30,
64 max_waiters => $limits{max_waiters},
65 632 ▶ reset_timeout => $limits{reset_timeout} // 10,
Condition71%5/7
$args{'on_error'} || sub { warn "Pool error: $_[0]\n"; }
l!l&&r!l&&!r
1614710
$limits{"acquire_timeout"} // 30
l!l
8624
$limits{"reset_timeout"} // 10
l!l
0632
MC/DC0%0/2
$args{'on_error'} || sub { warn "Pool error: $_[0]\n"; }
$args{'on_error'} sub { warn "Pool error: $_[0]\n"; }
MC/DC100%1/1
$limits{"acquire_timeout"} // 30
$limits{"acquire_timeout"}
MC/DC0%0/1
$limits{"reset_timeout"} // 10
$limits{"reset_timeout"}
Truth table66%2/3
A || B
A= $args{'on_error'}B= sub { warn "Pool error: $_[0]\n"; }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $limits{"acquire_timeout"}
Aresult
11
00
Truth table50%1/2
A
A= $limits{"reset_timeout"}
Aresult
11
00
66 _resetting => {},
67 }, $class;
68  
69 632 ▶ $self->{future_class} = $args{future_class} if $args{future_class};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"future_class"} 632 0 632
70 632 my $fc = $self->future_class;
71 # A name that is no class name throws before anything is loaded.
72 632 DBIO::Util::assert_class_name($fc);
73 632 ▶ eval { DBIO::Componentised->ensure_class_loaded($fc); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "DBIO::Componentised"->ensure_class_loaded($fc); 1 } } or croak("Cannot load future class ${fc}: $@") 631 1 632
74 or croak "Cannot load future class $fc: $@";
75  
76 # Optional back-reference to the owning DBIO::Storage::Async, held
77 # weakly (the storage owns the pool, not the other way round). When present,
78 # the pool replays the storage's configured on_connect / on_disconnect actions
79 # against every freshly-spawned / torn-down connection -- see the connect-action
80 # hooks in L</_spawn_connection> and L</shutdown>. A pool used standalone, or an
81 # async pool whose owner does not wire it, simply carries no {storage} and skips
82 # the hooks entirely.
83 631 ▶ if (defined $args{storage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"storage"}) 102 529 631
84 102 $self->{storage} = $args{storage};
85 102 ▶ Scalar::Util::weaken($self->{storage}) if ref $self->{storage};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"storage"} 102 0 102
86 }
87  
88 DBIO::Exception->throw(
89 'acquire_timeout needs a timer, and this pool has no owning storage to '
90 . 'ask for one (storage => $async_storage): give it acquire_timeout 0'
91 631 ▶ ) if defined $limits{acquire_timeout} && $limits{acquire_timeout} > 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $limits{"acquire_timeout"} and $limits{"acquire_timeout"} > 0 and not $self->_has_timer 1 630 631
Condition100%6/6
defined $limits{"acquire_timeout"} and $limits{"acquire_timeout"} > 0 and not $self->_has_timer
!ll&&!rl&&r
62461
defined $limits{"acquire_timeout"} and $limits{"acquire_timeout"} > 0
!ll&&!rl&&r
62317
MC/DC100%3/3
defined $limits{"acquire_timeout"} and $limits{"acquire_timeout"} > 0 and not $self->_has_timer
defined $limits{"acquire_timeout"} $limits{"acquire_timeout"} > 0 not $self->_has_timer
Truth table100%4/4
A and B and not(C)
A= defined $limits{"acquire_timeout"}B= $limits{"acquire_timeout"} > 0C= not $self->_has_timer
ABCresult
0XX0
10X0
1100
1111
92 && !$self->_has_timer;
93  
94 630 return $self;
95 }
96  
97  
98 38 ▶sub assert_limit_option {
Subroutine assert_limit_option: called CC=18 SCAR=31.2
Pod documented
99 38 my ($class, $name, $value) = @_;
100  
101 38 ▶ my $takes = $name eq 'max_waiters' ? 'a positive integer'
Branch: 4 branches
BranchTrueFalseTotal
$name eq 'max_waiters' ? : 13 25 38
$name eq 'acquire_timeout' ? : 20 5 25
$name eq 'reset_timeout' ? : 0 5 5
$name eq 'txn_timeout' ? : 5 0 5
102 : $name eq 'acquire_timeout' ? 'a number of seconds, 0 or more (0 turns the timeout off)'
103 : $name eq 'reset_timeout' ? 'a number of seconds, 0 or more (0 turns the timeout off)'
104 : $name eq 'txn_timeout' ? 'a number of seconds above 0'
105 : DBIO::Exception->throw("Unknown limit option '$name'");
106  
107 38 ▶ my $number = defined $value && !ref $value
Condition75%9/12
defined $value && !ref($value) && &Scalar::Util::looks_like_number($value) && $value == $value && abs $value != 9**9**9
!ll&&!rl&&r
9029
defined $value and not ref $value and &Scalar::Util::looks_like_number($value) and $value == $value
!ll&&!rl&&r
9029
defined $value and not ref $value and &Scalar::Util::looks_like_number($value)
!ll&&!rl&&r
2729
defined $value and not ref $value
!ll&&!rl&&r
0236
MC/DC40%2/5
defined $value && !ref($value) && &Scalar::Util::looks_like_number($value) && $value == $value && abs $value != 9**9**9
defined $value not ref $value &Scalar::Util::looks_like_number($value) $value == $value abs $value != 9**9**9
Truth table50%3/6
A and not(B) and C and D && E
A= defined $valueB= not ref $valueC= &Scalar::Util::looks_like_number($value)D= $value == $valueE= abs $value != 9**9**9
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
108 && Scalar::Util::looks_like_number($value)
109 && $value == $value && abs($value) != 9**9**9;
110 38 ▶ my $valid = $name eq 'max_waiters' ? (defined $value && !ref $value && $value =~ /\A[1-9][0-9]*\z/)
Branch: 3 branches
BranchTrueFalseTotal
$name eq 'max_waiters' ? : 13 25 38
$name eq 'acquire_timeout' ? : 20 5 25
$name eq 'reset_timeout' ? : 0 5 5
Condition60%9/15
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
085
defined $value and not ref $value
!ll&&!rl&&r
0013
$number and $value >= 0
!ll&&!rl&&r
5312
$number and $value >= 0
!ll&&!rl&&r
000
$number and $value > 0
!ll&&!rl&&r
122
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
defined $value not ref $value $value =~ /\A[1-9][0-9]*\z/
MC/DC100%2/2
$number and $value >= 0
$number $value >= 0
MC/DC0%0/2
$number and $value >= 0
$number $value >= 0
MC/DC100%2/2
$number and $value > 0
$number $value > 0
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
Truth table100%3/3
A and B
A= $numberB= $value >= 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $numberB= $value >= 0
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $numberB= $value > 0
ABresult
0X0
100
111
111 : $name eq 'acquire_timeout' ? ($number && $value >= 0)
112 : $name eq 'reset_timeout' ? ($number && $value >= 0)
113 : ($number && $value > 0);
114  
115 38 ▶ DBIO::Exception->throw("Connect option '$name' takes $takes")
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 19 19 38
116 unless $valid;
117 19 return 1;
118 }
119  
120  
121 9 ▶sub _delay {
Subroutine _delay: called CC=3 SCAR=12.0
122 9 my ($self, $seconds) = @_;
123 9 my $storage = $self->{storage};
124 9 ▶ DBIO::Exception->throw('This pool has no owning storage to ask for a timer')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($storage) and $storage->can("_delay") 0 9 9
Condition33%1/3
builtin::blessed($storage) and $storage->can("_delay")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($storage) and $storage->can("_delay")
builtin::blessed($storage) $storage->can("_delay")
Truth table33%1/3
A and B
A= builtin::blessed($storage)B= $storage->can("_delay")
ABresult
0X0
100
111
125 unless Scalar::Util::blessed($storage) && $storage->can('_delay');
126 9 return $storage->_delay($seconds);
127 }
128  
129 # True when _delay has a timer to ask. Overridden together with _delay.
130 316 ▶sub _has_timer {
Subroutine _has_timer: called CC=3 SCAR=11.0
131 316 my ($self) = @_;
132 316 my $storage = $self->{storage};
133 316 ▶ return Scalar::Util::blessed($storage) && $storage->can('_delay') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($storage) && $storage->can('_delay') ? : 28 288 316
Condition100%3/3
builtin::blessed($storage) and $storage->can("_delay")
!ll&&!rl&&r
2365228
MC/DC100%2/2
builtin::blessed($storage) and $storage->can("_delay")
builtin::blessed($storage) $storage->can("_delay")
Truth table100%3/3
A and B
A= builtin::blessed($storage)B= $storage->can("_delay")
ABresult
0X0
100
111
134 }
135  
136  
137 4726 ▶sub future_class { $_[0]->{future_class} || 'Future' }
Subroutine future_class: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"future_class"} or "Future" 4725 1 4726
138  
139  
140 1522 ▶sub acquire {
Subroutine acquire: called CC=2 SCAR=6.9
Pod documented
141 1522 my ($self) = @_;
142 1522 ▶ return $self->_shutdown_failure if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 7 1515 1522
143 1515 $self->_verify_pid;
144  
145 # What this caller's timeout is counted from, shared by the stages of its
146 # wait: the queue, then a connection coming up (karr core k397).
147 1515 my $wait = {};
148  
149 1328 ▶ return $self->_acquire_slot($wait)->then(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
150 1328 my $conn = shift;
151 1328 my ($ready, $error);
152 1328 ▶ unless (eval { $ready = $self->_connection_ready_future($conn); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $ready = $self->_connection_ready_future($conn); 1 } }) 3 1325 1328
153 3 $error = $@;
154 3 return $self->_retire_readiness_failure($conn, $error);
155 }
156  
157 # The seam is not the pool's code. A pool shut down from inside it closed
158 # $conn with the rest: there is nothing to hand out (karr core k277).
159 1325 ▶ return $self->_shutdown_failure if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 2 1323 1325
160  
161 return $ready->then(
162 1219 ▶ sub { $self->future_class->done($conn) },
Subroutine __ANON__: called CC=1 SCAR=0.0
163 2 ▶ sub { $self->_retire_readiness_failure($conn, $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
164 1323 ▶ ) if $ready->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $ready->is_ready 1221 102 1323
165  
166 102 return $self->_hand_over_when_ready($conn, $ready, $wait);
167 1515 });
168 }
169  
170 # A connection still coming up is handed to its caller through a Future of the
171 # caller's own, never through a ->then off the readiness Future: Future.pm
172 # carries a cancel down a ->then chain, so a caller that gave up -- a cancel, a
173 # wait_any timeout -- cancelled the pool's readiness Future, and the connection,
174 # counted against max_size, was never idle and never handed out; in a pool of
175 # one, for good (karr core k258). The pool holds the chain off the readiness
176 # Future until it settles. Done: the connection
177 # goes to the caller if it still waits, else where release sends one -- to the
178 # next caller still waiting, or back to idle. Failed: the connection is retired
179 # and its slot handed on (_retire_unready_connection), and the failure reaches
180 # the caller if it still waits. The caller's Future is of the class a ->then
181 # off the readiness Future would have been.
182 #
183 # What the chain acts for -- the caller and the connection -- it reaches only
184 # through $state, which the pool can empty: a chain cannot be taken off the
185 # readiness Future again, but it can be left with nothing to act for (karr core
186 # k261, _park_hand_over).
187 102 ▶sub _hand_over_when_ready {
Subroutine _hand_over_when_ready: called CC=4 SCAR=14.3
188 102 my ($self, $conn, $ready, $wait) = @_;
189  
190 102 my $caller = $ready->new;
191 # The connection coming up is part of the caller's wait: the remainder of
192 # its acquire_timeout runs on the caller's Future (karr core k397). A timer
193 # that cannot be armed is reported; the connection is handed over untimed.
194 102 ▶ if (defined(my $timer_error = $self->_arm_acquire_timer($wait ||= {}, $caller))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $timer_error = $self->_arm_acquire_timer($wait ||= {}, $caller))) 0 102 102
Condition50%1/2
$wait ||= {}
l!l
1020
MC/DC0%0/1
$wait ||= {}
$wait
Truth table50%1/2
A
A= $wait
Aresult
11
00
195 0 _report_hand_over_death($self, $timer_error, 'the acquire timeout could not be armed');
196 }
197 102 my $answer = $ready->new; # what the chain's callback returns, either way
198 102 my $state = { caller => $caller, conn => $conn };
199 102 my $key = ++$self->{_hand_over_seq};
200 102 my $pool = $self;
201 102 Scalar::Util::weaken($pool);
202  
203 # The hand-over names its connection, weakly: shutdown has to know a
204 # connection an acquire still waits on from one that came up (karr core
205 # k262), and the pool's connection list is what keeps a connection alive.
206 102 my $handover = $self->{_ready_handovers}{$key} = [ $caller ];
207 102 Scalar::Util::weaken( $handover->[2] = $conn );
208 102 @$handover[ 3, 4 ] = ($state, $ready);
209 $handover->[1] = my $chain = $ready->then(
210 48 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
211 48 my ($caller, $conn) = delete @$state{qw(caller conn)};
212 48 _forget_hand_over($pool, $key);
213 48 ▶ return $answer->done unless defined $caller; # let go of meanwhile
Branch: 1 branch
BranchTrueFalseTotal
unless defined $caller 10 38 48
214 38 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if (not $caller->is_ready) { $caller->done($conn); } elsif ($pool) { $pool->_release_unused($conn); } ; 1 } } or _report_hand_over_death($pool, $@) 38 0 38
215 38 ▶ if (!$caller->is_ready) { $caller->done($conn) }
Branch: 2 branches
BranchTrueFalseTotal
if (not $caller->is_ready) { } 36 2 38
elsif ($pool) { } 2 0 2
216 2 elsif ($pool) { $pool->_release_unused($conn) }
217 38 1;
218 } or _report_hand_over_death($pool, $@);
219 38 return $answer->done;
220 },
221 28 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
222 28 my ($error) = @_;
223 28 my ($caller, $conn) = delete @$state{qw(caller conn)};
224 28 my $retired = delete $state->{retired};
225 28 _forget_hand_over($pool, $key);
226 28 ▶ if (defined $caller) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $caller) { } 21 7 28
elsif ($retired and $pool) { } 3 8 11
227 21 ▶ $pool->_retire_unready_connection($conn) if $pool;
Branch: 1 branch
BranchTrueFalseTotal
if $pool 21 0 21
228 21 ▶ eval { $caller->fail($error) unless $caller->is_ready; 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $caller->fail($error) unless $caller->is_ready; 1 } } or _report_hand_over_death($pool, $@) 21 0 21
unless $caller->is_ready 21 0 21
229 or _report_hand_over_death($pool, $@);
230 }
231 ▶ elsif ($retired && $pool) {
Condition66%2/3
$retired and $pool
!ll&&!rl&&r
403
MC/DC50%1/2
$retired and $pool
$retired $pool
Truth table66%2/3
A and B
A= $retiredB= $pool
ABresult
0X0
100
111
232 # Retired while it was coming up, and now the connect has failed as
233 # well: the slot is offered to the oldest caller still waiting, as
234 # after any connect that failed (karr core k260).
235 3 $pool->_spawn_for_next_waiter;
236 }
237 28 return $answer->done;
238 },
239 102 );
240  
241 # The callbacks above forget the hand-over, and with it the only reference
242 # the pool has to $chain, while $chain's own callback is running. Future::XS
243 # does not hold a Future during its callback itself, so the chain is kept
244 # here until it has settled -- after its callback returned.
245 102 ▶ if ($chain->can('on_ready')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chain->can("on_ready")) 0 102 102
246 0 $self->{_hand_over_chains}{$key} = $chain;
247 0 ▶ $chain->on_ready(sub { delete $pool->{_hand_over_chains}{$key} if $pool });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $pool 0 0 0
248 }
249 102 return $caller;
250 }
251  
252 # A plain function, as the chain holds the pool weakly: the hand-over is over,
253 # whichever table the pool kept it in.
254 76 ▶sub _forget_hand_over {
Subroutine _forget_hand_over: called CC=2 SCAR=7.0
255 76 my ($pool, $key) = @_;
256 76 ▶ return unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 0 76 76
257 76 delete $pool->{_ready_handovers}{$key};
258 76 delete $pool->{_parked_handovers}{$key};
259 76 return;
260 }
261  
262 # Let go of a hand-over whose connection left the pool before it came up --
263 # retired, or dropped as inherited in a forked child (karr core k261). The
264 # chain stays on the readiness Future, which may still settle: it is left with
265 # nothing to act for, so that settling hands nobody a connection the pool no
266 # longer holds, and the connection and the caller's Future are held by nothing
267 # of the pool's any more. The chain itself, and the readiness Future it was
268 # made off, are parked until that Future settles: Future.pm warns of a
269 # sequence Future it lost when a chain is gone before its Future settles, and
270 # DBIO::Test::Future keeps a readiness Future only from where it is held.
271 # Returns the caller's Future, for whoever has something to tell it.
272 15 ▶sub _park_hand_over {
Subroutine _park_hand_over: called CC=3 SCAR=11.2
273 15 my ($self, $key) = @_;
274 15 ▶ my $handover = delete $self->{_ready_handovers}{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $handover = delete $self->{"_ready_handovers"}{$key} or return 15 0 15
275  
276 15 my ($caller, $chain, undef, $state, $ready) = @$handover;
277 15 ▶ %$state = () if ref $state;
Branch: 1 branch
BranchTrueFalseTotal
if ref $state 15 0 15
278 15 $self->{_parked_handovers}{$key} = [ $chain, $ready, $state ];
279 15 return $caller;
280 }
281  
282 # _retire_connection for a connection an acquire still waits on. That caller
283 # waited for a connection that was gone, until shutdown (karr core k261): it
284 # fails here, unless it gave up already. Not during shutdown, which fails
285 # every caller itself.
286 128 ▶sub _let_go_of_hand_overs_for {
Subroutine _let_go_of_hand_overs_for: called CC=9 SCAR=24.1
287 128 my ($self, $conn) = @_;
288 128 ▶ my $handovers = $self->{_ready_handovers} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $handovers = $self->{"_ready_handovers"} or return 28 100 128
289  
290 28 my $addr = Scalar::Util::refaddr($conn);
291 0 for my $key (sort { $a <=> $b } keys %$handovers) {
292 8 ▶ my $handover = $handovers->{$key} or next; # gone in a callback meanwhile
Branch: 1 branch
BranchTrueFalseTotal
my $handover = $handovers->{$key} or next 8 0 8
293 8 my $named = $handover->[2];
294 8 ▶ next unless defined $named && Scalar::Util::refaddr($named) == $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $named and builtin::refaddr($named) == $addr 0 8 8
Condition33%1/3
defined $named and builtin::refaddr($named) == $addr
!ll&&!rl&&r
008
MC/DC0%0/2
defined $named and builtin::refaddr($named) == $addr
defined $named builtin::refaddr($named) == $addr
Truth table33%1/3
A and B
A= defined $namedB= builtin::refaddr($named) == $addr
ABresult
0X0
100
111
295  
296 8 my $caller = $self->_park_hand_over($key);
297 8 $self->{_parked_handovers}{$key}[2]{retired} = 1;
298 8 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $caller->fail("Connection was retired while it was coming up") if defined $caller and not $caller->is_ready; 1 } } or _report_hand_over_death($self, $@, "a caller was told that its connection was retired while it was coming up") 8 0 8
299 8 ▶ $caller->fail('Connection was retired while it was coming up')
Branch: 1 branch
BranchTrueFalseTotal
if defined $caller and not $caller->is_ready 8 0 8
Condition33%1/3
defined $caller and not $caller->is_ready
!ll&&!rl&&r
008
MC/DC0%0/2
defined $caller and not $caller->is_ready
defined $caller not $caller->is_ready
Truth table33%1/3
A and not(B)
A= defined $callerB= not $caller->is_ready
ABresult
0X0
100
111
300 if defined $caller && !$caller->is_ready;
301 8 1;
302 } or _report_hand_over_death($self, $@,
303 'a caller was told that its connection was retired while it was coming up');
304 }
305 28 return;
306 }
307  
308 # The hand-over runs under the ->then off the readiness Future, where a death
309 # becomes the failure of a Future nobody reads. A callback that dies there --
310 # on the caller's Future, or on that of the waiter release hands the connection
311 # on to -- used to reach whoever settled the readiness Future; it goes to
312 # on_error instead of nowhere. An on_error that dies in turn is warned with the
313 # report it was given, as in _idle_connection_usable. A plain function, not a
314 # method: the hand-over holds the pool weakly.
315 #
316 # The same holds wherever the pool settles a Future that is somebody else's --
317 # release, _spawn_for_next_waiter, shutdown (karr core k279). A Future runs its
318 # callbacks as it is settled, so the callback of one caller runs inside the
319 # pool method another caller called, and its death was thrown at that caller.
320 # Each such place says in $as what it was doing, for the report.
321 0 ▶sub _report_hand_over_death {
Subroutine _report_hand_over_death: not called CC=7 SCAR=40.3
322 0 my ($pool, $err, $as) = @_;
323 0 ▶ $as //= 'the outcome of a connect was handed over';
Condition0%0/2
$as //= "the outcome of a connect was handed over"
l!l
00
MC/DC0%0/1
$as //= "the outcome of a connect was handed over"
$as
Truth table0%0/2
A
A= $as
Aresult
11
00
324 0 ▶ my $report = "A callback died as $as: " . ($err || 'unknown error');
Condition0%0/2
$err || 'unknown error'
l!l
00
MC/DC0%0/1
$err || 'unknown error'
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
325 0 ▶ return if $pool && eval { $pool->{on_error}->($report); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $pool and eval { do { $pool->{"on_error"}->($report); 1 } } 0 0 0
Condition0%0/3
$pool and eval { do { $pool->{"on_error"}->($report); 1 } }
!ll&&!rl&&r
000
MC/DC0%0/2
$pool and eval { do { $pool->{"on_error"}->($report); 1 } }
$pool eval { do { $pool->{"on_error"}->($report); 1 } }
Truth table0%0/3
A and B
A= $poolB= eval { do { $pool->{"on_error"}->($report); 1 } }
ABresult
0X0
100
111
326  
327 0 ▶ chomp(my $died = $pool ? $@ || 'unknown error' : 'the pool is gone');
Branch: 1 branch
BranchTrueFalseTotal
$pool ? : 0 0 0
Condition0%0/2
$@ or "unknown error"
l!l
00
MC/DC0%0/1
$@ or "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
328 0 chomp $report;
329 0 warn "DBIO pool on_error died ($died) while reporting: $report\n";
330 0 return;
331 }
332  
333 18 ▶sub _shutdown_failure {
Subroutine _shutdown_failure: called CC=1 SCAR=0.0
334 18 my ($self) = @_;
335 18 return $self->future_class->fail('Connection pool has been shut down');
336 }
337  
338 5 ▶sub _retire_readiness_failure {
Subroutine _retire_readiness_failure: called CC=2 SCAR=7.0
339 5 my ($self, $conn, $error) = @_;
340  
341 # The reason a connection is retired for is masked like the spawn's error,
342 # with what the pool knows of its connect info (karr k400).
343 $error = $self->_mask_connect_error($error, $self->{conninfo})
344 5 ▶ if defined $self->{conninfo};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"conninfo"} 5 0 5
345 5 $self->_retire_unready_connection($conn);
346 5 return $self->future_class->fail($error);
347 }
348  
349 # A connection that did not become ready is retired the way a checked-out one
350 # is, through _retire_in_use_connection: its slot is what a queued caller waits
351 # for. _retire_connection alone leaves the queue alone, and that caller then
352 # waited for a release nobody was left to make -- in a pool of one, for good
353 # (karr core k260). The slot goes to the oldest caller still waiting, on a
354 # fresh spawn or with that spawn's error. A callback that dies as it is handed
355 # on is reported like any other death in a hand-over.
356 115 ▶sub _mask_connect_error {
Subroutine _mask_connect_error: called CC=6 SCAR=18.0
357 115 my ($self, $error, @conninfo) = @_;
358 my @known = map {
359 115 ref $_ eq 'HASH'
360 160 ▶ ? ( [ undef, undef, $_->{password} // $_->{Password} ] )
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 6 154 160
defined $_ && !ref($_) ? : 95 59 154
Condition66%4/6
$_->{"password"} // $_->{"Password"}
l!l&&r!l&&!r
420
defined $_ and not ref $_
!ll&&!rl&&r
59095
MC/DC0%0/2
$_->{"password"} // $_->{"Password"}
$_->{"password"} $_->{"Password"}
MC/DC50%1/2
defined $_ and not ref $_
defined $_ not ref $_
Truth table66%2/3
A // B
A= $_->{"password"}B= $_->{"Password"}
ABresult
1X1
011
000
Truth table66%2/3
A and not(B)
A= defined $_B= not ref $_
ABresult
0X0
100
111
361 : ( defined $_ && !ref $_ ? [ $_ ] : () )
362 } @conninfo;
363 115 $error = DBIO::Util::mask_connect_secrets($error);
364 115 $error = DBIO::Util::mask_connect_secrets($error, $_) for @known;
365 115 return $error;
366 }
367  
368 26 ▶sub _retire_unready_connection {
Subroutine _retire_unready_connection: called CC=2 SCAR=7.0
369 26 my ($self, $conn) = @_;
370 26 ▶ eval { $self->_retire_in_use_connection($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_retire_in_use_connection($conn); 1 } } or _report_hand_over_death($self, $@) 26 0 26
371 or _report_hand_over_death($self, $@);
372 26 return;
373 }
374  
375 # Raw pool-slot acquisition: idle reuse / capacity spawn / waiter queue, WITHOUT
376 # readiness gating. Returns a Future resolving to a connection object (or a
377 # pending waiter Future that resolves to one on release). acquire() wraps this in
378 # the _connection_ready_future seam so all three paths are gated identically and
379 # in exactly one place -- an async subclass overrides the seam, never this or
380 # acquire(). Kept as a distinct method so a subclass can still reach
381 # the un-gated slot logic if it ever needs to.
382 1525 ▶sub _acquire_slot {
Subroutine _acquire_slot: called CC=26 SCAR=32.8
383 1525 my ($self, $wait) = @_;
384  
385 1525 ▶ while (@{ $self->{_idle} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_idle"};}) 576 957 1533
386 # FIFO: hand out the connection that has been idle longest (the one at
387 # the front of the queue). Release pushes onto the tail, so shift takes
388 # from the head — this cycles every conn through instead of always
389 # reusing the most-recently-released one (LIFO), which caused 1 hot
390 # conn and N-1 cold conns under bursty load. One that died while idle
391 # is retired and the next is tried; with none left, spawn below.
392 576 my $conn = shift @{ $self->{_idle} };
393 576 my $usable = $self->_idle_connection_usable($conn);
394  
395 # The liveness seam and on_error are not the pool's code. A pool shut down
396 # from inside either closed $conn with the rest, and nothing is handed out
397 # or spawned after it (karr core k277).
398 576 ▶ return $self->_shutdown_failure if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 3 573 576
399 573 ▶ return $self->future_class->done($conn) if $usable;
Branch: 1 branch
BranchTrueFalseTotal
if $usable 565 8 573
400 }
401  
402 957 ▶ if ($self->_has_room) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_room) 755 202 957
403 # Spawning is synchronous, and it replays the owning storage's
404 # on_connect_do / on_connect_call against the fresh connection -- a server
405 # refusing one of those dies right here. acquire() is contracted to return a
406 # Future, and every other failure on this path (query error, connect
407 # timeout, txn failure) reaches the caller as a failed one; letting this
408 # single case escape as an exception meant the ->on_fail a Future-based API
409 # teaches you to write was the one place the error could never arrive, and
410 # every *_async call would have needed a defensive eval around it.
411 755 my $conn;
412 755 ▶ unless (eval { $conn = $self->_spawn_connection; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $conn = $self->_spawn_connection; 1 } }) 81 674 755
413 81 ▶ my $error = $@ || 'Connection spawn failed';
Condition50%1/2
$@ || 'Connection spawn failed'
l!l
810
MC/DC0%0/1
$@ || 'Connection spawn failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
414  
415 # The spawn held its slot while it ran, and a caller that came in from
416 # inside it was queued behind it. The spawn took no slot after all, so
417 # that caller gets it -- nobody is left to release one (karr core k277).
418 81 ▶ eval { $self->_spawn_for_next_waiter; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_spawn_for_next_waiter; 1 } } or _report_hand_over_death($self, $@) 81 0 81
419 or _report_hand_over_death($self, $@);
420 81 return $self->future_class->fail($error);
421 }
422  
423 # No connection and no error: the pool was shut down under the spawn.
424 674 ▶ return $self->_shutdown_failure unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 5 669 674
425 669 return $self->future_class->done($conn);
426 }
427  
428 # A full queue refuses the caller before anything of it is made or held
429 # (karr core k397). Waiters settled by somebody else without being withdrawn
430 # do not count: they are passed over at release (karr k255).
431 202 my $max = $self->{max_waiters};
432 202 ▶ if (defined $max && @{ $self->{_waiters} } >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and @{$self->{"_waiters"};} >= $max) 2 200 202
Condition100%3/3
defined $max and @{$self->{"_waiters"};} >= $max
!ll&&!rl&&r
19642
MC/DC100%2/2
defined $max and @{$self->{"_waiters"};} >= $max
defined $max @{$self->{"_waiters"};} >= $max
Truth table100%3/3
A and B
A= defined $maxB= @{$self->{"_waiters"};} >= $max
ABresult
0X0
100
111
433 2 ▶ @{ $self->{_waiters} } = grep { defined $_ && !$_->is_ready } @{ $self->{_waiters} };
Condition33%1/3
defined $_ && !$_->is_ready
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_ && !$_->is_ready
defined $_ !$_->is_ready
Truth table33%1/3
A && not(B)
A= defined $_B= !$_->is_ready
ABresult
0X0
100
111
434 return $self->future_class->fail(
435 _exception("pool exhausted: $max callers waiting")
436 2 ▶ ) if @{ $self->{_waiters} } >= $max;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_waiters"};} >= $max 2 0 2
437 }
438  
439 # Queue the caller. Its Future is the transport's to make (_new_waiter) and
440 # the pool's to queue and settle; a seam that fails to make one fails the
441 # acquire, which never throws, and queues nothing (karr core k256).
442 200 my $waiter;
443 200 ▶ unless (eval { $waiter = $self->_new_waiter; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $waiter = $self->_new_waiter; 1 } }) 1 199 200
444 1 ▶ return $self->future_class->fail($@ || '_new_waiter died');
Condition50%1/2
$@ || '_new_waiter died'
l!l
10
MC/DC0%0/1
$@ || '_new_waiter died'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
445 }
446 199 ▶ my $is_future = Scalar::Util::blessed($waiter)
Condition66%4/6
builtin::blessed($waiter) && $waiter->can('then') && $waiter->can('is_ready')
!ll&&!rl&&r
20197
builtin::blessed($waiter) and $waiter->can("then")
!ll&&!rl&&r
20197
MC/DC33%1/3
builtin::blessed($waiter) && $waiter->can('then') && $waiter->can('is_ready')
builtin::blessed($waiter) $waiter->can("then") $waiter->can('is_ready')
Truth table50%2/4
A and B && C
A= builtin::blessed($waiter)B= $waiter->can("then")C= $waiter->can('is_ready')
ABCresult
0XX0
10X0
1100
1111
447 && $waiter->can('then') && $waiter->can('is_ready');
448 199 ▶ unless ($is_future && !$waiter->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_future and not $waiter->is_ready) 3 196 199
Condition100%3/3
$is_future and not $waiter->is_ready
!ll&&!rl&&r
21196
MC/DC100%2/2
$is_future and not $waiter->is_ready
$is_future not $waiter->is_ready
Truth table100%3/3
A and not(B)
A= $is_futureB= not $waiter->is_ready
ABresult
0X0
100
111
449 3 ▶ return $self->future_class->fail(
Branch: 2 branches
BranchTrueFalseTotal
!defined($waiter) ? : 1 2 3
!$is_future ? : 1 1 2
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
450 (ref($self) || $self) . '->_new_waiter returned '
451 . ( !defined $waiter ? 'undef, not a Future'
452 : !$is_future ? 'no Future'
453 : 'a Future that is not pending' )
454 );
455 }
456  
457 # A pool shut down from inside the seam failed everyone queued by then; a
458 # caller queued now would wait for good (karr core k277).
459 196 ▶ return $self->_shutdown_failure if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 1 195 196
460  
461 # The caller's wait is timed from here. A timer that cannot be armed fails
462 # the acquire and queues nothing: the caller is never left to wait untimed
463 # by a pool that was asked to time it.
464 195 ▶ if (defined(my $timer_error = $self->_arm_acquire_timer($wait ||= {}, $waiter))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $timer_error = $self->_arm_acquire_timer($wait ||= {}, $waiter))) 1 194 195
Condition100%2/2
$wait ||= {}
l!l
18510
MC/DC100%1/1
$wait ||= {}
$wait
Truth table100%2/2
A
A= $wait
Aresult
11
00
465 1 return $self->future_class->fail($timer_error);
466 }
467  
468 194 push @{ $self->{_waiters} }, $waiter;
469 194 ▶ push @{ $self->{_collecting_queued} }, $waiter if $self->{_collecting_queued};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_collecting_queued"} 7 187 194
470 194 $self->_withdraw_on_cancel($waiter);
471 194 return $waiter;
472 }
473  
474 # The failure a pool hands a caller: a DBIO::Exception, as every error of the
475 # core is.
476 12 ▶sub _exception {
Subroutine _exception: called CC=1 SCAR=0.0
477 12 my ($message) = @_;
478 12 local $@;
479 0 eval { DBIO::Exception->throw($message); 1 };
480 12 return $@;
481 }
482  
483 # Arm the timer of the caller's acquire_timeout on $target, the Future the
484 # caller waits on right now -- the queued waiter, or the caller's Future of a
485 # connection coming up. $wait is shared by the stages of one acquire: the
486 # deadline is set at the first stage, and a later stage gets what is left of
487 # it. The timer is stopped when $target is decided, however it is. Returns
488 # undef, or the error of a timer that could not be made (karr core k397).
489 297 ▶sub _arm_acquire_timer {
Subroutine _arm_acquire_timer: called CC=13 SCAR=28.5
490 297 my ($self, $wait, $target) = @_;
491  
492 297 ▶ my $timeout = $self->{acquire_timeout} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $timeout = $self->{"acquire_timeout"} or return undef 296 1 297
493 296 ▶ return undef unless $self->_has_timer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_timer 287 9 296
494  
495 9 my $now = Time::HiRes::time;
496 9 ▶ my $deadline = $wait->{deadline} //= $now + $timeout;
Condition100%2/2
$wait->{"deadline"} //= $now + $timeout
l!l
18
MC/DC100%1/1
$wait->{"deadline"} //= $now + $timeout
$wait->{"deadline"}
Truth table100%2/2
A
A= $wait->{"deadline"}
Aresult
11
00
497 9 my $left = $deadline - $now;
498 9 ▶ $left = 0 if $left < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left < 0 0 9 9
499  
500 9 my $timer;
501 {
502 9 local $@;
503 9 my $ok = eval { $timer = $self->_delay($left); 1 };
504 9 ▶ return $@ || '_delay died' unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 8 9
Condition50%1/2
$@ || '_delay died'
l!l
10
MC/DC0%0/1
$@ || '_delay died'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
505 }
506 8 ▶ return _exception(
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready") 0 8 8
!defined($timer) ? : 0 0 0
Condition33%2/6
builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready")
!ll&&!rl&&r
008
builtin::blessed($timer) and $timer->can("then")
!ll&&!rl&&r
008
MC/DC0%0/3
builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready")
builtin::blessed($timer) $timer->can("then") $timer->can("is_ready")
Truth table25%1/4
A and B and C
A= builtin::blessed($timer)B= $timer->can("then")C= $timer->can("is_ready")
ABCresult
0XX0
10X0
1100
1111
507 ref($self) . '->_delay returned '
508 . ( !defined $timer ? 'undef, not a Future' : 'no Future' )
509 ) unless Scalar::Util::blessed($timer) && $timer->can('then') && $timer->can('is_ready');
510  
511 8 my $pool = $self;
512 8 Scalar::Util::weaken($pool);
513 my $chain = $timer->then(
514 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
515 4 ▶ $pool->_acquire_timed_out($target, $timeout) if $pool;
Branch: 1 branch
BranchTrueFalseTotal
if $pool 4 0 4
516 4 return;
517 },
518 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
519 0 ▶ _report_hand_over_death($pool, $_[0], 'the acquire timeout timer failed') if $pool;
Branch: 1 branch
BranchTrueFalseTotal
if $pool 0 0 0
520 0 return;
521 },
522 8 );
523 8 ▶ $chain->retain if $chain->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $chain->can("retain") 0 8 8
524  
525 8 ▶ my $stop = sub { $timer->cancel if !$timer->is_ready && $timer->can('cancel'); return };
Subroutine __ANON__: called CC=3 SCAR=11.4
Branch: 1 branch
BranchTrueFalseTotal
if not $timer->is_ready and $timer->can("cancel") 0 6 6
Condition66%2/3
not $timer->is_ready and $timer->can("cancel")
!ll&&!rl&&r
330
MC/DC0%0/2
not $timer->is_ready and $timer->can("cancel")
not $timer->is_ready $timer->can("cancel")
Truth table66%2/3
not(A) and B
A= not $timer->is_readyB= $timer->can("cancel")
ABresult
0X0
100
111
526 0 ▶ if ($target->can('on_ready')) { $target->on_ready($stop) }
Branch: 1 branch
BranchTrueFalseTotal
if ($target->can('on_ready')) { } 0 8 8
527 8 else { $target->then($stop, $stop) }
528 8 return undef;
529 }
530  
531 # The caller's acquire_timeout ran out while $target was what it waited on. A
532 # queued waiter leaves the queue before it fails, so no release hands it a
533 # connection; the Future of a connection coming up is failed, and the
534 # connection goes where release sends one when it is up (karr core k397).
535 4 ▶sub _acquire_timed_out {
Subroutine _acquire_timed_out: called CC=3 SCAR=11.0
536 4 my ($self, $target, $timeout) = @_;
537 4 ▶ return if $target->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $target->is_ready 1 3 4
538  
539 3 $self->_withdraw_waiter($target);
540 3 ▶ eval { $target->fail(_exception("no pool connection within $timeout seconds")); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $target->fail(_exception("no pool connection within $timeout seconds")); 1 } } or _report_hand_over_death($self, $@, "a caller was told that no pool connection came within its acquire_timeout") 3 0 3
541 or _report_hand_over_death($self, $@,
542 'a caller was told that no pool connection came within its acquire_timeout');
543 3 return;
544 }
545  
546 # A caller that cancels its Future stops waiting: withdraw its waiter at once,
547 # so the queue holds only callers still waiting. Only a Future class that can
548 # be cancelled (Future.pm and its subclasses) is wired; DBIO::Test::Future is
549 # not. The hook holds the pool and the waiter weakly -- the pool holds the
550 # waiter, and the waiter holds the hook.
551 194 ▶sub _withdraw_on_cancel {
Subroutine _withdraw_on_cancel: called CC=2 SCAR=14.0
552 194 my ($self, $waiter) = @_;
553 194 ▶ return unless $waiter->can('on_cancel');
Branch: 1 branch
BranchTrueFalseTotal
unless $waiter->can("on_cancel") 194 0 194
554  
555 0 my ($pool, $weak_waiter) = ($self, $waiter);
556 0 Scalar::Util::weaken($pool);
557 0 Scalar::Util::weaken($weak_waiter);
558 0 ▶ $waiter->on_cancel(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
559 0 ▶ $pool->_withdraw_waiter($weak_waiter) if $pool && $weak_waiter;
Branch: 1 branch
BranchTrueFalseTotal
if $pool and $weak_waiter 0 0 0
Condition0%0/3
$pool and $weak_waiter
!ll&&!rl&&r
000
MC/DC0%0/2
$pool and $weak_waiter
$pool $weak_waiter
Truth table0%0/3
A and B
A= $poolB= $weak_waiter
ABresult
0X0
100
111
560 0 });
561 0 return;
562 }
563  
564  
565 195 ▶sub _new_waiter { $_[0]->future_class->new }
Subroutine _new_waiter: called CC=1 SCAR=0.0
566  
567  
568 14 ▶sub _withdraw_waiter {
Subroutine _withdraw_waiter: called CC=5 SCAR=16.2
569 14 my ($self, $waiter) = @_;
570 14 ▶ return 0 unless defined $waiter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $waiter 0 14 14
571  
572 14 my $addr = Scalar::Util::refaddr($waiter);
573 14 my $waiters = $self->{_waiters};
574 14 for my $i (0 .. $#$waiters) {
575 11 ▶ next unless defined $waiters->[$i]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $waiters->[$i] and builtin::refaddr($waiters->[$i]) == $addr 1 10 11
Condition66%2/3
defined $waiters->[$i] and builtin::refaddr($waiters->[$i]) == $addr
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $waiters->[$i] and builtin::refaddr($waiters->[$i]) == $addr
defined $waiters->[$i] builtin::refaddr($waiters->[$i]) == $addr
Truth table66%2/3
A and B
A= defined $waiters->[$i]B= builtin::refaddr($waiters->[$i]) == $addr
ABresult
0X0
100
111
576 && Scalar::Util::refaddr($waiters->[$i]) == $addr;
577 10 splice @$waiters, $i, 1;
578 10 return 1;
579 }
580 4 return 0;
581 }
582  
583  
584 16 ▶sub _collect_queued {
Subroutine _collect_queued: called CC=3 SCAR=11.0
585 16 my ($self, $code) = @_;
586 16 my $outer = $self->{_collecting_queued};
587 16 my ($result, @waiters);
588 {
589 16 local $self->{_collecting_queued} = [];
590 16 $result = $code->();
591 16 ▶ @waiters = grep { defined $_ && !$_->is_ready } @{ $self->{_collecting_queued} };
Condition33%1/3
defined $_ && !$_->is_ready
!ll&&!rl&&r
009
MC/DC0%0/2
defined $_ && !$_->is_ready
defined $_ !$_->is_ready
Truth table33%1/3
A && not(B)
A= defined $_B= !$_->is_ready
ABresult
0X0
100
111
592 }
593  
594 # A collection inside another one is part of it: what the inner call left
595 # waiting is waiting for the outer call too.
596 16 ▶ push @$outer, @waiters if $outer;
Branch: 1 branch
BranchTrueFalseTotal
if $outer 2 14 16
597 16 return ($result, @waiters);
598 }
599  
600  
601 9 ▶sub _note_queued_for {
Subroutine _note_queued_for: called CC=10 SCAR=31.1
602 9 my ($self, $future, @waiters) = @_;
603 9 ▶ return unless Scalar::Util::blessed($future) && @waiters;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($future) and @waiters 0 9 9
Condition33%1/3
builtin::blessed($future) and @waiters
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($future) and @waiters
builtin::blessed($future) @waiters
Truth table33%1/3
A and B
A= builtin::blessed($future)B= @waiters
ABresult
0X0
100
111
604  
605 9 ▶ my $noted = $self->{_queued_for} ||= {};
Condition100%2/2
$self->{"_queued_for"} ||= {}
l!l
36
MC/DC100%1/1
$self->{"_queued_for"} ||= {}
$self->{"_queued_for"}
Truth table100%2/2
A
A= $self->{"_queued_for"}
Aresult
11
00
606  
607 # Entries of Futures that are gone or decided are swept when the table has
608 # doubled since the last sweep, so noting stays cheap under a long queue.
609 9 ▶ if (keys %$noted > ($self->{_queued_for_swept} // 0) * 2 + 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$noted > ($self->{"_queued_for_swept"} // 0) * 2 + 16) 0 9 9
Condition50%1/2
$self->{"_queued_for_swept"} // 0
l!l
09
MC/DC0%0/1
$self->{"_queued_for_swept"} // 0
$self->{"_queued_for_swept"}
Truth table50%1/2
A
A= $self->{"_queued_for_swept"}
Aresult
11
00
610 0 for my $addr (keys %$noted) {
611 0 my $entry = $noted->{$addr};
612 0 ▶ delete $noted->{$addr} unless defined $entry->[0] && !$entry->[0]->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->[0] and not $entry->[0]->is_ready 0 0 0
Condition0%0/3
defined $entry->[0] and not $entry->[0]->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
defined $entry->[0] and not $entry->[0]->is_ready
defined $entry->[0] not $entry->[0]->is_ready
Truth table0%0/3
A and not(B)
A= defined $entry->[0]B= not $entry->[0]->is_ready
ABresult
0X0
100
111
613 }
614 0 $self->{_queued_for_swept} = keys %$noted;
615 }
616  
617 9 my $entry = [ $future, @waiters ];
618 9 Scalar::Util::weaken($_) for @$entry;
619 9 $noted->{ Scalar::Util::refaddr($future) } = $entry;
620 9 return;
621 }
622  
623  
624 403 ▶sub _queued_waiters_for {
Subroutine _queued_waiters_for: called CC=12 SCAR=25.7
625 403 my ($self, $future) = @_;
626  
627 403 my (%seen, @found);
628 403 my @todo = ($future);
629 403 ▶ while (defined(my $f = shift @todo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = shift @todo)) 406 403 809
630 406 ▶ next unless Scalar::Util::blessed($f);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) 0 406 406
631 406 ▶ next if $seen{ Scalar::Util::refaddr($f) }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{builtin::refaddr($f)}++ 0 406 406
632 406 ▶ next if $f->can('is_ready') && $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("is_ready") and $f->is_ready 370 36 406
Condition66%2/3
$f->can("is_ready") and $f->is_ready
!ll&&!rl&&r
036370
MC/DC50%1/2
$f->can("is_ready") and $f->is_ready
$f->can("is_ready") $f->is_ready
Truth table66%2/3
A and B
A= $f->can("is_ready")B= $f->is_ready
ABresult
0X0
100
111
633  
634 36 ▶ if (my $entry = $self->{_queued_for} && $self->{_queued_for}{ Scalar::Util::refaddr($f) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->{"_queued_for"} && $self->{"_queued_for"}{builtin::refaddr($f)}) 7 29 36
Condition100%3/3
$self->{'_queued_for'} && $self->{'_queued_for'}{builtin::refaddr($f)}
!ll&&!rl&&r
2817
MC/DC100%2/2
$self->{'_queued_for'} && $self->{'_queued_for'}{builtin::refaddr($f)}
$self->{'_queued_for'} $self->{'_queued_for'}{builtin::refaddr($f)}
Truth table100%3/3
A && B
A= $self->{'_queued_for'}B= $self->{'_queued_for'}{builtin::refaddr($f)}
ABresult
0X0
100
111
635 7 ▶ push @found, grep { defined $_ && !$_->is_ready } @$entry[ 1 .. $#$entry ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->[0] and builtin::refaddr($entry->[0]) == builtin::refaddr($f) 7 0 7
Condition33%2/6
defined $entry->[0] and builtin::refaddr($entry->[0]) == builtin::refaddr($f)
!ll&&!rl&&r
007
defined $_ && !$_->is_ready
!ll&&!rl&&r
007
MC/DC0%0/2
defined $entry->[0] and builtin::refaddr($entry->[0]) == builtin::refaddr($f)
defined $entry->[0] builtin::refaddr($entry->[0]) == builtin::refaddr($f)
MC/DC0%0/2
defined $_ && !$_->is_ready
defined $_ !$_->is_ready
Truth table33%1/3
A and B
A= defined $entry->[0]B= builtin::refaddr($entry->[0]) == builtin::refaddr($f)
ABresult
0X0
100
111
Truth table33%1/3
A && not(B)
A= defined $_B= !$_->is_ready
ABresult
0X0
100
111
636 if defined $entry->[0] && Scalar::Util::refaddr($entry->[0]) == Scalar::Util::refaddr($f);
637 }
638 36 ▶ if ($f->can('pending_futures')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->can("pending_futures")) 36 0 36
639 36 local $@;
640 36 push @todo, eval { $f->pending_futures };
641 }
642 }
643  
644 403 my %queued = map { Scalar::Util::refaddr($_) => 1 } grep { defined $_ } @{ $self->{_waiters} };
645 403 return grep { $queued{ Scalar::Util::refaddr($_) } } @found;
646 }
647  
648  
649 403 ▶sub _refuse_queued_for {
Subroutine _refuse_queued_for: called CC=5 SCAR=16.5
650 403 my ($self, $future, $message) = @_;
651  
652 403 my $refused = 0;
653 403 for my $waiter ($self->_queued_waiters_for($future)) {
654 7 ▶ next unless $self->_withdraw_waiter($waiter);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_withdraw_waiter($waiter) 0 7 7
655 7 $refused++;
656 7 ▶ eval { $waiter->fail(_exception($message)) unless $waiter->is_ready; 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $waiter->fail(_exception($message)) unless $waiter->is_ready; 1 } } or _report_hand_over_death($self, $@, "a queued caller was refused") 7 0 7
unless $waiter->is_ready 7 0 7
657 or _report_hand_over_death($self, $@, 'a queued caller was refused');
658 }
659 403 return $refused;
660 }
661  
662 # Ask _idle_connection_alive about an idle connection about to be handed out.
663 # One it does not vouch for is retired, never handed out; a throw counts as
664 # not vouching, and goes to on_error so a broken check is not silent (it
665 # would otherwise just turn every borrow into a spawn). An on_error that dies
666 # may not throw out of acquire, which never throws -- but its death is warned
667 # with the report it was given, not swallowed, as a dying on_listen_error hook
668 # is (karr core k252).
669 576 ▶sub _idle_connection_usable {
Subroutine _idle_connection_usable: called CC=6 SCAR=17.9
670 576 my ($self, $conn) = @_;
671  
672 576 my $alive;
673 576 ▶ unless (eval { $alive = $self->_idle_connection_alive($conn); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $alive = $self->_idle_connection_alive($conn); 1 } }) 3 573 576
674 3 ▶ my $err = $@ || 'unknown error';
Condition50%1/2
$@ || 'unknown error'
l!l
30
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
675 3 my $report = "Idle connection liveness check failed: $err";
676 3 ▶ unless (eval { $self->{on_error}->($report); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->{"on_error"}->($report); 1 } }) 1 2 3
677 1 ▶ chomp(my $died = $@ || 'unknown error');
Condition50%1/2
$@ || 'unknown error'
l!l
10
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
678 1 chomp $report;
679 1 warn "DBIO pool on_error died ($died) while reporting: $report\n";
680 }
681 3 $alive = 0;
682 }
683 576 ▶ return 1 if $alive;
Branch: 1 branch
BranchTrueFalseTotal
if $alive 566 10 576
684  
685 10 $self->_retire_connection($conn);
686 10 return 0;
687 }
688  
689  
690 558 ▶sub _idle_connection_alive { 1 }
Subroutine _idle_connection_alive: called CC=1 SCAR=0.0
691  
692  
693 1263 ▶sub _connection_ready_future {
Subroutine _connection_ready_future: called CC=5 SCAR=16.1
694 1263 my ($self, $conn) = @_;
695  
696 1263 ▶ return $self->future_class->done($conn)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($conn) and $conn->can("is_connected") and $conn->is_connected 3 1260 1263
Condition100%6/6
builtin::blessed($conn) and $conn->can("is_connected") and $conn->is_connected
!ll&&!rl&&r
125463
builtin::blessed($conn) and $conn->can("is_connected")
!ll&&!rl&&r
1012449
MC/DC100%3/3
builtin::blessed($conn) and $conn->can("is_connected") and $conn->is_connected
builtin::blessed($conn) $conn->can("is_connected") $conn->is_connected
Truth table100%4/4
A and B and C
A= builtin::blessed($conn)B= $conn->can("is_connected")C= $conn->is_connected
ABCresult
0XX0
10X0
1100
1111
697 if Scalar::Util::blessed($conn)
698 && $conn->can('is_connected')
699 && $conn->is_connected;
700  
701 1260 ▶ return $self->_connection_ready_lookup($conn)
Condition66%2/3
$self->_connection_ready_lookup($conn) || $self->future_class->done($conn)
l!l&&r!l&&!r
7811820
MC/DC0%0/2
$self->_connection_ready_lookup($conn) || $self->future_class->done($conn)
$self->_connection_ready_lookup($conn) $self->future_class->done($conn)
Truth table66%2/3
A || B
A= $self->_connection_ready_lookup($conn)B= $self->future_class->done($conn)
ABresult
1X1
011
000
702 || $self->future_class->done($conn);
703 }
704  
705  
706 57 ▶sub _create_ready_connection {
Subroutine _create_ready_connection: called CC=2 SCAR=7.0
707 57 my ($self, @args) = @_;
708  
709 57 my $ready = $self->future_class->new;
710 57 my $on_error = $self->{on_error};
711  
712 # A connection error the transport raises later comes through on_error and
713 # the readiness Future, not through the spawn's die: it is masked here with
714 # the connect info this connection was made from (karr k400).
715 57 ▶ my @conninfo = @{ $self->{_spawn_conninfo} || [ $self->{conninfo} ] };
Condition100%2/2
$self->{'_spawn_conninfo'} || [$self->{'conninfo'}]
l!l
561
MC/DC100%1/1
$self->{'_spawn_conninfo'} || [$self->{'conninfo'}]
$self->{'_spawn_conninfo'}
Truth table100%2/2
A
A= $self->{'_spawn_conninfo'}
Aresult
11
00
716  
717 # The transport keeps these callbacks for as long as the connection lives, so
718 # they hold the connection and its readiness Future weakly (karr core k235).
719 # Held strongly, each closes a cycle -- connection -> on_connect ->
720 # connection, and once on_connect has resolved the Future with the
721 # connection, connection -> on_error -> Future -> connection -- and no
722 # connection built here is ever freed, retired or not. The pool's holds are
723 # the strong ones: _connections for the connection, the readiness table for
724 # the Future.
725 #
726 # The pool is held weakly too, for the one thing the callbacks tell it: a
727 # callback somebody hung on the readiness Future that dies as it is settled.
728 # Thrown from here it reached the transport, and in on_error cut the pool's
729 # on_error off from the transport's error (karr core k261, left open by
730 # k279). The Future is held for the length of the call: settling it can
731 # take the last other hold on it away.
732 57 my ($weak_conn, $weak_ready);
733 57 Scalar::Util::weaken($weak_ready = $ready);
734 57 Scalar::Util::weaken(my $pool = $self);
735 my $conn = $self->_new_connection(
736 @args,
737 34 ▶ on_connect => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
738 34 my $settling = $weak_ready;
739 34 ▶ eval { $settling->done($weak_conn) if $settling && !$settling->is_ready; 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $settling->done($weak_conn) if $settling and not $settling->is_ready; 1 } } or _report_hand_over_death($pool, $@, "the readiness of a connection that came up was settled") 34 0 34
if $settling and not $settling->is_ready 31 3 34
Condition66%2/3
$settling and not $settling->is_ready
!ll&&!rl&&r
3031
MC/DC50%1/2
$settling and not $settling->is_ready
$settling not $settling->is_ready
Truth table66%2/3
A and not(B)
A= $settlingB= not $settling->is_ready
ABresult
0X0
100
111
740 or _report_hand_over_death($pool, $@,
741 'the readiness of a connection that came up was settled');
742 },
743 16 ▶ on_error => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
744 16 my ($error, @rest) = @_;
745 16 ▶ $error = $pool->_mask_connect_error($error, @conninfo) if $pool;
Branch: 1 branch
BranchTrueFalseTotal
if $pool 16 0 16
746 16 my $settling = $weak_ready;
747 16 ▶ eval { $settling->fail($error) if $settling && !$settling->is_ready; 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $settling->fail($error) if $settling and not $settling->is_ready; 1 } } or _report_hand_over_death($pool, $@, "the readiness of a connection that failed to come up was settled") 16 0 16
if $settling and not $settling->is_ready 13 3 16
Condition100%3/3
$settling and not $settling->is_ready
!ll&&!rl&&r
2113
MC/DC100%2/2
$settling and not $settling->is_ready
$settling not $settling->is_ready
Truth table100%3/3
A and not(B)
A= $settlingB= not $settling->is_ready
ABresult
0X0
100
111
748 or _report_hand_over_death($pool, $@,
749 'the readiness of a connection that failed to come up was settled');
750 16 $on_error->($error, @rest);
751 },
752 57 );
753 53 Scalar::Util::weaken($weak_conn = $conn);
754 53 $self->_register_connection_ready($conn, $ready);
755 53 return $conn;
756 }
757  
758  
759 1 ▶sub _new_connection {
Subroutine _new_connection: called CC=1 SCAR=0.0
760 1 croak 'Subclass must override _new_connection to use _create_ready_connection';
761 }
762  
763  
764 84 ▶sub _register_connection_ready {
Subroutine _register_connection_ready: called CC=1 SCAR=0.0
765 84 my ($self, $conn, $ready) = @_;
766 84 $self->{_ready}{ Scalar::Util::refaddr($conn) } = $ready;
767 84 return $ready;
768 }
769  
770  
771 1731 ▶sub _connection_ready_lookup {
Subroutine _connection_ready_lookup: called CC=3 SCAR=11.0
772 1731 my ($self, $conn) = @_;
773 1731 ▶ return unless $self->{_ready} && defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_ready"} and defined $conn 1593 138 1731
Condition66%2/3
$self->{"_ready"} and defined $conn
!ll&&!rl&&r
15930138
MC/DC50%1/2
$self->{"_ready"} and defined $conn
$self->{"_ready"} defined $conn
Truth table66%2/3
A and B
A= $self->{"_ready"}B= defined $conn
ABresult
0X0
100
111
774 138 return $self->{_ready}{ Scalar::Util::refaddr($conn) };
775 }
776  
777  
778 587 ▶sub _clear_connection_ready {
Subroutine _clear_connection_ready: called CC=3 SCAR=11.0
779 587 my ($self, $conn) = @_;
780 587 ▶ return unless $self->{_ready} && defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_ready"} and defined $conn 521 66 587
Condition66%2/3
$self->{"_ready"} and defined $conn
!ll&&!rl&&r
521066
MC/DC50%1/2
$self->{"_ready"} and defined $conn
$self->{"_ready"} defined $conn
Truth table66%2/3
A and B
A= $self->{"_ready"}B= defined $conn
ABresult
0X0
100
111
781 66 delete $self->{_ready}{ Scalar::Util::refaddr($conn) };
782 }
783  
784  
785 410 ▶sub acquire_txn {
Subroutine acquire_txn: called CC=1 SCAR=0.0
Pod documented
786 410 my $self = shift;
787 410 return $self->acquire; # same behavior, caller manages lifecycle
788 }
789  
790  
791 888 ▶sub release {
Subroutine release: called CC=10 SCAR=23.1
Pod documented
792 888 my ($self, $conn, $lease) = @_;
793 888 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 18 870 888
794 870 $self->_verify_pid;
795 870 ▶ return unless $self->_holds_connection($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_holds_connection($conn) 3 867 870
796  
797 # Released already and still idle: taken a second time, it would be on the
798 # idle list twice and go to two callers at once (karr core k270).
799 867 ▶ return if $self->_connection_is_idle($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connection_is_idle($conn) 4 863 867
800  
801 # Being reset: handed back already, and the reset decides where it goes.
802 863 ▶ return if $self->_connection_is_resetting($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connection_is_resetting($conn) 0 863 863
803  
804 # A lease that is not the connection's current one belongs to an earlier
805 # borrow: the connection went on since, and this is not its holder's release
806 # (karr core k275). Silent, like every other release that is not taken.
807 863 ▶ if (defined $lease) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lease) 82 781 863
808 82 my $current = $self->{_leases}{ Scalar::Util::refaddr($conn) };
809 82 ▶ return unless defined $current && $current eq $lease;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current and $current eq $lease 4 78 82
Condition66%2/3
defined $current and $current eq $lease
!ll&&!rl&&r
0478
MC/DC50%1/2
defined $current and $current eq $lease
defined $current $current eq $lease
Truth table66%2/3
A and B
A= defined $currentB= $current eq $lease
ABresult
0X0
100
111
810 }
811  
812 859 ▶ return $self->_retire_in_use_connection($conn)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connection_is_dead($conn) 10 849 859
813 if $self->_connection_is_dead($conn);
814  
815 # This borrow is over; whoever has the connection next asks for a lease of
816 # its own.
817 849 delete $self->{_leases}{ Scalar::Util::refaddr($conn) };
818  
819 # Never handed out, so nothing to reset (_release_unused).
820 849 ▶ return $self->_hand_on($conn) if delete $self->{_unused}{ Scalar::Util::refaddr($conn) };
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"_unused"}{builtin::refaddr($conn)} 4 845 849
821  
822 845 return $self->_reset_then_hand_on($conn);
823 }
824  
825 # A connection that was never handed out -- one that came up for a caller who
826 # gave up meanwhile -- carries no session state: it goes where release sends a
827 # connection, without the reset.
828 4 ▶sub _release_unused {
Subroutine _release_unused: called CC=3 SCAR=11.1
829 4 my ($self, $conn) = @_;
830 4 ▶ return unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 4 4
831 4 my $addr = Scalar::Util::refaddr($conn);
832 4 $self->{_unused}{$addr} = 1;
833 4 my $ok = eval { $self->release($conn); 1 };
834 4 my $error = $@;
835 4 delete $self->{_unused}{$addr};
836 4 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
837 4 return;
838 }
839  
840 # The connection goes to the oldest queued caller still waiting, or to the idle
841 # list.
842 849 ▶sub _hand_on {
Subroutine _hand_on: called CC=3 SCAR=11.0
843 849 my ($self, $conn) = @_;
844  
845 849 ▶ if (my $waiter = $self->_next_waiter) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $waiter = $self->_next_waiter) 60 789 849
846 # The waiter's callbacks run here, and they are another caller's. One that
847 # dies does not reach whoever released (karr core k279). The connection is
848 # the waiter's either way: its Future was settled before its callbacks ran.
849 60 ▶ eval { $waiter->done($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $waiter->done($conn); 1 } } or _report_hand_over_death($self, $@, "a released connection was handed to a queued caller") 60 0 60
850 or _report_hand_over_death($self, $@,
851 'a released connection was handed to a queued caller');
852 60 return;
853 }
854  
855 789 push @{ $self->{_idle} }, $conn;
856 789 return;
857 }
858  
859 # The session of a released connection is reset before anyone else gets it
860 # (karr core k398). The owning storage's _reset_pool_connection answers a Future;
861 # until it is done the connection is `resetting`: it counts against max_size, is
862 # neither idle nor leased, and a release of it is a no-op. A pool without an
863 # owning storage that has the seam is not a session pool and hands on as before.
864 845 ▶sub _reset_then_hand_on {
Subroutine _reset_then_hand_on: called CC=17 SCAR=41.9
865 845 my ($self, $conn) = @_;
866  
867 845 my $storage = $self->{storage};
868 845 ▶ return $self->_hand_on($conn)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($storage) and $storage->can("_reset_pool_connection") 832 13 845
Condition100%3/3
builtin::blessed($storage) and $storage->can("_reset_pool_connection")
!ll&&!rl&&r
8161613
MC/DC100%2/2
builtin::blessed($storage) and $storage->can("_reset_pool_connection")
builtin::blessed($storage) $storage->can("_reset_pool_connection")
Truth table100%3/3
A and B
A= builtin::blessed($storage)B= $storage->can("_reset_pool_connection")
ABresult
0X0
100
111
869 unless Scalar::Util::blessed($storage) && $storage->can('_reset_pool_connection');
870  
871 13 my $state = { conn => $conn };
872 13 $self->{_resetting}{ Scalar::Util::refaddr($conn) } = $state;
873  
874 13 my $future;
875 13 my $ok = eval { $future = $storage->_reset_pool_connection($conn); 1 };
876 13 ▶ unless ($ok && Scalar::Util::blessed($future) && $future->can('then') && $future->can('is_ready')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and builtin::blessed($future) and $future->can("then") and $future->can("is_ready")) 0 13 13
Condition33%3/9
$ok and builtin::blessed($future) and $future->can("then") and $future->can("is_ready")
!ll&&!rl&&r
0013
$ok and builtin::blessed($future) and $future->can("then")
!ll&&!rl&&r
0013
$ok and builtin::blessed($future)
!ll&&!rl&&r
0013
MC/DC0%0/4
$ok and builtin::blessed($future) and $future->can("then") and $future->can("is_ready")
$ok builtin::blessed($future) $future->can("then") $future->can("is_ready")
Truth table20%1/5
A and B and C and D
A= $okB= builtin::blessed($future)C= $future->can("then")D= $future->can("is_ready")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
877 0 $state->{void} = 1;
878 0 delete $self->{_resetting}{ Scalar::Util::refaddr($conn) };
879 0 return $self->_retire_in_use_connection($conn);
880 }
881 13 $state->{future} = $future;
882  
883 13 my $pool = $self;
884 13 Scalar::Util::weaken($pool);
885  
886 # Bounded: a reset that never answers retires the connection.
887 13 my $timeout = $self->{reset_timeout};
888 13 ▶ if ($timeout && $self->_has_timer && !$future->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and $self->_has_timer and not $future->is_ready) 0 13 13
Condition33%2/6
$timeout and $self->_has_timer and not $future->is_ready
!ll&&!rl&&r
0130
$timeout and $self->_has_timer
!ll&&!rl&&r
0013
MC/DC0%0/3
$timeout and $self->_has_timer and not $future->is_ready
$timeout $self->_has_timer not $future->is_ready
Truth table25%1/4
A and B and not(C)
A= $timeoutB= $self->_has_timerC= not $future->is_ready
ABCresult
0XX0
10X0
1100
1111
889 0 my $timer;
890 0 ▶ if (eval { $timer = $self->_delay($timeout); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $timer = $self->_delay($timeout); 1 } }) { } 0 0 0
891 0 ▶ if (Scalar::Util::blessed($timer) && $timer->can('then') && $timer->can('is_ready')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready")) 0 0 0
Condition0%0/6
builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready")
!ll&&!rl&&r
000
builtin::blessed($timer) and $timer->can("then")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($timer) and $timer->can("then") and $timer->can("is_ready")
builtin::blessed($timer) $timer->can("then") $timer->can("is_ready")
Truth table0%0/4
A and B and C
A= builtin::blessed($timer)B= $timer->can("then")C= $timer->can("is_ready")
ABCresult
0XX0
10X0
1100
1111
892 0 $state->{timer} = $timer;
893 0 ▶ my $chain = $timer->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
894 0 ▶ _reset_ended($pool, $state, 'timeout') if $pool;
Branch: 1 branch
BranchTrueFalseTotal
if $pool 0 0 0
895 0 return;
896 0 ▶ }, sub { return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
897 0 ▶ $chain->retain if $chain->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $chain->can("retain") 0 0 0
898 }
899 }
900 else {
901 0 ▶ _report_hand_over_death($self, $@ || '_delay died',
Condition0%0/2
$@ || '_delay died'
l!l
00
MC/DC0%0/1
$@ || '_delay died'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
902 'the reset timeout could not be armed');
903 }
904 }
905  
906 13 ▶ my $decided = sub {
Subroutine __ANON__: called CC=6 SCAR=21.2
907 13 my ($f) = @_;
908 13 ▶ my $done = $f->is_failed ? 0
Branch: 2 branches
BranchTrueFalseTotal
$f->is_failed ? : 0 13 13
$f->can('is_cancelled') && $f->is_cancelled ? : 0 13 13
Condition33%1/3
$f->can("is_cancelled") and $f->is_cancelled
!ll&&!rl&&r
1300
MC/DC0%0/2
$f->can("is_cancelled") and $f->is_cancelled
$f->can("is_cancelled") $f->is_cancelled
Truth table33%1/3
A and B
A= $f->can("is_cancelled")B= $f->is_cancelled
ABresult
0X0
100
111
909 : ($f->can('is_cancelled') && $f->is_cancelled) ? 0
910 : 1;
911 13 ▶ _reset_ended($pool, $state, $done ? 'done' : 'failed') if $pool;
Branch: 2 branches
BranchTrueFalseTotal
if $pool 13 0 13
$done ? : 13 0 13
912 13 return;
913 13 };
914 0 ▶ if ($future->can('on_ready')) { $future->on_ready($decided) }
Branch: 1 branch
BranchTrueFalseTotal
if ($future->can('on_ready')) { } 0 13 13
915 0 ▶ else { $future->then(sub { $decided->($future) }, sub { $decided->($future) }) }
Subroutine __ANON__: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
916 13 return;
917 }
918  
919 # The end of a reset: its Future decided, or its timer ran out. A reset that was
920 # voided meanwhile -- shutdown, a fork, a retirement -- is over already and
921 # changes nothing.
922 13 ▶sub _reset_ended {
Subroutine _reset_ended: called CC=5 SCAR=16.7
923 13 my ($pool, $state, $how) = @_;
924 13 ▶ return if $state->{void}++;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"void"}++ 0 13 13
925 13 my $conn = $state->{conn};
926 13 delete $pool->{_resetting}{ Scalar::Util::refaddr($conn) };
927 13 _stop_reset($state, $how ne 'timeout', $how eq 'timeout');
928  
929 13 ▶ return if $pool->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $pool->{"_shutdown"} 0 13 13
930 13 ▶ return $pool->_retire_in_use_connection($conn) unless $how eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
unless $how eq "done" 0 13 13
931 13 ▶ return $pool->_retire_in_use_connection($conn) if $pool->_connection_is_dead($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $pool->_connection_is_dead($conn) 0 13 13
932 13 return $pool->_hand_on($conn);
933 }
934  
935 # Stop the timer and, when the reset is cut short, the reset itself.
936 13 ▶sub _stop_reset {
Subroutine _stop_reset: called CC=9 SCAR=27.6
937 13 my ($state, $stop_timer, $stop_future) = @_;
938 13 my $timer = delete $state->{timer};
939 13 ▶ eval { $timer->cancel if $stop_timer && $timer && !$timer->is_ready && $timer->can('cancel'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $stop_timer and $timer and not $timer->is_ready and $timer->can("cancel") 0 13 13
Condition33%3/9
$stop_timer and $timer and not $timer->is_ready and $timer->can("cancel")
!ll&&!rl&&r
1300
$stop_timer and $timer and not $timer->is_ready
!ll&&!rl&&r
1300
$stop_timer and $timer
!ll&&!rl&&r
0130
MC/DC0%0/4
$stop_timer and $timer and not $timer->is_ready and $timer->can("cancel")
$stop_timer $timer not $timer->is_ready $timer->can("cancel")
Truth table20%1/5
A and B and not(C) and D
A= $stop_timerB= $timerC= not $timer->is_readyD= $timer->can("cancel")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
940 13 my $future = delete $state->{future};
941 13 ▶ eval { $future->cancel if $stop_future && $future && !$future->is_ready && $future->can('cancel'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $stop_future and $future and not $future->is_ready and $future->can("cancel") 0 13 13
Condition33%3/9
$stop_future and $future and not $future->is_ready and $future->can("cancel")
!ll&&!rl&&r
1300
$stop_future and $future and not $future->is_ready
!ll&&!rl&&r
1300
$stop_future and $future
!ll&&!rl&&r
1300
MC/DC0%0/4
$stop_future and $future and not $future->is_ready and $future->can("cancel")
$stop_future $future not $future->is_ready $future->can("cancel")
Truth table20%1/5
A and B and not(C) and D
A= $stop_futureB= $futureC= not $future->is_readyD= $future->can("cancel")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
942 13 return;
943 }
944  
945 # Voids the reset of $conn, if one runs: its callbacks change nothing from now
946 # on. The connection is being retired, shut down or dropped by the caller.
947 584 ▶sub _void_reset {
Subroutine _void_reset: called CC=4 SCAR=23.7
948 584 my ($self, $conn, $cancel) = @_;
949 584 ▶ my $state = delete $self->{_resetting}{ Scalar::Util::refaddr($conn) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = delete $self->{"_resetting"}{builtin::refaddr($conn)} or return 0 584 584
950 0 ▶ return if $state->{void}++;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"void"}++ 0 0 0
951 0 ▶ _stop_reset($state, 1, $cancel && !in_global_destruction);
Condition0%0/3
$cancel && !&in_global_destruction()
!ll&&!rl&&r
000
MC/DC0%0/2
$cancel && !&in_global_destruction()
$cancel !&in_global_destruction()
Truth table0%0/3
A && not(B)
A= $cancelB= !&in_global_destruction()
ABresult
0X0
100
111
952 0 return;
953 }
954  
955 # Every reset still running is voided; the owner's are cancelled, an
956 # inherited one belongs to the parent and is only forgotten.
957 570 ▶sub _drop_resets {
Subroutine _drop_resets: called CC=5 SCAR=20.2
958 570 my ($self, $cancel) = @_;
959 570 ▶ my @states = values %{ $self->{_resetting} || {} };
Condition100%2/2
$self->{'_resetting'} || {}
l!l
5691
MC/DC100%1/1
$self->{'_resetting'} || {}
$self->{'_resetting'}
Truth table100%2/2
A
A= $self->{'_resetting'}
Aresult
11
00
960 570 $self->{_resetting} = {};
961 570 for my $state (@states) {
962 0 ▶ next if $state->{void}++;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"void"}++ 0 0 0
963 0 ▶ _stop_reset($state, $cancel, $cancel && !in_global_destruction);
Condition0%0/3
$cancel && !&in_global_destruction()
!ll&&!rl&&r
000
MC/DC0%0/2
$cancel && !&in_global_destruction()
$cancel !&in_global_destruction()
Truth table0%0/3
A && not(B)
A= $cancelB= !&in_global_destruction()
ABresult
0X0
100
111
964 }
965 570 return;
966 }
967  
968 1303 ▶sub _connection_is_resetting {
Subroutine _connection_is_resetting: called CC=4 SCAR=17.9
969 1303 my ($self, $conn) = @_;
970 return defined $conn && $self->{_resetting}
971 1303 ▶ && $self->{_resetting}{ Scalar::Util::refaddr($conn) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $conn && $self->{'_resetting'} && $self->{'_resetting'}{builtin::refaddr($conn)} ? : 0 1303 1303
Condition33%2/6
defined $conn and $self->{"_resetting"} and $self->{"_resetting"}{builtin::refaddr($conn)}
!ll&&!rl&&r
013030
defined $conn and $self->{"_resetting"}
!ll&&!rl&&r
001303
MC/DC0%0/3
defined $conn and $self->{"_resetting"} and $self->{"_resetting"}{builtin::refaddr($conn)}
defined $conn $self->{"_resetting"} $self->{"_resetting"}{builtin::refaddr($conn)}
Truth table25%1/4
A and B and C
A= defined $connB= $self->{"_resetting"}C= $self->{"_resetting"}{builtin::refaddr($conn)}
ABCresult
0XX0
10X0
1100
1111
972 }
973  
974  
975 750 ▶sub _lease {
Subroutine _lease: called CC=4 SCAR=13.9
976 750 my ($self, $conn) = @_;
977 750 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 0 750 750
978 750 $self->_verify_pid;
979 750 ▶ return unless $self->_holds_connection($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_holds_connection($conn) 1 749 750
980 749 ▶ return $self->{_leases}{ Scalar::Util::refaddr($conn) } //= ++$self->{_lease_seq};
Condition100%2/2
$self->{"_leases"}{builtin::refaddr($conn)} //= ++$self->{"_lease_seq"}
l!l
3746
MC/DC100%1/1
$self->{"_leases"}{builtin::refaddr($conn)} //= ++$self->{"_lease_seq"}
$self->{"_leases"}{builtin::refaddr($conn)}
Truth table100%2/2
A
A= $self->{"_leases"}{builtin::refaddr($conn)}
Aresult
11
00
981 }
982  
983 # Take the oldest queued caller that is still waiting off the queue, or return
984 # nothing. A waiter that is already ready was given up on -- cancelled (Future.pm
985 # carries a cancel down the ->then chain to it) or settled from outside -- and
986 # is dropped on the way: ->done on a cancelled Future.pm does nothing, so a
987 # connection handed to it would be lost, and on a settled one it dies (karr
988 # core k255).
989 1042 ▶sub _next_waiter {
Subroutine _next_waiter: called CC=4 SCAR=13.9
990 1042 my ($self) = @_;
991 1042 ▶ while (@{ $self->{_waiters} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_waiters"};}) 121 927 1048
992 121 my $waiter = shift @{ $self->{_waiters} };
993 121 ▶ return $waiter if defined $waiter && !$waiter->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if defined $waiter and not $waiter->is_ready 115 6 121
Condition66%2/3
defined $waiter and not $waiter->is_ready
!ll&&!rl&&r
06115
MC/DC50%1/2
defined $waiter and not $waiter->is_ready
defined $waiter not $waiter->is_ready
Truth table66%2/3
A and not(B)
A= defined $waiterB= not $waiter->is_ready
ABresult
0X0
100
111
994 }
995 927 return;
996 }
997  
998  
999 173 ▶sub size { scalar @{ $_[0]->{_connections} } }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
1000  
1001  
1002 145 ▶sub available { scalar @{ $_[0]->{_idle} } }
Subroutine available: called CC=1 SCAR=0.0
Pod documented
1003  
1004  
1005 18 ▶sub max_size { $_[0]->{max_size} }
Subroutine max_size: called CC=1 SCAR=0.0
Pod documented
1006  
1007  
1008 1172 ▶sub is_shutdown { return $_[0]->{_shutdown} ? 1 : 0 }
Subroutine is_shutdown: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_shutdown'} ? : 2 1170 1172
1009  
1010  
1011 928 ▶sub shutdown {
Subroutine shutdown: called CC=27 SCAR=34.4
Pod documented
1012 928 my $self = shift;
1013 928 ▶ return 1 if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 373 555 928
1014 555 $self->_verify_pid;
1015 555 $self->{_shutdown} = 1;
1016  
1017 # In global destruction nothing is settled (karr core k254): a waiter freed
1018 # ahead of the pool is undef here, and failing one whose caller's derived
1019 # Future was freed first makes Future.pm carp "lost a sequence Future".
1020 555 my $settle = !in_global_destruction;
1021  
1022 555 my @waiters = @{ $self->{_waiters} };
1023 555 $self->{_waiters} = [];
1024 # A callback that dies as its Future is failed -- here, and at every Future
1025 # failed below -- may not end the shutdown: everyone behind it would stay
1026 # unsettled and every connection open, in a pool that takes no second
1027 # shutdown (karr core k279).
1028 555 ▶ if ($settle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settle) 555 0 555
1029 555 for my $waiter (@waiters) {
1030 18 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $waiter->fail("Connection pool has been shut down") unless $waiter->is_ready; 1 } } or _report_hand_over_death($self, $@, "the shutdown failed a queued caller") 18 0 18
1031 18 ▶ $waiter->fail('Connection pool has been shut down')
Branch: 1 branch
BranchTrueFalseTotal
unless $waiter->is_ready 18 0 18
1032 unless $waiter->is_ready;
1033 18 1;
1034 } or _report_hand_over_death($self, $@, 'the shutdown failed a queued caller');
1035 }
1036 }
1037  
1038 # Fork safety (classic DBI InactiveDestroy): the physical connections belong to
1039 # the process that opened them. A forked child that inherited the pool must NOT
1040 # disconnect the parent's live connections, nor settle the parent's queued
1041 # waiters -- during the child's own teardown (typically global destruction)
1042 # _verify_pid above has already dropped both unsettled, without any physical
1043 # close, and made the child the owner of the emptied pool (karr core k234).
1044 # The child's copies of the sockets are reclaimed by the OS when it
1045 # exits. _teardown_belongs_here() is false when the owner PID was reclaimed
1046 # before us during global destruction, which fails safe the same way.
1047 555 ▶ if ($self->_teardown_belongs_here) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_teardown_belongs_here) 554 1 555
1048 554 my @connections = @{ $self->{_connections} };
1049 554 for my $conn (@connections) {
1050 # Global destruction can reclaim a connection's SV before the pool's own,
1051 # so a slot may already be undef by the time DESTROY reaches shutdown --
1052 # skip it rather than run it through refaddr (which warns on undef).
1053 460 ▶ next unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 4 456 460
1054  
1055 456 my $ready = $self->_connection_ready_lookup($conn);
1056 456 ▶ if ($ready && !$ready->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ready and not $ready->is_ready) 8 448 456
Condition100%3/3
$ready and not $ready->is_ready
!ll&&!rl&&r
423258
MC/DC100%2/2
$ready and not $ready->is_ready
$ready not $ready->is_ready
Truth table100%3/3
A and not(B)
A= $readyB= not $ready->is_ready
ABresult
0X0
100
111
1057 8 ▶ if ($settle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settle) 8 0 8
1058 8 ▶ eval { $ready->fail('Connection pool has been shut down'); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ready->fail("Connection pool has been shut down"); 1 } } or _report_hand_over_death($self, $@, "the shutdown failed the readiness of a connection still coming up") 8 0 8
1059 or _report_hand_over_death($self, $@,
1060 'the shutdown failed the readiness of a connection still coming up');
1061 }
1062 8 $self->_retire_connection($conn);
1063 8 next;
1064 }
1065  
1066 # Cold as well: a connection an acquire still waits on, behind a
1067 # readiness Future the transport's own seam made. The readiness table
1068 # does not know that Future; the hand-over held for the acquire knows
1069 # the connection (karr core k262).
1070 448 ▶ if ($self->_hand_over_pending($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_hand_over_pending($conn)) 8 440 448
1071 8 $self->_retire_connection($conn);
1072 8 next;
1073 }
1074  
1075 # Being reset: its session is mid-reset, so no disconnect actions go out
1076 # beside it. The reset is voided and the connection closed (karr core k398).
1077 440 ▶ if ($self->_connection_is_resetting($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_connection_is_resetting($conn)) 0 440 440
1078 0 $self->_retire_connection($conn);
1079 0 next;
1080 }
1081  
1082 440 my $addr = Scalar::Util::refaddr($conn);
1083 next unless grep {
1084 468 ▶ defined($_) && Scalar::Util::refaddr($_) == $addr
Condition100%3/3
defined $_ && builtin::refaddr($_) == $addr
!ll&&!rl&&r
424440
MC/DC100%2/2
defined $_ && builtin::refaddr($_) == $addr
defined $_ builtin::refaddr($_) == $addr
Truth table100%3/3
A && B
A= defined $_B= builtin::refaddr($_) == $addr
ABresult
0X0
100
111
1085 440 ▶ } @{ $self->{_connections} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_ and builtin::refaddr($_) == $addr;} @{$$self{"_connections"};} 0 440 440
1086 440 $self->_shutdown_ready_connection($conn);
1087 }
1088 }
1089  
1090 # An acquire still waiting for its connection to come up that the loop above
1091 # did not fail -- its readiness Future is one the transport's own seam made,
1092 # not one registered for a connection the pool holds -- fails here all the
1093 # same. The pool held the chain off that readiness Future for the caller and
1094 # lets go of it now, cancelling it where its class can be (karr core k258).
1095 555 ▶ my $handovers = $self->{_ready_handovers} || {};
Condition100%2/2
$self->{'_ready_handovers'} || {}
l!l
70485
MC/DC100%1/1
$self->{'_ready_handovers'} || {}
$self->{'_ready_handovers'}
Truth table100%2/2
A
A= $self->{'_ready_handovers'}
Aresult
11
00
1096 555 $self->{_ready_handovers} = {};
1097 555 ▶ if ($settle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settle) 555 0 555
1098 555 for my $handover (values %$handovers) {
1099 8 my ($caller, $chain, undef, $state) = @$handover;
1100  
1101 # A chain that cannot be cancelled stays on its readiness Future, and
1102 # held the connection for as long as that Future was (karr core k261).
1103 8 ▶ %$state = () if ref $state;
Branch: 1 branch
BranchTrueFalseTotal
if ref $state 8 0 8
1104 8 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $caller->fail("Connection pool has been shut down") if defined $caller and not $caller->is_ready; 1 } } or _report_hand_over_death($self, $@, "the shutdown failed a caller waiting for its connection to come up") 8 0 8
1105 8 ▶ $caller->fail('Connection pool has been shut down')
Branch: 1 branch
BranchTrueFalseTotal
if defined $caller and not $caller->is_ready 8 0 8
Condition33%1/3
defined $caller and not $caller->is_ready
!ll&&!rl&&r
008
MC/DC0%0/2
defined $caller and not $caller->is_ready
defined $caller not $caller->is_ready
Truth table33%1/3
A and not(B)
A= defined $callerB= not $caller->is_ready
ABresult
0X0
100
111
1106 if defined $caller && !$caller->is_ready;
1107 8 1;
1108 } or _report_hand_over_death($self, $@,
1109 'the shutdown failed a caller waiting for its connection to come up');
1110 8 ▶ eval { $chain->cancel if defined $chain && $chain->can('cancel'); 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $chain->cancel if defined $chain and $chain->can("cancel"); 1 } } or _report_hand_over_death($self, $@, "the shutdown cancelled the wait for a connection to come up") 8 0 8
if defined $chain and $chain->can("cancel") 0 8 8
Condition33%1/3
defined $chain and $chain->can("cancel")
!ll&&!rl&&r
080
MC/DC0%0/2
defined $chain and $chain->can("cancel")
defined $chain $chain->can("cancel")
Truth table33%1/3
A and B
A= defined $chainB= $chain->can("cancel")
ABresult
0X0
100
111
1111 or _report_hand_over_death($self, $@,
1112 'the shutdown cancelled the wait for a connection to come up');
1113 }
1114 }
1115  
1116 555 $self->_drop_resets($settle);
1117 555 $self->{_connections} = [];
1118 555 $self->{_idle} = [];
1119 555 $self->{_waiters} = [];
1120 555 $self->{_ready} = {};
1121 555 $self->{_leases} = {};
1122 555 return 1;
1123 }
1124  
1125 # Fork safety: may THIS process physically tear down the pool's connections?
1126 # True only in the process that owns the pool: the one that created it, or a
1127 # forked child that has since used it and so, through _verify_pid, dropped the
1128 # parent's connections and taken the pool over with connections of its own. A
1129 # forked child that inherited the pool object and never used it (a different
1130 # PID) must leave the parent's live connections alone -- closing them here would
1131 # disconnect the owner. Tracked once per pool rather than per connection: every
1132 # connection the pool holds belongs to its one owner, and a single scalar
1133 # survives global-destruction SV reclamation better than a per-connection side
1134 # table would. A pool whose owner PID is already gone (blessed directly in a
1135 # test, or reclaimed ahead of us during global destruction) is treated as NOT
1136 # ours, which fails safe: the worst case is a connection left for the OS to
1137 # reclaim at process exit, never the owner's connection closed from under it.
1138 555 ▶sub _teardown_belongs_here {
Subroutine _teardown_belongs_here: called CC=3 SCAR=11.1
1139 555 my ($self) = @_;
1140 555 ▶ return 0 unless defined $self->{_owner_pid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_owner_pid"} 1 554 555
1141 554 ▶ return $self->{_owner_pid} == $$ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_owner_pid'} == $$ ? : 554 0 554
1142 }
1143  
1144  
1145 3847 ▶sub _verify_pid {
Subroutine _verify_pid: called CC=5 SCAR=16.1
1146 3847 my ($self) = @_;
1147 3847 my $pid = $self->{_owner_pid};
1148 3847 ▶ return unless defined $pid && $pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid != $$ 3832 15 3847
Condition100%3/3
defined $pid and $pid != $$
!ll&&!rl&&r
1383115
MC/DC100%2/2
defined $pid and $pid != $$
defined $pid $pid != $$
Truth table100%3/3
A and B
A= defined $pidB= $pid != $$
ABresult
0X0
100
111
1149  
1150 # The inherited hand-overs are let go of, not cancelled and not just
1151 # forgotten (karr core k261): a chain forgotten with its caller still in
1152 # hand settled the parent's caller here when its readiness Future did, and
1153 # cancelling one would run the parent's on_cancel callbacks.
1154 15 ▶ $self->_park_hand_over($_) for keys %{ $self->{_ready_handovers} || {} };
Condition100%2/2
$self->{'_ready_handovers'} || {}
l!l
87
MC/DC100%1/1
$self->{'_ready_handovers'} || {}
$self->{'_ready_handovers'}
Truth table100%2/2
A
A= $self->{'_ready_handovers'}
Aresult
11
00
1155  
1156 15 $self->{_connections} = [];
1157 15 $self->{_idle} = [];
1158 15 $self->{_waiters} = [];
1159 15 $self->{_spawning} = {};
1160 15 $self->{_ready} = {};
1161 15 $self->{_leases} = {};
1162 15 $self->{_ready_handovers} = {};
1163 15 $self->_drop_resets(0);
1164 15 $self->{_owner_pid} = $$;
1165 15 return;
1166 }
1167  
1168 # Does the pool hold $conn -- is it one of the connections it counts against
1169 # max_size? A connection it retired, or dropped as inherited, is not.
1170 1634 ▶sub _holds_connection {
Subroutine _holds_connection: called CC=3 SCAR=11.2
1171 1634 my ($self, $conn) = @_;
1172 1634 ▶ return 0 unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 1634 1634
1173 1634 my $addr = Scalar::Util::refaddr($conn);
1174 return scalar grep {
1175 1671 ▶ defined($_) && Scalar::Util::refaddr($_) == $addr
Condition66%2/3
defined $_ && builtin::refaddr($_) == $addr
!ll&&!rl&&r
0421629
MC/DC50%1/2
defined $_ && builtin::refaddr($_) == $addr
defined $_ builtin::refaddr($_) == $addr
Truth table66%2/3
A && B
A= defined $_B= builtin::refaddr($_) == $addr
ABresult
0X0
100
111
1176 1634 } @{ $self->{_connections} };
1177 }
1178  
1179 # Is an acquire still waiting for $conn to come up -- does the pool hold a
1180 # hand-over for it (_hand_over_when_ready)? An entry may be gone already in
1181 # global destruction, hence the guards.
1182 448 ▶sub _hand_over_pending {
Subroutine _hand_over_pending: called CC=4 SCAR=14.3
1183 448 my ($self, $conn) = @_;
1184 448 my $addr = Scalar::Util::refaddr($conn);
1185 return scalar grep {
1186 9 ▶ ref $_ && defined $_->[2] && Scalar::Util::refaddr($_->[2]) == $addr
Condition50%3/6
ref $_ && defined $_->[2] && builtin::refaddr($_->[2]) == $addr
!ll&&!rl&&r
018
ref $_ and defined $_->[2]
!ll&&!rl&&r
009
MC/DC33%1/3
ref $_ && defined $_->[2] && builtin::refaddr($_->[2]) == $addr
ref $_ defined $_->[2] builtin::refaddr($_->[2]) == $addr
Truth table50%2/4
A and B && C
A= ref $_B= defined $_->[2]C= builtin::refaddr($_->[2]) == $addr
ABCresult
0XX0
10X0
1100
1111
1187 448 ▶ } values %{ $self->{_ready_handovers} || {} };
Condition100%2/2
$self->{'_ready_handovers'} || {}
l!l
41407
MC/DC100%1/1
$self->{'_ready_handovers'} || {}
$self->{'_ready_handovers'}
Truth table100%2/2
A
A= $self->{'_ready_handovers'}
Aresult
11
00
1188 }
1189  
1190 # Is $conn on the idle list -- released, and not handed out since?
1191 867 ▶sub _connection_is_idle {
Subroutine _connection_is_idle: called CC=2 SCAR=7.0
1192 867 my ($self, $conn) = @_;
1193 867 my $addr = Scalar::Util::refaddr($conn);
1194 return scalar grep {
1195 19 ▶ defined($_) && Scalar::Util::refaddr($_) == $addr
Condition66%2/3
defined $_ && builtin::refaddr($_) == $addr
!ll&&!rl&&r
0154
MC/DC50%1/2
defined $_ && builtin::refaddr($_) == $addr
defined $_ builtin::refaddr($_) == $addr
Truth table66%2/3
A && B
A= defined $_B= builtin::refaddr($_) == $addr
ABresult
0X0
100
111
1196 867 } @{ $self->{_idle} };
1197 }
1198  
1199 440 ▶sub _shutdown_ready_connection {
Subroutine _shutdown_ready_connection: called CC=2 SCAR=7.0
1200 440 my ($self, $conn) = @_;
1201 440 ▶ return unless $self->_detach_connection($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_detach_connection($conn) 0 440 440
1202  
1203 # on_disconnect_do / on_disconnect_call run against a connection that reached
1204 # readiness, while it is still live and before it is closed. Both steps remain
1205 # best-effort.
1206 440 eval { $self->_dbio_run_pool_disconnect_actions($conn) };
1207 440 eval { $self->_shutdown_connection($conn) };
1208 440 $self->_clear_connection_ready($conn);
1209 440 return;
1210 }
1211  
1212  
1213 8 ▶sub set_conninfo_provider {
Subroutine set_conninfo_provider: called CC=2 SCAR=7.0
Pod documented
1214 8 my ($self, $provider) = @_;
1215 8 ▶ croak('set_conninfo_provider requires a coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $provider eq "CODE" 0 8 8
1216 unless ref $provider eq 'CODE';
1217 8 $self->{conninfo_provider} = $provider;
1218 8 delete $self->{conninfo};
1219 8 return $self;
1220 }
1221  
1222 # Room for one more connection? A spawn in flight counts: it holds its slot
1223 # from before the first code it calls that is not the pool's (karr core k277).
1224 1151 ▶sub _has_room {
Subroutine _has_room: called CC=2 SCAR=7.0
1225 1151 my ($self) = @_;
1226 1151 ▶ return @{ $self->{_connections} } + scalar( keys %{ $self->{_spawning} || {} } )
Condition50%1/2
$self->{'_spawning'} || {}
l!l
11510
MC/DC0%0/1
$self->{'_spawning'} || {}
$self->{'_spawning'}
Truth table50%1/2
A
A= $self->{'_spawning'}
Aresult
11
00
1227 1151 < $self->{max_size};
1228 }
1229  
1230 # Returns the connection, dies with the error of a spawn that failed -- masked
1231 # of connect secrets, for every transport (karr k305) -- or returns nothing when
1232 # the pool was shut down under it.
1233 820 ▶sub _spawn_connection {
Subroutine _spawn_connection: called CC=8 SCAR=20.8
1234 820 my $self = shift;
1235  
1236 # Everything a spawn calls before its connection counts is code that is not
1237 # the pool's -- the provider (an AccessBroker's answer), the transport's
1238 # seams -- and may come back into the pool: a client that runs the event
1239 # loop while it fetches, a transport that reports an error from its
1240 # constructor. So the slot is taken before any of it runs, and whoever comes
1241 # in from inside finds it taken. Counted only once it existed, the connection
1242 # left its slot to an acquire made from inside: two connections in a pool of
1243 # one (karr core k277). The reservation is this spawn's own entry, so letting
1244 # go of it after _verify_pid dropped it with the rest in a forked child takes
1245 # nothing from another spawn.
1246 820 my $slot = ++$self->{_spawn_seq};
1247 820 $self->{_spawning}{$slot} = 1;
1248  
1249 820 my ($conn, $conninfo, @conninfo);
1250 820 my $made = eval {
1251 $conninfo = $self->{conninfo_provider}
1252 ? $self->{conninfo_provider}->()
1253 820 ▶ : $self->{conninfo};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'conninfo_provider'} ? : 231 589 820
1254 761 @conninfo = $self->_transform_conninfo($conninfo);
1255  
1256 # What an error of this connection, raised later by the transport, is
1257 # masked with (_create_ready_connection, karr k400).
1258 755 local $self->{_spawn_conninfo} = [ $conninfo, @conninfo ];
1259  
1260 # Shut down from inside one of the two: no connection is made.
1261 755 ▶ $conn = $self->_create_connection(@conninfo) unless $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_shutdown"} 750 5 755
1262 726 1;
1263 };
1264 820 my $error = $@;
1265  
1266 # From here the connection counts, or the spawn took no slot (karr core
1267 # k260).
1268 820 delete $self->{_spawning}{$slot};
1269  
1270 # A client library may quote the connection string it was given in the error
1271 # it raises, a password in it. The core masks the error here for every
1272 # transport (karr k305) -- the one place that does, through the one function
1273 # that knows how. What the pool knows of the connect info goes along: a
1274 # string conninfo as the DSN slot, a hash with a password as the password
1275 # slot. A reference (an exception object) goes through as it came.
1276 820 ▶ die $self->_mask_connect_error($error, $conninfo, @conninfo) unless $made;
Branch: 1 branch
BranchTrueFalseTotal
unless $made 94 726 820
1277  
1278 # Shut down from inside _create_connection: the connection was born into a
1279 # pool that is gone. shutdown never saw it, so it is closed here, like every
1280 # cold connection -- once, no on_disconnect_* -- and not counted.
1281 726 ▶ if ($self->{_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_shutdown"}) 7 719 726
1282 7 ▶ if (defined $conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn) 2 5 7
1283 2 eval { $self->_shutdown_connection($conn) };
1284 2 $self->_clear_connection_ready($conn);
1285 }
1286 7 return;
1287 }
1288  
1289 719 push @{ $self->{_connections} }, $conn;
1290  
1291 # A freshly spawned physical connection must receive the same
1292 # session setup (on_connect_do / on_connect_call) the owning sync storage was
1293 # configured with, before it is ever served to a caller -- otherwise pooled
1294 # async connections diverge silently from the sync path. Runs exactly once per
1295 # physical connection (idle reuse goes through acquire, not spawn).
1296 #
1297 # The connection is registered above, before its session setup
1298 # runs. One whose setup was refused is never handed out and never will be, so
1299 # retire it before rethrowing -- otherwise it keeps counting against max_size
1300 # and a pool that goes on being refused leaks one slot per attempt, until it
1301 # is "full" of connections that do not exist and acquire() only ever returns
1302 # pending waiters.
1303 719 ▶ unless (eval { $self->_dbio_run_pool_connect_actions($conn); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_dbio_run_pool_connect_actions($conn); 1 } }) 11 708 719
1304 11 my $err = $@;
1305 11 $self->_retire_connection($conn);
1306 11 die $err;
1307 }
1308  
1309 # Shut down from inside the connect actions: the connection counted by then,
1310 # so shutdown closed it with the rest.
1311 708 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 2 706 708
1312  
1313 706 return $conn;
1314 }
1315  
1316  
1317 149 ▶sub _retire_connection {
Subroutine _retire_connection: called CC=4 SCAR=13.9
1318 149 my ($self, $conn) = @_;
1319 149 $self->_verify_pid;
1320 149 ▶ return unless $self->_detach_connection($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_detach_connection($conn) 5 144 149
1321  
1322 144 eval { $self->_shutdown_connection($conn) };
1323 144 $self->_clear_connection_ready($conn);
1324 144 ▶ delete $self->{_dead}{ Scalar::Util::refaddr($conn) } if $self->{_dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} 10 134 144
1325 144 ▶ $self->_let_go_of_hand_overs_for($conn) unless $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_shutdown"} 128 16 144
1326  
1327 144 return;
1328 }
1329  
1330  
1331 113 ▶sub _retire_in_use_connection {
Subroutine _retire_in_use_connection: called CC=2 SCAR=6.9
1332 113 my ($self, $conn) = @_;
1333 113 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 13 100 113
1334  
1335 100 $self->_retire_connection($conn);
1336 100 $self->_spawn_for_next_waiter;
1337 100 return;
1338 }
1339  
1340 # A free slot goes to the oldest caller still waiting, on a fresh spawn. A
1341 # spawn that dies fails the waiter it was made for and leaves the slot free,
1342 # so the slot goes on to the next one (karr core k260).
1343 184 ▶sub _spawn_for_next_waiter {
Subroutine _spawn_for_next_waiter: called CC=13 SCAR=25.8
1344 184 my ($self) = @_;
1345  
1346 184 ▶ while (!$self->{_shutdown} && $self->_has_room) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_shutdown"} and $self->_has_room) 193 5 198
Condition100%3/3
not $self->{"_shutdown"} and $self->_has_room
!ll&&!rl&&r
41193
MC/DC100%2/2
not $self->{"_shutdown"} and $self->_has_room
not $self->{"_shutdown"} $self->_has_room
Truth table100%3/3
not(A) and B
A= not $self->{"_shutdown"}B= $self->_has_room
ABresult
0X0
100
111
1347 193 ▶ my $waiter = $self->_next_waiter or return;
Branch: 1 branch
BranchTrueFalseTotal
my $waiter = $self->_next_waiter or return 55 138 193
1348  
1349 55 my $fresh;
1350 # The waiter is another caller's Future, wherever it is settled below: a
1351 # callback of it that dies goes to on_error, not to whoever is handing the
1352 # slot on, and the slot goes on to the next waiter all the same (karr core
1353 # k279).
1354 55 ▶ unless (eval { $fresh = $self->_spawn_connection; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $fresh = $self->_spawn_connection; 1 } }) 14 41 55
1355 14 ▶ my $error = $@ || 'Connection spawn failed';
Condition50%1/2
$@ || 'Connection spawn failed'
l!l
140
MC/DC0%0/1
$@ || 'Connection spawn failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1356 14 ▶ eval { $waiter->fail($error) unless $waiter->is_ready; 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $waiter->fail($error) unless $waiter->is_ready; 1 } } or _report_hand_over_death($self, $@, "a spawn's failure was handed to a queued caller") 14 0 14
unless $waiter->is_ready 13 1 14
1357 or _report_hand_over_death($self, $@,
1358 "a spawn's failure was handed to a queued caller");
1359 14 next;
1360 }
1361  
1362 # The spawn ran code that is not the pool's while its waiter was off the
1363 # queue (karr core k277). A pool shut down from inside failed everyone
1364 # still queued, and this caller fails the same way. A caller that gave up
1365 # from inside -- cancelled, or settled by somebody else -- is passed over
1366 # as release passes it over (karr core k255): its connection goes where
1367 # release sends one, to the next caller still waiting or to the idle list.
1368 41 ▶ unless (defined $fresh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fresh) 4 37 41
1369 4 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $waiter->fail("Connection pool has been shut down") unless $waiter->is_ready; 1 } } or _report_hand_over_death($self, $@, "the shutdown failed a queued caller") 4 0 4
1370 4 ▶ $waiter->fail('Connection pool has been shut down') unless $waiter->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $waiter->is_ready 4 0 4
1371 4 1;
1372 } or _report_hand_over_death($self, $@, 'the shutdown failed a queued caller');
1373 4 return;
1374 }
1375 37 ▶ return $self->_release_unused($fresh) if $waiter->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $waiter->is_ready 2 35 37
1376  
1377 35 ▶ eval { $waiter->done($fresh); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $waiter->done($fresh); 1 } } or _report_hand_over_death($self, $@, "a new connection was handed to a queued caller") 35 0 35
1378 or _report_hand_over_death($self, $@,
1379 'a new connection was handed to a queued caller');
1380 35 return;
1381 }
1382 5 return;
1383 }
1384  
1385  
1386 17 ▶sub _mark_connection_dead {
Subroutine _mark_connection_dead: called CC=6 SCAR=18.0
1387 17 my ($self, $conn) = @_;
1388 17 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 2 15 17
1389 15 ▶ return unless defined $conn && $self->_holds_connection($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn and $self->_holds_connection($conn) 2 13 15
Condition100%3/3
defined $conn and $self->_holds_connection($conn)
!ll&&!rl&&r
1113
MC/DC100%2/2
defined $conn and $self->_holds_connection($conn)
defined $conn $self->_holds_connection($conn)
Truth table100%3/3
A and B
A= defined $connB= $self->_holds_connection($conn)
ABresult
0X0
100
111
1390  
1391 13 my $addr = Scalar::Util::refaddr($conn);
1392 13 ▶ if (grep { defined($_) && Scalar::Util::refaddr($_) == $addr } @{ $self->{_idle} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_ and builtin::refaddr($_) == $addr;} @{$$self{"_idle"};}) 1 12 13
Condition33%1/3
defined $_ && builtin::refaddr($_) == $addr
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_ && builtin::refaddr($_) == $addr
defined $_ builtin::refaddr($_) == $addr
Truth table33%1/3
A && B
A= defined $_B= builtin::refaddr($_) == $addr
ABresult
0X0
100
111
1393 1 $self->_retire_connection($conn);
1394 1 return;
1395 }
1396  
1397 12 Scalar::Util::weaken( $self->{_dead}{$addr} = $conn );
1398 12 return;
1399 }
1400  
1401 # Was $conn reported dead through _mark_connection_dead? The mark is a weak
1402 # reference to the connection itself, so it is gone once that connection is.
1403 953 ▶sub _connection_is_dead {
Subroutine _connection_is_dead: called CC=4 SCAR=13.9
1404 953 my ($self, $conn) = @_;
1405 953 ▶ return 0 unless $self->{_dead} && defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_dead"} and defined $conn 941 12 953
Condition66%2/3
$self->{"_dead"} and defined $conn
!ll&&!rl&&r
941012
MC/DC50%1/2
$self->{"_dead"} and defined $conn
$self->{"_dead"} defined $conn
Truth table66%2/3
A and B
A= $self->{"_dead"}B= defined $conn
ABresult
0X0
100
111
1406 12 ▶ return defined $self->{_dead}{ Scalar::Util::refaddr($conn) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_dead'}{builtin::refaddr($conn)} ? : 10 2 12
1407 }
1408  
1409 589 ▶sub _detach_connection {
Subroutine _detach_connection: called CC=6 SCAR=17.9
1410 589 my ($self, $conn) = @_;
1411 589 ▶ return unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 589 589
1412  
1413 589 my $addr = Scalar::Util::refaddr($conn);
1414 return unless grep {
1415 617 ▶ defined($_) && Scalar::Util::refaddr($_) == $addr
Condition100%3/3
defined $_ && builtin::refaddr($_) == $addr
!ll&&!rl&&r
429584
MC/DC100%2/2
defined $_ && builtin::refaddr($_) == $addr
defined $_ builtin::refaddr($_) == $addr
Truth table100%3/3
A && B
A= defined $_B= builtin::refaddr($_) == $addr
ABresult
0X0
100
111
1416 589 ▶ } @{ $self->{_connections} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_ and builtin::refaddr($_) == $addr;} @{$$self{"_connections"};} 5 584 589
1417  
1418 584 delete $self->{_leases}{$addr};
1419 584 $self->_void_reset($conn, 1);
1420 $self->{_connections} = [
1421 584 ▶ grep { defined($_) && Scalar::Util::refaddr($_) != $addr } @{ $self->{_connections} }
Condition100%3/3
defined $_ && builtin::refaddr($_) != $addr
!ll&&!rl&&r
458429
MC/DC100%2/2
defined $_ && builtin::refaddr($_) != $addr
defined $_ builtin::refaddr($_) != $addr
Truth table100%3/3
A && B
A= defined $_B= builtin::refaddr($_) != $addr
ABresult
0X0
100
111
1422 ];
1423 $self->{_idle} = [
1424 584 ▶ grep { defined($_) && Scalar::Util::refaddr($_) != $addr } @{ $self->{_idle} }
Condition100%3/3
defined $_ && builtin::refaddr($_) != $addr
!ll&&!rl&&r
320614
MC/DC100%2/2
defined $_ && builtin::refaddr($_) != $addr
defined $_ builtin::refaddr($_) != $addr
Truth table100%3/3
A && B
A= defined $_B= builtin::refaddr($_) != $addr
ABresult
0X0
100
111
1425 ];
1426 584 return 1;
1427 }
1428  
1429 # Bridge the central pool-spawn / shutdown path to the owning async
1430 # storage's connect-action seam (DBIO::Storage::Async::_setup_pool_connection /
1431 # _teardown_pool_connection). No-op unless an async storage back-ref is wired
1432 # and exposes the seam, so non-async pools and un-wired pools are unaffected.
1433 719 ▶sub _dbio_run_pool_connect_actions {
Subroutine _dbio_run_pool_connect_actions: called CC=3 SCAR=11.0
1434 719 my ($self, $conn) = @_;
1435 719 ▶ my $storage = $self->{storage} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $storage = $self->{"storage"} or return 127 592 719
1436 127 ▶ return unless $storage->can('_setup_pool_connection');
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->can("_setup_pool_connection") 15 112 127
1437 112 $storage->_setup_pool_connection($conn);
1438 }
1439  
1440 440 ▶sub _dbio_run_pool_disconnect_actions {
Subroutine _dbio_run_pool_disconnect_actions: called CC=3 SCAR=11.0
1441 440 my ($self, $conn) = @_;
1442 440 ▶ my $storage = $self->{storage} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $storage = $self->{"storage"} or return 73 367 440
1443 73 ▶ return unless $storage->can('_teardown_pool_connection');
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->can("_teardown_pool_connection") 0 73 73
1444 73 $storage->_teardown_pool_connection($conn);
1445 }
1446  
1447  
1448 2 ▶sub _create_connection { croak 'Subclass must override _create_connection' }
Subroutine _create_connection: called CC=1 SCAR=0.0
1449  
1450  
1451 243 ▶sub _shutdown_connection {}
Subroutine _shutdown_connection: called CC=1 SCAR=0.0
1452  
1453  
1454 578 ▶sub _transform_conninfo { $_[1] }
Subroutine _transform_conninfo: called CC=1 SCAR=0.0
1455  
1456 553 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
1457 553 my $self = shift;
1458 553 $self->shutdown;
1459 }
1460  
1461 140 1;
1462  
1463 __END__
« blib/lib/DBIO/Test/Kubernetes.pm ↑ Summary blib/lib/DBIO/Row.pm »