| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package DBIx::Poggy; | ||
| 6 | our $VERSION = '0.04'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | DBIx::Poggy - async Pg with AnyEvent and Promises | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use DBIx::Poggy; | ||
| 18 | my $pool = DBIx::Poggy->new( pool_size => 5 ); | ||
| 19 | $pool->connect('dbi:Pg:db=test', 'root', 'password'); | ||
| 20 | |||
| 21 | use AnyEvent; | ||
| 22 | my $cv = AnyEvent->condvar; | ||
| 23 | |||
| 24 | my $res; | ||
| 25 | $pool->take->selectrow_arrayref( | ||
| 26 | 'SELECT * FROM users WHERE name = ?', {}, 'ruz' | ||
| 27 | ) | ||
| 28 | ->then(sub { | ||
| 29 | my $user = $res->{user} = shift; | ||
| 30 | |||
| 31 | return $pool->take->selectall_arrayref( | ||
| 32 | 'SELECT * FROM friends WHERE user_id = ?', undef, $user->{id} | ||
| 33 | ); | ||
| 34 | }) | ||
| 35 | ->then(sub { | ||
| 36 | my $friends = $res->{friends} = shift; | ||
| 37 | ... | ||
| 38 | }) | ||
| 39 | ->catch(sub { | ||
| 40 | my $error = shift; | ||
| 41 | die $error; | ||
| 42 | }) | ||
| 43 | ->finally(sub { | ||
| 44 | $cv->send( $res ); | ||
| 45 | }); | ||
| 46 | |||
| 47 | $cv->recv; | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | "Async" postgres as much as L<DBD::Pg> allows with L<Promises> instead of callbacks. | ||
| 52 | |||
| 53 | You get DBI interface you used to that returns promises, connections pool, queries | ||
| 54 | queuing and support of transactions. | ||
| 55 | |||
| 56 | =head2 Why pool? | ||
| 57 | |||
| 58 | DBD::Pg is not async, it's non blocking. Every connection can execute only one query | ||
| 59 | at a moment, so to execute several queries in parallel you need several connections. | ||
| 60 | What you get is you can do something in Perl side while postgres crunches data for | ||
| 61 | you. | ||
| 62 | |||
| 63 | =head2 Queue | ||
| 64 | |||
| 65 | Usually if you attempt to run two queries on the same connection then DBI throws an | ||
| 66 | error about active query. Poggy takes care of that by queuing up queries you run on | ||
| 67 | one connection. Handy for transactions and pool doesn't grow too much. | ||
| 68 | |||
| 69 | =head2 What is async here then? | ||
| 70 | |||
| 71 | Only a queries on multiple connections, so if you need to execute many parallel | ||
| 72 | queries then you need many connections. pg_bouncer and haproxy are your friends. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | 1 | ▶ | use DBIx::Poggy::DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 77 | use DBIx::Poggy::Error; | ||
| 78 | |||
| 79 | =head1 METHODS | ||
| 80 | |||
| 81 | =head2 new | ||
| 82 | |||
| 83 | Named arguments: | ||
| 84 | |||
| 85 | =over 4 | ||
| 86 | |||
| 87 | =item pool_size | ||
| 88 | |||
| 89 | number of connections to create, creates one more in case all are busy | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | Returns a new pool object. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub new { | ||
| 98 | my $proto = shift; | ||
| 99 | my $self = bless { @_ }, ref($proto) || $proto; | ||
| 100 | return $self->init; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub init { | ||
| 104 | my $self = shift; | ||
| 105 | $self->{pool_size} ||= 10; | ||
| 106 | return $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 connect | ||
| 110 | |||
| 111 | Takes the same arguments as L<DBI/connect>, opens "pool_size" connections. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub connect { | ||
| 116 | my $self = shift; | ||
| 117 | my ($dsn, $user, $password, $opts) = @_; | ||
| 118 | |||
| 119 | $opts ||= {}; | ||
| 120 | |||
| 121 | $self->{free} ||= []; | ||
| 122 | |||
| 123 | $self->{connection_settings} = [ $dsn, $user, $password, $opts ]; | ||
| 124 | |||
| 125 | $self->_connect for 1 .. $self->{pool_size}; | ||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _connect { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | push @{ $self->{free} }, DBIx::Poggy::DBI->connect( | ||
| 133 | @{ $self->{connection_settings} } | ||
| 134 | ) or die DBIx::Poggy::Error->new( 'DBIx::Poggy::DBI' ); | ||
| 135 | |||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 take | ||
| 140 | |||
| 141 | Gives one connection from the pool. Takes arguments: | ||
| 142 | |||
| 143 | =over 4 | ||
| 144 | |||
| 145 | =item auto | ||
| 146 | |||
| 147 | Connection will be released to the pool after transaction or | ||
| 148 | as soon as query queue becomes empty. True by default. | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | Returns L<DBIx::Poggy::DBI> handle. When "auto" is turned off | ||
| 153 | then in list context returns also guard object that will L</release> | ||
| 154 | handle to the pool on destruction. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub take { | ||
| 159 | my $self = shift; | ||
| 160 | my (%args) = (auto => 1, @_); | ||
| 161 | unless ( $self->{free} ) { | ||
| 162 | die DBIx::Poggy::Error->new( | ||
| 163 | err => 666, | ||
| 164 | errstr => 'Attempt to take a connection from not initialized pool', | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | unless ( @{ $self->{free} } ) { | ||
| 168 | warn "DB pool exhausted, creating a new connection"; | ||
| 169 | $self->_connect; | ||
| 170 | } | ||
| 171 | |||
| 172 | my $dbh = shift @{ $self->{free} }; | ||
| 173 | if ( $args{auto} ) { | ||
| 174 | $dbh->{private_poggy_state}{release_to} = $self; | ||
| 175 | return $dbh; | ||
| 176 | } | ||
| 177 | return $dbh unless wantarray; | ||
| 178 | return ( $dbh, guard { $self->release($dbh) } ); | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 release | ||
| 182 | |||
| 183 | Takes a handle as argument and puts it back into the pool. At the moment, | ||
| 184 | no protection against double putting or active queries on the handle. | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub release { | ||
| 189 | my $self = shift; | ||
| 190 | push @{ $self->{free} }, shift; | ||
| 191 | return $self; | ||
| 192 | } | ||
| 193 | |||
| 194 | =head2 AUTHOR | ||
| 195 | |||
| 196 | Ruslan U. Zakirov E<lt>Ruslan.Zakirov@gmail.comE<gt> | ||
| 197 | |||
| 198 | =head2 LICENSE | ||
| 199 | |||
| 200 | Under the same terms as perl itself. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | 1; | ||