NAME
AnyEvent::ConnPool
DESCRIPTION
Simple connections pool designed for asynchronous connections
METHODS
- new
-
Returns new pool object
AnyEvent::ConnPool->new( constructor => sub { return generate_connection(); }, check => { cb => sub { my $connection = shift; ... if ($connection->conn()->ping()) { return 1; } return 0; }, interval => 10, }, size => 5, init => 1, );
constructor => subroutine, which generates connection for pool.
check => pingers, allows to specify methods and interval for connection state validation. check->{cb} => callback, used for ping connection. You should implement this logic by yourself. If you need reconnect, you can just call
$connection->reconnect();
check->{interval} => interval for the callback.
size => how many connections should be created on pool initialization.
init => initialize connections on pool construction.
dispatcher => returns dispatcher object instead of pool object. You can use dispatcher as wrapper around your connection. In that case pool will use all it's features behind the scene. You can get dispatcher not only from constructor, you can get it from pool with dispatcher method. For example:
# with your connection $connection->selectall_arrayref(...); # pooled my $dispatcher = $connpool->dispatcher(); # same thing as selectall_arrayref above, but with connpool behind the scene. $dispatcher->selectall_arrayref(...); # you can always get connpool from dispatcher: my $pool = AnyEvent::ConnPool->pool_from_dispatcher($dispatcher);
- init
-
Initializes pool.
- dispatcher
-
Returns dispatcher object instead of pool object. It allows you to call connection's method directly, if you don't care about pool mechanism. And it's simple.
my $dispatcher = $connpool->dispatcher(); $dispatcher->selectall_arrayref(...); # equivalent to: $connpool->get()->conn()->selectall_arrayref(...);
- pool_from_dispatcher
-
Returns pool object from dispatcher object. You can call it by 3 ways:
my $pool = AnyEvent::ConnPool::pool_from_dispatcher($dispatcher); my $pool = AnyEvent::ConnPool->pool_from_dispatcher($dispatcher); my $pool = $connpool->pool_from_dispatcher($dispatcher);
- add
-
Adds connection to the pool.
- get
-
Returns AnyEvent::ConnPool::Unit object from the pool.
my $unit = $pool->get(); my $connection = $unit->conn();
NAME
AnyEvent::ConnPool::Unit
DESCRIPTION
Connection unit. Just wrapper around user-specified connection. Required for transactions support.
METHODS
- conn
-
Returns connection from unit object.
- lock
-
Locks current connection. After that connection shouldn't be used in balancing mechanism and never will be returned from pool. To unlock connection you should use unlock method.
$connection->lock();
- unlock
-
Unlocks connection and returns it to the balancing scheme.
$connection->unlock();
- locked
-
Returns true if connection is locked.
if ($connection->locked()) { ... }