File Coverage

blib/lib/AnyEvent/MySQL/ConnPool.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


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             =head1 METHODS
40              
41             =over
42              
43             =item B
44              
45             Returns connected L object.
46             All options for connect_pool are similar to the AnyEvent::MySQL->connect method.
47             But pool accepts additional options in parameters hashref(4th parameter).
48              
49             AnyEvent::MySQL->connect_pool($dsn, $user, $password, {PoolSize => 5, CheckInterval => 10}, $callback);
50              
51             PoolSize => how many connections should be created. 5 connections by default.
52              
53             CheckInterval => Interval for ping connections. 10 seconds by default.
54              
55             =back
56              
57             =cut
58              
59             package AnyEvent::MySQL::ConnPool;
60 1     1   607 use strict;
  1         1  
  1         39  
61 1     1   5 use warnings;
  1         1  
  1         33  
62              
63 1     1   232 use AnyEvent::MySQL;
  0            
  0            
64             use AnyEvent::ConnPool;
65              
66             our $VERSION = 0.05;
67              
68             sub import {
69             *{AnyEvent::MySQL::connect_pool} = sub {
70             my ($caller, $dsn, $user, $password, $params, $cb) = @_;
71              
72             my @conn_args = @_;
73             shift @conn_args;
74              
75             my $pool_size = delete $params->{PoolSize};
76             my $check_interval = delete $params->{CheckInterval};
77              
78             $pool_size ||= 5;
79             $check_interval ||= 10;
80              
81             my $connpool = AnyEvent::ConnPool->new(
82             init => 1,
83             size => $pool_size,
84             check => {
85             cb => sub {
86             my $unit = shift;
87             $unit->conn()->ping();
88             },
89             interval => $check_interval,
90             },
91             constructor => sub {
92             return AnyEvent::MySQL->connect(@conn_args);
93             },
94             );
95             };
96             }
97              
98             1;
99              
100             __END__