AnyEvent-ConnPool
view release on metacpan or search on metacpan
lib/AnyEvent/ConnPool.pm view on Meta::CPAN
if (defined $index) {
return $self->{_payload}->[$index];
}
if ($self->{index} + 1 > $self->{count}) {
$self->{index} = 0;
}
my $retval = $self->{_payload}->[$self->{index}];
if ($retval->locked()) {
$self->{locked}->{$self->{index}} = 1;
$retval = $self->get_free_connection($self->{index});
}
else {
delete $self->{locked}->{$self->{index}};
}
if (wantarray) {
$self->{index}++;
return ($index, $retval);
}
else {
$self->{index}++;
return $retval;
}
lib/AnyEvent/ConnPool.pm view on Meta::CPAN
# utility functions
sub get_free_connection {
my ($self, $desired_index) = @_;
my $retval = undef;
my @balanced_array = $self->balance_array($desired_index);
for my $i (@balanced_array) {
my $conn = $self->{_payload}->[$i];
unless ($conn->locked) {
$retval = $conn;
$self->{index} = $i;
last;
}
}
return $retval;
}
lib/AnyEvent/ConnPool.pm view on Meta::CPAN
use strict;
use warnings;
sub new {
my ($class, $object, %opts) = @_;
my ($index, $constructor) = ($opts{index}, $opts{constructor});
my $unit = {
_conn => $object,
_locked => 0,
_index => $index,
_constructor => $constructor,
};
bless $unit, $class;
return $unit;
}
=item B<conn>
lib/AnyEvent/ConnPool.pm view on Meta::CPAN
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();
=cut
sub lock {
my ($self) = @_;
$self->{_locked} = 1;
return 1;
}
=item B<unlock>
Unlocks connection and returns it to the balancing scheme.
$connection->unlock();
=cut
sub unlock {
my ($self) = @_;
delete $self->{_locked};
return 1;
}
=item B<locked>
Returns true if connection is locked.
if ($connection->locked()) {
...
}
=cut
sub locked {
my ($self) = @_;
return $self->{_locked};
}
sub index {
my ($self) = @_;
return $self->{_index};
}
sub reconnect {
( run in 0.454 second using v1.01-cache-2.11-cpan-49f99fa48dc )