blib/lib/DBIO/PostgreSQL/EV/Future.pm

filter: Statementstmt 100% 94 / 94 Branchbran 90.4% 38 / 42 Conditioncond 72.2% 13 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 92.5% 174 / 188 CC 30 SCAR 12.2
CC
30
Cov
94%
await
6.0
new
5.2
_queued_caller
5.1
CRAP 3.4
« blib/lib/DBIO/PostgreSQL/EV/Pool.pm ↑ Summary blib/lib/DBIO/PostgreSQL/EV/TestHarness.pm »
1 package DBIO::PostgreSQL::EV::Future;
2 # ABSTRACT: A Future whose get waits on the EV loop
3  
4 73 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 73 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 73 our $VERSION = '0.900007';
8  
9 73 ▶use base 'Future';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 73 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 73 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 73 ▶use EV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 667 ▶sub await {
Subroutine await: called CC=6 SCAR=17.9
Pod documented
17 667 my $self = shift;
18 667 ▶ until ($self->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_ready) 1195 647 1842
19 1195 ▶ if (my $conn = _delivering_with_more_due()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = _delivering_with_more_due()) 18 1177 1195
20 18 my $refusal = _refusal($conn);
21 # A caller still queued for a connection has nothing on the wire to
22 # complete: it leaves the queue, whatever the wait ends with (karr k85).
23 18 $self->_withdraw_queued($refusal);
24 # What is on the wire completes: the dying caller drops this Future, so
25 # keep its chain running.
26 18 $self->retain;
27 18 Carp::croak($refusal);
28 }
29 # Failing the queued caller this waits for settles this Future, unless its
30 # chain catches the failure. Asked before every iteration, as the refusal
31 # above is: the pool is not what it was before the last one.
32 1177 ▶ next if $self->_refuse_queued;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_refuse_queued 18 1159 1177
33 1159 ▶ unless (EV::run(EV::RUN_ONCE()) or $self->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&EV::run(2) or $self->is_ready) 2 1157 1159
Condition100%3/3
&EV::run(2) or $self->is_ready
l!l&&r!l&&!r
6185392
MC/DC100%2/2
&EV::run(2) or $self->is_ready
&EV::run(2) $self->is_ready
Truth table100%3/3
A or B
A= &EV::run(2)B= $self->is_ready
ABresult
1X1
011
000
34 2 my $dry = "$self is pending and the EV loop has nothing left that could complete it";
35 2 $self->_withdraw_queued($dry); # karr k86
36 2 Carp::croak($dry);
37 }
38 }
39 647 return $self;
40 }
41  
42 18 ▶sub _refusal {
Subroutine _refusal: called CC=1 SCAR=0.0
43 18 my ($conn) = @_;
44 18 return 'Blocking wait refused: this blocking call runs inside a callback of '
45 . 'an EV::Pg connection that has ' . ($conn->pending_count - 1)
46 . ' more result(s) due, and EV::Pg would hand the next of them to the '
47 . 'callback that is still running, then spin. Use the *_async methods and '
48 . 'chain with ->then instead (in a txn_do coderef: $ctx->select_async, '
49 . '$ctx->insert_async, ...)';
50 }
51  
52  
53 # The mark is kept as Future's user data (udata, Future 0.49), never in the
54 # instance itself: [ $waiter, $pool ], both weak -- the pool holds the waiter,
55 # and the waiter and everything chained off it share the one arrayref.
56 73 my $QUEUED = 'dbio_postgresql_ev_queued';
57  
58 9446 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
59 9446 my $proto = shift;
60 9446 my $self = $proto->SUPER::new(@_);
61 9446 ▶ if (ref $proto and my $queued = $proto->udata($QUEUED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto and my $queued = $proto->udata($QUEUED)) 169 9277 9446
Condition100%3/3
ref $proto and my $queued = $proto->udata($QUEUED)
!ll&&!rl&&r
56193658169
MC/DC100%2/2
ref $proto and my $queued = $proto->udata($QUEUED)
ref $proto my $queued = $proto->udata($QUEUED)
Truth table100%3/3
A and B
A= ref $protoB= my $queued = $proto->udata($QUEUED)
ABresult
0X0
100
111
62 169 ▶ $self->set_udata($QUEUED => $queued)
Branch: 1 branch
BranchTrueFalseTotal
if $queued->[0] and not $queued->[0]->is_ready 169 0 169
Condition33%1/3
$queued->[0] and not $queued->[0]->is_ready
!ll&&!rl&&r
00169
MC/DC0%0/2
$queued->[0] and not $queued->[0]->is_ready
$queued->[0] not $queued->[0]->is_ready
Truth table33%1/3
A and not(B)
A= $queued->[0]B= not $queued->[0]->is_ready
ABresult
0X0
100
111
63 if $queued->[0] && !$queued->[0]->is_ready;
64 }
65 9446 return $self;
66 }
67  
68  
69 87 ▶sub _queued_in {
Subroutine _queued_in: called CC=2 SCAR=6.9
70 87 my ($self, $pool) = @_;
71 87 my $queued = [ $self, $pool ];
72 87 Scalar::Util::weaken($_) for @$queued;
73 87 return $self->set_udata($QUEUED => $queued);
74 }
75  
76 # Refuse the wait when this Future waits for a caller queued for a pooled
77 # connection, and no connection of that pool can come back before a callback
78 # that is running returns: take the caller out of the queue and fail it (karr
79 # k71). True when it did. Failed, not cancelled, and taken out first: a caller
80 # left in the queue is handed the connection once the callback has returned,
81 # and the call its caller was told had failed runs after all. Nothing else is
82 # touched -- a queued caller nobody waits for stays queued, and is served once
83 # those callbacks have returned.
84 1237 ▶sub _refuse_queued {
Subroutine _refuse_queued: called CC=3 SCAR=11.0
85 1237 my ($self) = @_;
86 1237 ▶ my ($waiter, $pool) = $self->_queued_caller or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_queued_caller or return 0 63 1174 1237
87 63 ▶ return 0 unless $pool->_pg_held_by_callbacks;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool->_pg_held_by_callbacks 43 20 63
88 20 return $self->_withdraw_queued(_queued_refusal($pool->max_size, $pool->_pg_opening));
89 }
90  
91 # The caller this Future waits for and the pool it is queued in, while it is
92 # queued and pending; nothing otherwise.
93 1277 ▶sub _queued_caller {
Subroutine _queued_caller: called CC=5 SCAR=16.2
94 1277 my ($self) = @_;
95 1277 ▶ my $queued = $self->udata($QUEUED) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $queued = $self->udata($QUEUED) or return 129 1148 1277
96 129 my ($waiter, $pool) = @$queued;
97 129 ▶ return unless $waiter && !$waiter->is_ready && $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $waiter and not $waiter->is_ready and $pool 38 91 129
Condition66%4/6
$waiter and not $waiter->is_ready and $pool
!ll&&!rl&&r
38091
$waiter and not $waiter->is_ready
!ll&&!rl&&r
38091
MC/DC33%1/3
$waiter and not $waiter->is_ready and $pool
$waiter not $waiter->is_ready $pool
Truth table50%2/4
A and not(B) and C
A= $waiterB= not $waiter->is_readyC= $pool
ABCresult
0XX0
10X0
1100
1111
98 91 return ($waiter, $pool);
99 }
100  
101 # The one way a wait that is given up takes the caller it waits for out of the
102 # pool (karr k71, k85, k86): out of the queue first, then failed with $message.
103 # A caller that has no connection yet and is not taken out would be handed one
104 # when the next is released, and run after its caller was told the wait had
105 # failed -- or, its chain dropped with the dying caller, lose the connection
106 # and wedge the pool. True when it was queued and is out. A callback that dies
107 # as the caller is failed does not undo that; it is warned about.
108 40 ▶sub _withdraw_queued {
Subroutine _withdraw_queued: called CC=4 SCAR=13.9
109 40 my ($self, $message) = @_;
110 40 ▶ my ($waiter, $pool) = $self->_queued_caller or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_queued_caller or return 0 28 12 40
111 28 ▶ return 0 unless $pool->_withdraw_waiter($waiter);
Branch: 1 branch
BranchTrueFalseTotal
unless $pool->_withdraw_waiter($waiter) 0 28 28
112 28 ▶ eval { $waiter->fail($message); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $waiter->fail($message); 1 } } or warn "DBIO::PostgreSQL::EV: a callback died as a refused blocking call was failed: $@" 27 1 28
113 or warn "DBIO::PostgreSQL::EV: a callback died as a refused blocking call was failed: $@";
114 28 return 1;
115 }
116  
117 # $opening: how many of the pool's connections a spawn that is running holds
118 # (karr k88). Without one the callbacks on the call stack hold them all, and
119 # the text is that of karr k71.
120 20 ▶sub _queued_refusal {
Subroutine _queued_refusal: called CC=2 SCAR=6.9
121 20 my ($size, $opening) = @_;
122 20 my $queued = 'Blocking wait refused: this blocking call is queued for a '
123 . "connection of a pool of $size, and ";
124 20 ▶ return $queued . 'every one of them is held by a callback this '
Branch: 1 branch
BranchTrueFalseTotal
unless $opening 6 14 20
125 . 'call runs inside -- a then on a statement, or the coderef of a txn_do '
126 . '-- which gives its connection back only after it has returned, so '
127 . 'waiting here would never end. Use the *_async methods of the storage '
128 . 'and chain with ->then instead of blocking (to run on the connection of '
129 . 'a transaction, use its context: $ctx->select_async, $ctx->insert_async, '
130 . '...), or raise pool_size'
131 unless $opening;
132 14 return $queued . 'none of them can come back while it waits: the pool is '
133 . "opening $opening of them in a call this one runs inside -- this call "
134 . 'was made where the AccessBroker is asked for connect info, or in a '
135 . 'callback the EV loop ran while that, or the replay of on_connect_do / '
136 . 'on_connect_call, waited -- and a connection being opened is handed out '
137 . 'only after that call has returned; any other is held by a callback '
138 . 'this call runs inside. Waiting here would never end. Use the *_async '
139 . 'methods of the storage and chain with ->then instead of blocking (an '
140 . 'AccessBroker cannot wait for the storage it answers), or raise '
141 . 'pool_size';
142 }
143  
144 # The connections one of this transport's callbacks is being delivered for
145 # right now, by refaddr: set by the frame _delivering hands out, for exactly as
146 # long as that callback runs.
147 73 my %DELIVERING;
148  
149 # Called first thing in every callback the storage hands to EV::Pg for $conn;
150 # the callback keeps the returned frame in a lexical. While the frame lives,
151 # $conn is delivering; when it goes out of scope -- the callback returns or
152 # dies -- the previous state is restored, so a callback of the same connection
153 # nested in another has a frame of its own. Set inside the callback rather than
154 # by wrapping it, so the callback EV::Pg keeps stays the one created in the
155 # storage's dispatch sub (karr k29 / k30).
156 1154 ▶sub _delivering {
Subroutine _delivering: called CC=2 SCAR=7.0
157 1154 my ($class, $conn) = @_;
158 # A callback EV::Pg runs after its connection is gone (global destruction)
159 # delivers nothing: no frame, %DELIVERING untouched.
160 1154 ▶ return unless defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 1154 1154
161 1154 my $key = Scalar::Util::refaddr($conn);
162 1154 my $frame = bless [ $key, $DELIVERING{$key} ], 'DBIO::PostgreSQL::EV::Future::_Frame';
163 1154 $DELIVERING{$key} = $conn;
164 1154 return $frame;
165 }
166  
167 1154 ▶sub DBIO::PostgreSQL::EV::Future::_Frame::DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
168 1154 my ($key, $outer) = @{ $_[0] };
169 1154 ▶ if (defined $outer) { $DELIVERING{$key} = $outer }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outer) { } 4 1150 1154
170 1150 else { delete $DELIVERING{$key} }
171 1154 return;
172 }
173  
174 # A delivering connection with more than the delivering statement pending, or
175 # undef. EV::Pg counts the delivering callback in pending_count until it
176 # returns, so anything above one is a result due during a nested loop.
177 1286 ▶sub _delivering_with_more_due {
Subroutine _delivering_with_more_due: called CC=4 SCAR=13.9
178 1286 for my $conn (values %DELIVERING) {
179 63 ▶ return $conn if $conn && $conn->pending_count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $conn and $conn->pending_count > 1 21 42 63
Condition66%2/3
$conn and $conn->pending_count > 1
!ll&&!rl&&r
04221
MC/DC50%1/2
$conn and $conn->pending_count > 1
$conn $conn->pending_count > 1
Truth table66%2/3
A and B
A= $connB= $conn->pending_count > 1
ABresult
0X0
100
111
180 }
181 1265 return;
182 }
183  
184  
185 73 ▶sub _held_by_callback {
Subroutine _held_by_callback: called CC=3 SCAR=11.0
186 73 my ($class, $conn) = @_;
187 73 ▶ my $delivering = $DELIVERING{ Scalar::Util::refaddr($conn) } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $delivering = $DELIVERING{builtin::refaddr($conn)} or return 0 17 56 73
188 17 ▶ return $delivering->pending_count > 1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$delivering->pending_count > 1 ? : 1 16 17
189 }
190  
191  
192 # The connections whose connect actions are being replayed right now, by
193 # refaddr: counted, so a frame that ends leaves the mark of one still running.
194 my %REPLAYING;
195  
196 38 ▶sub _replaying {
Subroutine _replaying: called CC=1 SCAR=0.0
197 38 my ($class, $conn) = @_;
198 38 my $key = Scalar::Util::refaddr($conn);
199 38 $REPLAYING{$key}++;
200 38 return bless \$key, 'DBIO::PostgreSQL::EV::Future::_Replay';
201 }
202  
203 38 ▶sub DBIO::PostgreSQL::EV::Future::_Replay::DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
204 38 my $key = ${ $_[0] };
205 38 ▶ delete $REPLAYING{$key} unless --$REPLAYING{$key} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless --$REPLAYING{$key} > 0 38 0 38
206 38 return;
207 }
208  
209 71 ▶sub _held_by_spawn {
Subroutine _held_by_spawn: called CC=2 SCAR=6.9
210 71 my ($class, $conn) = @_;
211 71 ▶ return $REPLAYING{ Scalar::Util::refaddr($conn) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$REPLAYING{builtin::refaddr($conn)} ? : 12 59 71
212 }
213  
214 73 1;
215  
216 __END__
« blib/lib/DBIO/PostgreSQL/EV/Pool.pm ↑ Summary blib/lib/DBIO/PostgreSQL/EV/TestHarness.pm »