DBIx-Connector-Pool

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


# SYNOPSIS

    use Coro;
    use AnyEvent;
    use Coro::AnyEvent;
    use DBIx::Connector::Pool;
    
    my $pool = DBIx::Connector::Pool->new(
      initial    => 1,
      keep_alive => 1,
      max_size   => 5,
      tid_func   => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
      wait_func => sub        {Coro::AnyEvent::sleep 0.05},
      attrs     => {RootClass => 'DBIx::PgCoroAnyEvent'}
    );
    
    async {
      my $connector = $pool->get_connector;
      $connector->run(
        sub {

README.md  view on Meta::CPAN


This module requires some threading model and I know about only one really 
working [Coro](https://metacpan.org/pod/Coro). 

# Methods

- **new**

        my $pool = DBIx::Connector::Pool->new(
          initial    => 1,
          keep_alive => 1,
          max_size   => 5,
          tid_func   => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
          wait_func => sub        {Coro::AnyEvent::sleep 0.05},
          attrs     => {RootClass => 'DBIx::PgCoroAnyEvent'}
        );

    Creates new pool. Possible parameters:

    - **initial**

        Initial number of connected connectors. This means also minimum of of
        connected connectors. It throws error if this minimum can not be met.

    - **keep\_alive**

        How long connector can live after it becomes unused. Initial connectors will
        live forever. `-1` means no limit. `0` means collect it immediate. Positive 
        number means seconds.

    - **max\_size**

        Maximum pool capacity. `-1` means unlimited.

    - **user**

lib/DBIx/Connector/Pool.pm  view on Meta::CPAN

use Time::HiRes 'time';

our $VERSION = "0.02";

sub new {
	my ($class, %args) = @_;
	$args{initial}   //= 1;
	$args{tid_func}  //= sub {1};
	$args{wait_func} //= sub {croak "real waiting function must be supplied"};
	$args{max_size} ||= -1;
	$args{keep_alive}     //= -1;
	$args{user}           //= ((getpwuid $>)[0]);
	$args{password}       //= '';
	$args{attrs}          //= {};
	$args{dsn}            //= 'dbi:Pg:dbname=' . $args{user};
	$args{connector_mode} //= 'fixup';
	if ($args{max_size} > 0 && $args{initial} != 0 && $args{initial} > $args{max_size}) {
		$args{initial} = $args{max_size};
	}
	$args{pool} = [];
	my $self = bless \%args, $class;

lib/DBIx/Connector/Pool.pm  view on Meta::CPAN

		if ($i == @{$self->{pool}} - 1) {
			pop @{$self->{pool}};
		} else {
			$self->{pool}[$i] = {};
		}
		--$connected_size;
	};
	my $now = time;
	for ($i = @{$self->{pool}} - 1; $i >= 0 && $connected_size > $self->{initial}; --$i) {
		if ($self->{pool}[$i]{connector} && !$self->{pool}[$i]{connector}->item_in_use) {
			if ($now - $self->{pool}[$i]{connector}->item_last_use > $self->{keep_alive}) {
				$remove_sub->();
			} else {
				$self->{pool}[$i]{tid} = undef;
			}
		} elsif (!$self->{pool}[$i]{connector}) {
			$remove_sub->();
		}
	}
}

lib/DBIx/Connector/Pool.pm  view on Meta::CPAN

 
=head1 SYNOPSIS

  use Coro;
  use AnyEvent;
  use Coro::AnyEvent;
  use DBIx::Connector::Pool;
  
  my $pool = DBIx::Connector::Pool->new(
    initial    => 1,
    keep_alive => 1,
    max_size   => 5,
    tid_func   => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
    wait_func => sub        {Coro::AnyEvent::sleep 0.05},
    attrs     => {RootClass => 'DBIx::PgCoroAnyEvent'}
  );
  
  async {
    my $connector = $pool->get_connector;
    $connector->run(
      sub {

lib/DBIx/Connector/Pool.pm  view on Meta::CPAN

working L<Coro>. 

=head1 Methods

=over 

=item B<new>
  
  my $pool = DBIx::Connector::Pool->new(
    initial    => 1,
    keep_alive => 1,
    max_size   => 5,
    tid_func   => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
    wait_func => sub        {Coro::AnyEvent::sleep 0.05},
    attrs     => {RootClass => 'DBIx::PgCoroAnyEvent'}
  );

Creates new pool. Possible parameters:

=over

=item B<initial>

Initial number of connected connectors. This means also minimum of of
connected connectors. It throws error if this minimum can not be met.

=item B<keep_alive>

How long connector can live after it becomes unused. Initial connectors will
live forever. C<-1> means no limit. C<0> means collect it immediate. Positive 
number means seconds.

=item B<max_size>

Maximum pool capacity. C<-1> means unlimited.

=item B<user>

t/01_pool.t  view on Meta::CPAN

		}
		$_[0]->used_now;
	};

	ok(
		my $pool = DBIx::Connector::Pool->new(
			dsn        => "dbi:Pg:dbname=postgres",
			user       => $PgSet::testuser,
			password   => '',
			initial    => 1,
			keep_alive => 1,
			max_size   => 5,
			tid_func   => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
			wait_func  => sub {push @pool_wait_queue, $Coro::current; Coro::schedule;},
			attrs => {RootClass => 'DBIx::PgCoroAnyEvent'}
		),
		'created pool'
	);

	my @async;
	for my $th (1 .. 10) {



( run in 0.622 second using v1.01-cache-2.11-cpan-39bf76dae61 )