| 1 | package DBIO::MySQL::EV::Future; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A Future whose get waits on the EV loop | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 60 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 60 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 60 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 60 | ▶ | use base 'Future'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 60 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 60 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 60 | ▶ | use EV (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 251 | ▶ | sub await { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
await: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 251 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 251 | ▶ | until ($self->is_ready) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Failing the queued caller this waits for settles this Future, unless its | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # chain catches the failure. Asked again when the loop has run dry: the | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # answer to the last statement in flight is what makes the wait hopeless. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 954 | ▶ | next if $self->_refuse_queued; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 947 | ▶ | EV::run(EV::RUN_ONCE()) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
&EV::run(2) or $self->is_ready or $self->_refuse_queued
&EV::run(2) or $self->is_ready
MC/DC66%2/3
&EV::run(2) or $self->is_ready or $self->_refuse_queued
&EV::run(2)
$self->is_ready
$self->_refuse_queued
Truth table75%3/4
A or B or C
A= &EV::run(2)B= $self->is_readyC= $self->_refuse_queued
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | or $self->is_ready | ||||||||||||||||||||||||||||||||||||||||||
| 25 | or $self->_refuse_queued | ||||||||||||||||||||||||||||||||||||||||||
| 26 | or $self->_dry_loop; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 249 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # The loop is dry and nothing settled the Future: it dies, and a caller queued | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # for a connection that this Future waits for is taken out of the queue with | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # it (karr k69). Left queued, the next release would hand a connection to a | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # caller nobody waits for any more, and the pool would lose it. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | sub _dry_loop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dry_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | my $msg = "$self is pending and the EV loop has nothing left that could complete it"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | $self->_drop_queued($msg); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | Carp::croak($msg); | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # The mark is kept as Future's user data (udata, Future 0.49), never in the | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # instance itself: [ $waiter, $pool ], both weak -- the pool holds the waiter, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # and the waiter and everything chained off it share the one arrayref. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 60 | my $QUEUED = 'dbio_mysql_ev_queued'; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 6855 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6855 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 6855 | my $self = $proto->SUPER::new(@_); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 6855 | ▶ | if (ref $proto and my $queued = $proto->udata($QUEUED)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $proto and my $queued = $proto->udata($QUEUED)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 84 | ▶ | $self->set_udata($QUEUED => $queued) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$queued->[0] and not $queued->[0]->is_ready
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
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | if $queued->[0] && !$queued->[0]->is_ready; | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 6855 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 46 | ▶ | sub _queued_in { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_queued_in: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 46 | my ($self, $pool) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 46 | my $queued = [ $self, $pool ]; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 46 | Scalar::Util::weaken($_) for @$queued; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 46 | return $self->set_udata($QUEUED => $queued); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # Refuse the wait when this Future waits for a caller queued for a pooled | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # connection, and no connection of that pool can come back before a callback | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # that is running returns: take the caller out of the queue and fail it. True | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # when it did. Nothing else is touched -- a queued caller nobody waits for | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # stays queued, and is served once those callbacks have returned. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 958 | ▶ | sub _refuse_queued { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse_queued: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 958 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 958 | ▶ | my $queued = $self->udata($QUEUED) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 127 | my ($waiter, $pool) = @$queued; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 127 | ▶ | return 0 unless $waiter && !$waiter->is_ready && $pool; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$waiter and not $waiter->is_ready and $pool
$waiter and not $waiter->is_ready
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
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 81 | ▶ | return 0 unless $pool->_mysql_held_by_callbacks; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | return 0 unless $pool->_withdraw_waiter($waiter); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 8 | $waiter->fail(_queued_refusal($pool->max_size)); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # Take the caller queued for a connection that this Future waits for out of the | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # pool's queue and fail it with $reason, for a wait that dies instead of | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # ending. True when there was one. Unlike _refuse_queued it asks nothing of the | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # pool's state: the wait is hopeless already. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub _drop_queued { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drop_queued: called CC=6 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | my ($self, $reason) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | my $queued = $self->udata($QUEUED) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | my ($waiter, $pool) = @$queued; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | return 0 unless $waiter && !$waiter->is_ready && $pool; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$waiter and not $waiter->is_ready and $pool
$waiter and not $waiter->is_ready
MC/DC0%0/3
$waiter and not $waiter->is_ready and $pool
$waiter
not $waiter->is_ready
$pool
Truth table25%1/4
A and not(B) and C
A= $waiterB= not $waiter->is_readyC= $pool
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | return 0 unless $pool->_withdraw_waiter($waiter); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | $waiter->fail($reason); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 8 | ▶ | sub _queued_refusal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_queued_refusal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | my ($size) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | return 'Blocking wait refused: this blocking call is queued for a connection ' | |||||||||||||||||||||||||||||||||||||||||
| 99 | . "of a pool of $size, and every one of them is held by a callback this " | ||||||||||||||||||||||||||||||||||||||||||
| 100 | . 'call runs inside -- the coderef of a txn_do, or a then on a statement of ' | ||||||||||||||||||||||||||||||||||||||||||
| 101 | . 'a transaction -- which gives its connection back only after it has ' | ||||||||||||||||||||||||||||||||||||||||||
| 102 | . 'returned, so waiting here would deadlock. To run on the connection of ' | ||||||||||||||||||||||||||||||||||||||||||
| 103 | . 'the transaction, use its context: $ctx->select_async, ' | ||||||||||||||||||||||||||||||||||||||||||
| 104 | . '$ctx->insert_async, ..., chained with ->then. For a statement outside ' | ||||||||||||||||||||||||||||||||||||||||||
| 105 | . 'the transaction, use the *_async methods of the storage and chain with ' | ||||||||||||||||||||||||||||||||||||||||||
| 106 | . '->then instead of blocking, or raise pool_size'; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 60 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||||||||||||