| 1 | package DBIO::MySQL::EV::Pool; | ||||||||||||||||||||
| 2 | # ABSTRACT: EV::MariaDB connection pool for DBIO | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 9 | ▶ | use base 'DBIO::Storage::PoolBase'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | sub _create_connection { | ||||||||||||||||||
|
Subroutine
_create_connection: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 11 | 2 | my ($self, $conninfo) = @_; | |||||||||||||||||||
| 12 | 2 | ▶ | my %args = ref($conninfo) eq 'HASH' ? %$conninfo : ( conninfo => $conninfo ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | my $ready = $self->future_class->new; | |||||||||||||||||||
| 15 | 2 | my $on_error = $self->{on_error}; | |||||||||||||||||||
| 16 | 2 | my $conn; | |||||||||||||||||||
| 17 | $conn = $self->_new_ev_mariadb( | ||||||||||||||||||||
| 18 | %args, | ||||||||||||||||||||
| 19 | 1 | ▶ | on_connect => sub { $ready->done($conn) unless $ready->is_ready }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 1 | ▶ | on_error => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 21 | 1 | ▶ | $ready->fail($_[0]) unless $ready->is_ready; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 1 | $on_error->(@_); | |||||||||||||||||||
| 23 | }, | ||||||||||||||||||||
| 24 | 2 | ); | |||||||||||||||||||
| 25 | 2 | $self->_register_connection_ready($conn, $ready); | |||||||||||||||||||
| 26 | 2 | return $conn; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Construct the underlying EV::MariaDB handle. Isolated so the readiness | ||||||||||||||||||||
| 30 | # wiring in _create_connection can be unit-tested with a fake connection | ||||||||||||||||||||
| 31 | # (t/08), mirroring dbio-postgresql-ev's _new_ev_pg seam. | ||||||||||||||||||||
| 32 | 0 | ▶ | sub _new_ev_mariadb { | ||||||||||||||||||
|
Subroutine
_new_ev_mariadb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 33 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 34 | 0 | require EV::MariaDB; | |||||||||||||||||||
| 35 | 0 | return EV::MariaDB->new(%args); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 6 | ▶ | sub _connection_ready_future { | ||||||||||||||||||
|
Subroutine
_connection_ready_future: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 40 | 6 | my ($self, $conn) = @_; | |||||||||||||||||||
| 41 | 6 | ▶ | return $self->future_class->done($conn) if $conn->is_connected; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 3 | ▶ | return $self->_connection_ready_lookup($conn) || $self->future_class->done($conn); | ||||||||||||||||||
|
Condition33%1/3
$self->_connection_ready_lookup($conn) || $self->future_class->done($conn)
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 table33%1/3
A || B
A= $self->_connection_ready_lookup($conn)B= $self->future_class->done($conn)
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 3 | ▶ | sub _shutdown_connection { | ||||||||||||||||||
|
Subroutine
_shutdown_connection: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | 3 | my ($self, $mdb) = @_; | |||||||||||||||||||
| 48 | 3 | $mdb->close_async; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 9 | 1; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =pod | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =encoding UTF-8 | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 NAME | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | DBIO::MySQL::EV::Pool - EV::MariaDB connection pool for DBIO | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head1 VERSION | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | version 0.900001 | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | my $pool = DBIO::MySQL::EV::Pool->new( | ||||||||||||||||||||
| 70 | conninfo => { host => 'localhost', database => 'myapp', user => 'myapp' }, | ||||||||||||||||||||
| 71 | size => 10, | ||||||||||||||||||||
| 72 | on_error => sub { warn $_[0] }, | ||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | my $mdb = $pool->acquire; # get idle connection | ||||||||||||||||||||
| 76 | $pool->release($mdb); # return to pool | ||||||||||||||||||||
| 77 | my $mdb = $pool->acquire_txn; # pinned for transaction | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Connection pool for L<DBIO::MySQL::EV::Storage>. Manages a pool of | ||||||||||||||||||||
| 82 | L<EV::MariaDB> connections, dispatching queries to available connections | ||||||||||||||||||||
| 83 | and queuing when all are busy. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | The acquire / release / capacity / shutdown mechanics, including the | ||||||||||||||||||||
| 86 | connection-readiness gate, are inherited from L<DBIO::Storage::PoolBase>; | ||||||||||||||||||||
| 87 | this class supplies only the EV::MariaDB seam — see L</_create_connection>, | ||||||||||||||||||||
| 88 | L</_connection_ready_future> and L</_shutdown_connection>. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head1 METHODS | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head2 _create_connection | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | Builds one L<EV::MariaDB> connection from the (already-transformed) | ||||||||||||||||||||
| 95 | connect info. Accepts a hashref of L<EV::MariaDB> named parameters or | ||||||||||||||||||||
| 96 | a plain string (passed as the C<conninfo> argument). The pool tracks | ||||||||||||||||||||
| 97 | the returned connection — do not push it onto L</_connections> | ||||||||||||||||||||
| 98 | yourself. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Wires the connection's readiness Future (see L</_connection_ready_future>): | ||||||||||||||||||||
| 101 | C<on_connect> resolves it with the connection; an error before connect | ||||||||||||||||||||
| 102 | fails it, so a dependent L<DBIO::Storage::PoolBase/acquire> Future fails | ||||||||||||||||||||
| 103 | rather than hanging (karr #20). | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 _connection_ready_future | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Overrides L<DBIO::Storage::PoolBase/_connection_ready_future>: the resolved | ||||||||||||||||||||
| 108 | Future does not complete until the connection is actually ready for queries. | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | C<EV::MariaDB-E<gt>new> returns before its async connect finishes, so the | ||||||||||||||||||||
| 111 | base's default (immediately-C<done>) seam would hand back a brand-new | ||||||||||||||||||||
| 112 | handle whose C<on_connect> has not fired — the very first bound query on a | ||||||||||||||||||||
| 113 | cold pool then throws C<not connected> (karr #20; this pool previously | ||||||||||||||||||||
| 114 | wired C<on_connect =E<gt> sub {}>, a pure no-op, with no readiness tracking | ||||||||||||||||||||
| 115 | at all). We look up the per-connection readiness Future registered in | ||||||||||||||||||||
| 116 | L</_create_connection> via L<DBIO::Storage::PoolBase/_connection_ready_lookup>, | ||||||||||||||||||||
| 117 | falling back to the base's immediately-C<done> seam for an already-connected | ||||||||||||||||||||
| 118 | (idle-reused) connection. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head2 _shutdown_connection | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Closes one L<EV::MariaDB> connection during L<DBIO::Storage::PoolBase/shutdown>. | ||||||||||||||||||||
| 123 | Errors are swallowed by the caller. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | DBIO Authors | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 134 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||