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.

init

Initializes pool.

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()) {
    ...
}