Async/Pool.pm

filter: Statementstmt 100% 45 / 45 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 92.9% 66 / 71 CC 6 SCAR 5.0
CC
6
Cov
91%
new
3.1
_await_conn_ready
2.0
_create_connection
2.0
CRAP 1.7
« Async/Storage.pm ↑ Summary Async.pm »
1 package DBIO::Async::Pool;
2 # ABSTRACT: Generic async connection pool for DBIO drivers
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use base 'DBIO::Storage::PoolBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
17 8 my ($class, %args) = @_;
18 8 my $storage = delete $args{storage};
19 8 ▶ croak('storage required') unless $storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage 0 8 8
20  
21 8 my $self = $class->SUPER::new(%args);
22 8 $self->{storage} = $storage;
23 8 ▶ Scalar::Util::weaken($self->{storage}) if ref $self->{storage};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"storage"} 8 0 8
24 8 return $self;
25 }
26  
27  
28 12 ▶sub acquire {
Subroutine acquire: called CC=1 SCAR=0.0
Pod documented
29 12 my $self = shift;
30 12 ▶ return $self->SUPER::acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 12 my $conn = shift;
32 12 return $self->_await_conn_ready($conn);
33 12 });
34 }
35  
36  
37 12 ▶sub _await_conn_ready {
Subroutine _await_conn_ready: called CC=2 SCAR=7.1
38 12 my ($self, $conn) = @_;
39 12 ▶ return $self->future_class->done($conn) unless $self->{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"storage"} 0 12 12
40 12 return $self->{storage}->_await_conn_ready($conn);
41 }
42  
43  
44 10 ▶sub _create_connection {
Subroutine _create_connection: called CC=2 SCAR=7.1
45 10 my ($self, $conninfo) = @_;
46 10 ▶ croak 'Storage reference lost; cannot create connection' unless $self->{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"storage"} 0 10 10
47 10 return $self->{storage}->_create_pool_connection($conninfo);
48 }
49  
50  
51 10 ▶sub _shutdown_connection {
Subroutine _shutdown_connection: called CC=2 SCAR=7.1
52 10 my ($self, $conn) = @_;
53 10 ▶ return unless $self->{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"storage"} 0 10 10
54 10 $self->{storage}->_shutdown_pool_connection($conn);
55 }
56  
57 8 1;
58  
59 __END__
60  
61 =pod
62  
63 =encoding UTF-8
64  
65 =head1 NAME
66  
67 DBIO::Async::Pool - Generic async connection pool for DBIO drivers
68  
69 =head1 VERSION
70  
71 version 0.900001
72  
73 =head1 SYNOPSIS
74  
75 my $pool = DBIO::Async::Pool->new(
76 storage => $storage,
77 conninfo => { host => 'localhost', dbname => 'myapp' },
78 size => 10,
79 on_error => sub { warn $_[0] },
80 );
81  
82 my $conn = $pool->acquire->get; # Future resolving to ready connection
83 $pool->release($conn); # return to pool
84 my $conn = $pool->acquire_txn; # pinned for transaction
85  
86 =head1 DESCRIPTION
87  
88 Connection pool for L<DBIO::Async::Storage>. Manages a pool of database
89 connections using the idle-list / waiter-queue / capacity mechanics
90 inherited from L<DBIO::Storage::PoolBase>.
91  
92 This class supplies two additions over PoolBase:
93  
94 =over 4
95  
96 =item * Readiness gating on L</acquire> -- the resolved Future does not
97 complete until the connection is actually ready for queries, via
98 L</_await_conn_ready>.
99  
100 =item * Delegation of connection lifecycle to the owning Storage via
101 L</_create_connection> and L</_shutdown_connection>, which call back to
102 L<DBIO::Async::Storage/_create_pool_connection> and
103 L<DBIO::Async::Storage/_shutdown_pool_connection>.
104  
105 =back
106  
107 Drivers that need a different pool implementation can subclass this or
108 provide their own L<DBIO::Storage::PoolBase> subclass.
109  
110 =head1 METHODS
111  
112 =head2 new
113  
114 my $pool = DBIO::Async::Pool->new(
115 storage => $storage, # required
116 conninfo => $conninfo, # or conninfo_provider
117 size => 10,
118 on_error => sub { warn $_[0] },
119 );
120  
121 Like L<DBIO::Storage::PoolBase/new>, but requires a C<storage> argument
122 (the L<DBIO::Async::Storage> that owns this pool). The storage reference
123 is weakened to avoid a cycle (storage holds pool, pool holds storage).
124  
125 =head2 acquire
126  
127 Like L<DBIO::Storage::PoolBase/acquire>, but the resolved Future does not
128 complete until the connection is actually ready for queries. This
129 correctly handles drivers where connection construction returns before
130 the async connect finishes (e.g. libpq's C<PQconnectStart>).
131  
132 Readiness is checked via L</_await_conn_ready>, which delegates to the
133 Storage's L<DBIO::Async::Storage/_await_conn_ready>.
134  
135 =head2 _await_conn_ready
136  
137 my $future = $pool->_await_conn_ready($conn);
138  
139 Returns a Future that resolves to C<$conn> once it is ready for queries.
140 Delegates to the Storage's L<DBIO::Async::Storage/_await_conn_ready>.
141 If the Storage reference has been lost (DESTROY path), short-circuits
142 to an immediately-done Future.
143  
144 =head2 _create_connection
145  
146 Delegates to the owning Storage's
147 L<DBIO::Async::Storage/_create_pool_connection>. The pool tracks the
148 returned connection -- do not push it onto C<_connections> yourself.
149  
150 =head2 _shutdown_connection
151  
152 Delegates to the owning Storage's
153 L<DBIO::Async::Storage/_shutdown_pool_connection>. If the Storage
154 reference has been lost, returns silently (best-effort on DESTROY).
155  
156 =head1 AUTHOR
157  
158 DBIO Authors
159  
160 =head1 COPYRIGHT AND LICENSE
161  
162 Copyright (C) 2026 DBIO Authors
163  
164 This is free software; you can redistribute it and/or modify it under
165 the same terms as the Perl 5 programming language system itself.
166  
167 =cut
« Async/Storage.pm ↑ Summary Async.pm »