| 1 | package Database::Async::Pool; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '0.019'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Database::Async::Pool - connection manager for L<Database::Async> | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Database::Async::Backoff::Exponential; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Database::Async::Backoff::None; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use Future::AsyncAwait qw(:experimental); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use List::UtilsBy qw(extract_by); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 6 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 30 | 6 | my $backoff = delete $args{backoff}; | |||||||||||||||||||||||||||
| 31 | 6 | ▶ | unless(blessed $backoff) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 6 | my $type = 'exponential'; | |||||||||||||||||||||||||||
| 33 | 6 | ▶ | $type = $backoff if $backoff and not ref $backoff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$backoff and not ref $backoff
MC/DC50%1/2
$backoff and not ref $backoff
$backoff
not ref $backoff
Truth table66%2/3
A and not(B)
A= $backoffB= not ref $backoff
| |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | $backoff = Database::Async::Backoff->instantiate( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$backoff and ref $backoff
MC/DC0%0/2
$backoff and ref $backoff
$backoff
ref $backoff
Truth table66%2/3
A and B
A= $backoffB= ref $backoff
| |||||||||||||||||||||||||||||
| 35 | type => $type, | ||||||||||||||||||||||||||||
| 36 | initial_delay => 0.010, | ||||||||||||||||||||||||||||
| 37 | max_delay => 30, | ||||||||||||||||||||||||||||
| 38 | ($backoff && ref($backoff) ? %$backoff : ()) | ||||||||||||||||||||||||||||
| 39 | ) | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | bless { | ||||||||||||||||||||||||||||
| 42 | 6 | pending_count => 0, | |||||||||||||||||||||||||||
| 43 | count => 0, | ||||||||||||||||||||||||||||
| 44 | min => 0, | ||||||||||||||||||||||||||||
| 45 | max => 1, | ||||||||||||||||||||||||||||
| 46 | attempts => undef, | ||||||||||||||||||||||||||||
| 47 | ordering => 'serial', | ||||||||||||||||||||||||||||
| 48 | backoff => $backoff, | ||||||||||||||||||||||||||||
| 49 | waiting => [], | ||||||||||||||||||||||||||||
| 50 | ready => [], | ||||||||||||||||||||||||||||
| 51 | %args | ||||||||||||||||||||||||||||
| 52 | }, $class | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub min { shift->{min} } | ||||||||||||||||||||||||||
|
Subroutine
min: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | sub max { shift->{max} } | ||||||||||||||||||||||||||
|
Subroutine
max: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 7 | ▶ | sub count { shift->{count} } | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | sub pending_count { shift->{pending_count} } | ||||||||||||||||||||||||||
|
Subroutine
pending_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 5 | ▶ | sub backoff { shift->{backoff} } | ||||||||||||||||||||||||||
|
Subroutine
backoff: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub register_engine { | ||||||||||||||||||||||||||
|
Subroutine
register_engine: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 2 | my ($self, $engine) = @_; | |||||||||||||||||||||||||||
| 63 | 2 | --$self->{pending_count}; | |||||||||||||||||||||||||||
| 64 | 2 | ++$self->{count}; | |||||||||||||||||||||||||||
| 65 | 2 | $self | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub unregister_engine { | ||||||||||||||||||||||||||
|
Subroutine
unregister_engine: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $engine) = @_; | |||||||||||||||||||||||||||
| 70 | try { | ||||||||||||||||||||||||||||
| 71 | 0 | $log->tracef('Engine is removed from the pool, with %d in the queue', 0 + @{$self->{waiting}}); | |||||||||||||||||||||||||||
| 72 | 0 | my $addr = refaddr($engine); | |||||||||||||||||||||||||||
| 73 | # This engine may have been actively processing a request, and not in the pool: | ||||||||||||||||||||||||||||
| 74 | # that's fine, we only remove if we had it. | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $count = () = extract_by { refaddr($_) == $addr } @{$self->{ready}}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 0 | $log->tracef('Removed %d engine instances from the ready pool', $count); | |||||||||||||||||||||||||||
| 77 | # Any engine that wasn't in the ready queue (`count`) was out on assignment | ||||||||||||||||||||||||||||
| 78 | # and thus included in `pending_count` | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | --$self->{$count ? 'count' : 'pending_count'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $log->infof('After cleanup we have %d count, %d pending, %d waiting', $self->{count}, $self->{pending_count}, 0 + @{$self->{waiting}}); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $self->process_pending->retain if @{$self->{waiting}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | } catch ($e) { | |||||||||||||||||||||||||||
| 83 | 0 | $log->errorf('Failed %s', $e); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | $self | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 queue_ready_engine | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Called when there's a spare engine we can put back in the pool. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub queue_ready_engine { | ||||||||||||||||||||||||||
|
Subroutine
queue_ready_engine: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 1 | my ($self, $engine) = @_; | |||||||||||||||||||||||||||
| 96 | 1 | $log->tracef('Engine is now ready, with %d in the queue', 0 + @{$self->{waiting}}); | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | return $self->notify_engine($engine) if @{$self->{waiting}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | push @{$self->{ready}}, $engine; | |||||||||||||||||||||||||||
| 99 | 1 | $self | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 notify_engine | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | We call this internally to hand an engine over to the next | ||||||||||||||||||||||||||||
| 105 | waiting request. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub notify_engine { | ||||||||||||||||||||||||||
|
Subroutine
notify_engine: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self, $engine) = @_; | |||||||||||||||||||||||||||
| 111 | die 'unable to notify, we have no pending requests' | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | unless my $f = shift @{$self->{waiting}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $f->done($engine); | |||||||||||||||||||||||||||
| 114 | 0 | return $self; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 next_engine | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Resolves to an engine. May need to wait if there are none available. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | async sub next_engine { | ||||||||||||||||||||||||||
|
Subroutine
next_engine: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | 2 | $log->tracef('Have %d ready engines to use', 0 + @{$self->{ready}}); | |||||||||||||||||||||||||||
| 126 | 2 | ▶ | if(my $engine = shift @{$self->{ready}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 1 | return $engine; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 1 | push @{$self->{waiting}}, my $f = $self->new_future; | |||||||||||||||||||||||||||
| 130 | 1 | await $self->process_pending; | |||||||||||||||||||||||||||
| 131 | 1 | return await $f; | |||||||||||||||||||||||||||
| 132 | 5 | } | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | async sub process_pending { | ||||||||||||||||||||||||||
|
Subroutine
process_pending: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 1 | my $total = $self->count + $self->pending_count; | |||||||||||||||||||||||||||
| 137 | 1 | $log->tracef('Might request, current count is %d/%d (%d pending, %d active)', $total, $self->max, $self->pending_count, $self->count); | |||||||||||||||||||||||||||
| 138 | 1 | ▶ | await $self->request_engine unless $total >= $self->max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | return; | |||||||||||||||||||||||||||
| 140 | 5 | } | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | sub new_future { | ||||||||||||||||||||||||||
|
Subroutine
new_future: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 1 | my ($self, $label) = @_; | |||||||||||||||||||||||||||
| 144 | ( | ||||||||||||||||||||||||||||
| 145 | 1 | ▶ | $self->{new_future} //= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | 1 | Future->new->set_label($_[1]) | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 1 | ▶ | )->($label) | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"new_future"} //= sub {
"Future"->new->set_label($_[1]);
}
MC/DC0%0/2
$self->{"new_future"} //= sub {
"Future"->new->set_label($_[1]);
}
$self->{"new_future"}
sub {
"Future"->new->set_label($_[1]);
}
Truth table33%1/3
A //= B
A= $self->{"new_future"}B= sub {
"Future"->new->set_label($_[1]);
}
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | async sub request_engine { | ||||||||||||||||||||||||||
|
Subroutine
request_engine: called CC=2 SCAR=7.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 153 | 1 | $log->tracef('Pool requesting new engine'); | |||||||||||||||||||||||||||
| 154 | 1 | ++$self->{pending_count}; | |||||||||||||||||||||||||||
| 155 | 1 | my $delay = $self->backoff->next; | |||||||||||||||||||||||||||
| 156 | 1 | ▶ | if($delay) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | my $f = $self->loop->delay_future( | |||||||||||||||||||||||||||
| 158 | after => $delay | ||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | CANCEL { $f->cancel } | ||||||||||||||||||||||||||||
| 161 | 0 | await $f; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 1 | my $req = $self->{request_engine}->(); | |||||||||||||||||||||||||||
| 164 | CANCEL { $req->cancel } | ||||||||||||||||||||||||||||
| 165 | 1 | await $req; | |||||||||||||||||||||||||||
| 166 | 1 | $self->backoff->reset; | |||||||||||||||||||||||||||
| 167 | 5 | } | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | ▶ | sub _remove_from_loop { | ||||||||||||||||||||||||||
|
Subroutine
_remove_from_loop: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 170 | 2 | my ($self, $loop) = @_; | |||||||||||||||||||||||||||
| 171 | 2 | $_->cancel for splice @{$self->{waiting}}; | |||||||||||||||||||||||||||
| 172 | 2 | $self->unregister_engine($_) for splice @{$self->{ready}}; | |||||||||||||||||||||||||||
| 173 | 2 | return $self->next::method($loop); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Tom Molesworth C<< <TEAM@cpan.org> >> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Copyright Tom Molesworth 2011-2023. Licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||