AnyEvent-Pg
view release on metacpan or search on metacpan
lib/AnyEvent/Pg.pm view on Meta::CPAN
$self->{read_watcher} = $rw;
$self->{write_watcher} = $ww;
# warn "read_watcher: $rw, write_watcher: $ww";
if ($goto) {
delete $self->{timeout_watcher};
$debug and $debug & 1 and $self->_debug("goto $goto");
$self->$goto;
}
elsif ($self->{timeout}) {
$self->{timeout_watcher} = AE::timer $self->{timeout}, 0, weak_method_callback_cached($self, '_connectPollTimeout');
}
}
sub _connectPollTimeout {
my $self = shift;
$debug and $debug & 2 and $self->_debug("_connectPoll timed out");
delete @{$self}{qw(timeout_watcher read_watcher write_watcher)};
$self->{timedout} = 1;
$self->_on_connect_error;
}
lib/AnyEvent/Pg.pm view on Meta::CPAN
my $queries = $self->{queries};
if ($self->{state} eq 'connected') {
while (@$queries) {
if ($queries->[0]{canceled}) {
$debug and $debug & 2 and $self->_debug("the query at the head of the queue was canceled, looking again!");
shift @$queries;
next;
}
$debug and $debug & 1 and $self->_debug("want to write query");
$self->{write_watcher} = AE::io $self->{fd}, 1, weak_method_callback_cached($self, '_on_push_query_writable');
$self->{timeout_watcher} = AE::timer $self->{timeout}, 0, weak_method_callback_cached($self, '_on_timeout')
if $self->{timeout};
return;
}
if (delete $self->{call_on_empty_queue}) {
# This sub may be called repeatly from calls stacked by
# AE::postponed, so we don't call the 'on_empty_queue'
# callback unless this (ugly) flag is set
$self->_maybe_callback('on_empty_queue');
}
lib/AnyEvent/Pg.pm view on Meta::CPAN
if ($flush == -1) {
$self->_on_fatal_error;
}
elsif ($flush == 0) {
$debug and $debug & 1 and $self->_debug("flushed");
$self->_on_consume_input;
}
elsif ($flush == 1) {
$debug and $debug & 1 and $self->_debug("wants to write");
$self->{write_watcher} = $ww // AE::io $self->{fd}, 1, weak_method_callback_cached($self, '_on_push_query_flushable');
$self->{timeout_watcher} = AE::timer $self->{timeout}, 0, weak_method_callback_cached($self, '_on_timeout')
if $self->{timeout};
}
else {
die "internal error: flush returned $flush";
}
}
sub _on_consume_input {
my $self = shift;
my $dbc = $self->{dbc};
lib/AnyEvent/Pg.pm view on Meta::CPAN
$debug and $debug & 2 and $self->_debug("notify recived: @notify");
$self->_maybe_callback(on_notify => @notify);
}
if (defined (my $cq = $self->{current_query})) {
while (1) {
if ($self->{write_watcher} or $dbc->busy) {
$debug and $debug & 1 and $self->_debug($self->{write_watcher}
? "wants to write and read"
: "wants to read");
$self->{timeout_watcher} = AE::timer $self->{timeout}, 0, weak_method_callback_cached($self, '_on_timeout')
if $self->{timeout};
return;
}
else {
$debug and $debug & 1 and $self->_debug("data available");
my $result = $dbc->result;
if ($result) {
if ($debug and $debug & 2) {
my $status = $result->status // '<undef>';
lib/AnyEvent/Pg/Pool.pm view on Meta::CPAN
# 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
t/AnyEvent-Pg.t view on Meta::CPAN
###########################################################################################33
#
# Tests go here:
#
#
plan tests => 28;
diag "conninfo: " . Pg::PQ::Conn::_make_conninfo($ci);
my $timer;
my $cv = AnyEvent->condvar;
my $pg = AnyEvent::Pg->new($ci,
on_connect => sub { pass("connected") },
on_connect_error => sub { fail("connect error") },
on_empty_queue => sub {
ok ($queued == 0, "queue is empty");
undef $timer;
$cv->send;
} );
fail_query($pg, 'drop table foo');
fail_query($pg, 'drop table bar');
ok_query($pg, 'create table foo (id int, name varchar(20))');
ok_query_prepare($pg, populate_foo => 'insert into foo (id, name) values ($1, $2)');
my %data = ( hello => 10, hola => 45, cheers => 1);
ok_query($pg, 'insert into foo (id, name) values ($1, $2)', $data{$_}, $_)
t/AnyEvent-Pg.t view on Meta::CPAN
ok_query_prepare($pg, foo_bigger => 'select * from foo where id > $1 order by id desc');
my %data1 = ( bye => 12, goodbye => 13, adios => 111, 'hasta la vista' => 41);
ok_query_prepared($pg, populate_foo => $data1{$_}, $_)
for keys %data1;
ok_query($pg, 'select * from foo');
ok_query_prepared($pg, 'foo_bigger', 12);
ok_query($pg, 'select * from foo where id < 12 order by name; select * from foo where id > 12 order by name');
$timer = AE::timer 120, 0, sub {
fail("timeout");
$cv->send;
};
$cv->recv;
pass("after recv");
$cv = AnyEvent->condvar;
$pg = AnyEvent::Pg->new($ci,
on_empty_queue => sub {
ok ($queued == 0, "queue is empty");
undef $timer;
$cv->send;
} );
$timer = AE::timer 120, 0, sub {
fail("timeout");
$cv->send;
};
$cv->recv;
pass("after recv 2");
undef $pg;
undef @w;
t/AnyEvent-Pg.t view on Meta::CPAN
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");
( run in 0.458 second using v1.01-cache-2.11-cpan-49f99fa48dc )