File Coverage

blib/lib/AnyEvent/MySQL/ConnPool.pm
Criterion Covered Total %
statement 16 29 55.1
branch 0 2 0.0
condition 0 4 0.0
subroutine 6 9 66.6
pod 1 1 100.0
total 23 45 51.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             AnyEvent::MySQL::ConnPool
4              
5             =head1 DESCRIPTION
6              
7             Adds additional method "pool_connect" to L package.
8              
9             =head1 SYNOPSIS
10              
11             Similar to AnyEvent::MySQL->connect();
12              
13             use AnyEvent;
14             use AnyEvent::MySQL;
15             use AnyEvent::MySQL::ConnPool;
16              
17             my $connpool = AnyEvent::MySQL->connect_pool(
18             "DBI:mysql:database=test;host=127.0.0.1;port=3306",
19             "ptest",
20             "pass", {
21             PrintError => 1,
22             PoolSize => 10,
23             CheckInterval => 5,
24             },
25             sub {
26             my($dbh) = @_;
27             if( $dbh ) {
28             warn "Connect success!";
29             $dbh->pre_do("set names latin1");
30             $dbh->pre_do("set names utf8");
31             }
32             else {
33             warn "Connect fail: $AnyEvent::MySQL::errstr ($AnyEvent::MySQL::err)";
34             $end->send;
35             }
36             }
37             );
38            
39             # if you need only connection methods, you can use dispatcher object as regular AnyEvent::MySQL connection object.
40             # the difference is: dispatcher applies connection pool functional to your connection object.
41             my $dispatcher = $connpool->dispatcher();
42             $dispatcher->selectall_hashref('SELECT * FROM `table1`', {}, sub {
43             ...;
44             });
45              
46             =head1 METHODS
47              
48             =over
49              
50             =item B
51              
52             Returns connected L object.
53             All options for connect_pool are similar to the AnyEvent::MySQL->connect method.
54             But pool accepts additional options in parameters hashref(4th parameter).
55              
56             AnyEvent::MySQL->connect_pool($dsn, $user, $password, {PoolSize => 5, CheckInterval => 10, Dispatcher => 0}, $callback);
57              
58             PoolSize => how many connections should be created. 5 connections by default.
59              
60             CheckInterval => Interval for ping connections. 10 seconds by default.
61              
62             Dispatcher => Determines return-value type. If true, connect_pool will return dispatcher object, instead of pool object. You can use dispatcher object
63             as regular AnyEvent::MySQL connection object, pool will do it's own behind the scene.
64              
65              
66             =cut
67              
68             package AnyEvent::MySQL::ConnPool;
69 1     1   434 use strict;
  1         1  
  1         30  
70 1     1   3 use warnings;
  1         1  
  1         22  
71              
72 1     1   4 use Carp;
  1         1  
  1         56  
73 1     1   507 use AnyEvent::MySQL;
  1         65791  
  1         32  
74 1     1   582 use AnyEvent::ConnPool;
  1         1563  
  1         175  
75              
76             our $VERSION = 0.07;
77              
78             sub import {
79 1     1   18 *{AnyEvent::MySQL::connect_pool} = \&new;
80             }
81              
82             =item B
83              
84             Same thing as connect_pool.
85            
86             my $connpool = AnyEvent::MySQL::ConnPool->new($dsn, $user, $password, {PoolSize => 5, CheckInterval => 10, Dispatcher => 0}, $callback);
87              
88             =cut
89              
90             sub new {
91 0     0 1   my ($caller, $dsn, $user, $password, $params, $cb) = @_;
92              
93 0           my @conn_args = @_;
94 0           shift @conn_args;
95              
96 0           my $pool_size = delete $params->{PoolSize};
97 0           my $check_interval = delete $params->{CheckInterval};
98 0           my $dispatcher = delete $params->{Dispatcher};
99              
100 0   0       $pool_size ||= 5;
101 0   0       $check_interval ||= 10;
102 0 0         $dispatcher = 0 unless $dispatcher;
103              
104             my $connpool = AnyEvent::ConnPool->new(
105             init => 1,
106             dispatcher => $dispatcher,
107             size => $pool_size,
108             check => {
109             cb => sub {
110 0     0     my $unit = shift;
111 0           $unit->conn()->ping();
112             },
113             interval => $check_interval,
114             },
115             constructor => sub {
116 0     0     return AnyEvent::MySQL->connect(@conn_args);
117             },
118 0           );
119             };
120              
121             1;
122              
123             =back
124              
125             =cut
126              
127             __END__