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