AnyEvent-Pg

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.11  Jul 2, 2013
       - add support for priorizing queries
       - add initialization queries

0.10  Mar 25, 2013
       - add support for set method that allows to change pool
         parameters after creation
       - resolve some problems with Test::postgresql

0.09  Mar 21, 2013
       - add support for global_timeout feature

0.08  Feb 27, 2013
        - AnyEvent::Pg objects were not calling on_empty_queue after
          stablishing the connection
        - more tests added

0.07  Feb 25, 2013
        - remove dependency on unused Devel::FindRef 

0.06  Feb 20, 2013

lib/AnyEvent/Pg/Pool.pm  view on Meta::CPAN

                timeout => 30,
                size => 1 );

sub new {
    my ($class, $conninfo, %opts) = @_;
    $conninfo = { %$conninfo } if ref $conninfo;
    my $size = delete $opts{size} // $default{size};
    my $connection_retries = delete $opts{connection_retries} // $default{connection_retries};
    my $connection_delay = delete $opts{connection_delay} // $default{connection_delay};
    my $timeout = delete $opts{timeout} // $default{timeout};
    my $global_timeout = delete $opts{global_timeout};
    my $on_error = delete $opts{on_error} ;
    my $on_connect_error = delete $opts{on_connect_error};
    my $on_transient_error = delete $opts{on_transient_error};
    # my $on_empty_queue = delete $opts{on_empty_queue};
    my $pool = { conninfo => $conninfo,
                 size => $size,
                 on_error => $on_error,
                 on_connect_error => $on_connect_error,
                 on_transient_error => $on_transient_error,
                 # on_empty_queue => $on_empty_queue,
                 timeout => $timeout,
                 max_conn_retries => $connection_retries,
                 conn_retries => 0,
                 conn_delay => $connection_delay,
                 global_timeout => $global_timeout,
                 conns => {},
                 current => {},
                 busy => {},
                 idle => {},
                 connecting => {},
                 initializing => {},
                 init_queue_ix => {},
                 queue => [],
                 seq => 1,
                 query_seq => 1,

lib/AnyEvent/Pg/Pool.pm  view on Meta::CPAN

    $pool;
}

sub is_dead { shift->{dead} }

sub set {
    my $pool = shift;
    while (@_) {
        my $k = shift;
        my $v = shift // $default{$k};
        if ($k eq 'global_timeout') {
            if (defined (my $gt = shift)) {
                $pool->{max_conn_time} += $gt - $pool->{global_timeout}
                    if defined $pool->{max_conn_time};
            }
            else {
                delete $pool->{max_conn_time};
            }
            $pool->{$k} = $v;
        }
    }
}

lib/AnyEvent/Pg/Pool.pm  view on Meta::CPAN

        return;
    }

    my $now = time;
    # This failed connection is not counted against the limit
    # unless it is the only connection remaining. Effectively the
    # module will keep going until all the connections become
    # broken and no more connections can be established.
    unless (keys(%{$pool->{conns}}) > 1) {
        $pool->{conn_retries}++;
        if ($pool->{global_timeout}) {
            $pool->{max_conn_time} ||= $now + $pool->{global_timeout} - $pool->{conn_delay};
        }
    }

    if ($pool->{conn_retries} <= $pool->{max_conn_retries}) {
        if (not $pool->{max_conn_time} or $pool->{max_conn_time} >= $now) {
            $debug and $debug & 8 and $pool->_debug("starting timer for delayed reconnection $pool->{conn_delay}s");
            $pool->{delay_watcher} = AE::timer $pool->{conn_delay}, 0, weak_method_callback($pool, '_on_delayed_reconnect');
            return
        }
        $debug and $debug & 8 and $pool->_debug("global_timeout expired");
    }

    # giving up!
    $debug and $debug & 8 and $pool->_debug("it has been impossible to connect to the database, giving up!!!");
    $pool->{dead} = 1;

    # processing continues on the on_conn_error callback
}

sub _on_fatal_connect_error {

lib/AnyEvent/Pg/Pool.pm  view on Meta::CPAN


When establishing a new connection fails, this setting allows to
configure the number of seconds to delay before trying to connect
again.

=item timeout => $seconds

When some active connection does not report activity for the given
number of seconds, it is considered dead and closed.

=item global_timeout => $seconds

When all the connections to the database become broken and it is not
possible to establish a new connection for the given time period the
pool is considered dead and the C<on_error> callback will be called.

Note that this timeout is approximate. It is checked every time a new
connection attempt fails but its expiration will not cause the
abortion of an in-progress connection.

=item on_error => $callback

t/AnyEvent-Pg.t  view on Meta::CPAN

undef $pg;
undef @w;

##############################
#
# Pool tests:
#

$cv = AnyEvent->condvar;

my $global_timeout = 10;
my $timeout        =  1;
my $delay          =  2;
my $max_ok = $global_timeout + $timeout * 2 + $delay + 3;
my $min_ok = $global_timeout - $delay;

my $pool = AnyEvent::Pg::Pool->new({host   => 'localhost',
                                    port   => $port + 123,
                                    dbname => 'rominadb',
                                    user   => 'albano'},
                                   global_timeout     => $global_timeout,
                                   timeout            => $timeout,
                                   connection_retries => 1000,
                                   on_connect_error   => sub { $cv->send });

# Pool object would not try to connect unless some query is queued
push @w, $pool->push_query(query => "select now()");

my $start = time;
$timer = AE::timer $max_ok + 2, 0, sub {
    diag("timer callback called!");
    $cv->send
};

$cv->recv;
my $elapsed = time - $start;

ok($elapsed >= $min_ok, "retried for the given time")
    or diag ("min_ok: $min_ok, elapsed: $elapsed");
ok($elapsed <= $max_ok, "connection aborted after the given global_timeout")
    or diag ("max_ok: $max_ok, elapsed: $elapsed");

undef $pool;
undef @w;



( run in 0.688 second using v1.01-cache-2.11-cpan-49f99fa48dc )