AnyEvent-Pg
view release on metacpan or search on metacpan
lib/AnyEvent/Pg/Pool.pm view on Meta::CPAN
=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
When some error happens that can not be automatically handled by the
module (for instance, by requeuing the current query), this callback
is invoked.
=item on_connect_error => $callback
When the number of failed reconnection attempts goes over the limit,
this callback is called. The pool object and the L<AnyEvent::Pg>
object representing the last failed attempt are passed as arguments.
=item on_transient_error => $callback
The given callback is invoked every time an internal recoverable error
happens (for instance, on of the pool connections fails or times out).
There is no guarantee about when this callback will be called and how
many times. It should be considered just a hint.
=back
=item $w = $pool->push_query(%opts)
Pushes a database query on the pool queue. It will be sent to the
database once any of the database connections becomes idle.
A watcher object is returned. If that watcher goes out of scope, the
query is canceled.
This method accepts all the options supported by the method of the
same name on L<AnyEvent::Pg> plus the following ones:
=over 4
=item retry_on_sqlstate => \@states
=item retry_on_sqlstate => \%states
A hash of sqlstate values that are retryable. When some error happens,
and the value of sqlstate from the result object has a value on this
hash, the query is reset and reintroduced on the query.
=item max_retries => $n
Maximum number of times a query can be retried. When this limit is
reached, the on_error callback will be called.
Note that queries are not retried after partial success. For instance,
when a result object is returned, but then the server decides to abort
the transaction (this is rare, but can happen from time to time).
=item priority => $n
This option allows to prioritize queries. The pool dispatches first those
with the highest priority value.
The default priority is -inf.
Queries of equal priority are dispatched in FIFO order.
=item initialization => $bool
When this option is set, the query will be invoked for every database
connection (both currently existing or created on the future) before
any other query.
It can be used to set up session parameters. For instance:
$pool->push_query(initialization => 1,
query => "set session time zone 'UTC'");
Pushing initialization queries does not return a watcher object. Also,
once pushed, the current API does not allow removing them.
=back
The callbacks for the C<push_query> method receive as arguments the
pool object, the underlying L<AnyEvent::Pg> object actually handling
the query and the result object when applicable. For instance:
sub on_result_cb {
my ($pool, $conn, $result) = @_;
...
}
sub on_done_cb {
my ($pool, $conn) = @_;
}
my $watcher = $pool->push_query("select * from foo",
on_result => \&on_result_cb,
on_done => \&on_done_cb);
=item $w = $pool->listen($channel, %opts)
This method allows to subscribe to the given notification channel and
receive an event every time another sends a notification (see
PostgreSQL NOTIFY/LISTEN documentation).
The module will take care of keeping an active L<AnyEvent::Pg> connection
subscribed to the channel, recovering from errors automatically.
Currently, due to some limitations on the way the C<LISTEN> SQL
command is parsed, the channel selector has to match C</^[a-z]\w*$/i>.
The options accepted by the method are as follow:
=over 4
( run in 2.058 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )