File Coverage

blib/lib/AnyEvent/MySQL/ConnPool.pm
Criterion Covered Total %
statement 13 24 54.1
branch n/a
condition 0 4 0.0
subroutine 5 8 62.5
pod n/a
total 18 36 50.0


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   442 use strict;
  1         2  
  1         31  
61 1     1   3 use warnings;
  1         1  
  1         24  
62              
63 1     1   493 use AnyEvent::MySQL;
  1         54311  
  1         31  
64 1     1   471 use AnyEvent::ConnPool;
  1         1114  
  1         156  
65              
66             our $VERSION = 0.06;
67              
68             sub import {
69             *{AnyEvent::MySQL::connect_pool} = sub {
70 0     0   0 my ($caller, $dsn, $user, $password, $params, $cb) = @_;
71              
72 0         0 my @conn_args = @_;
73 0         0 shift @conn_args;
74              
75 0         0 my $pool_size = delete $params->{PoolSize};
76 0         0 my $check_interval = delete $params->{CheckInterval};
77              
78 0   0     0 $pool_size ||= 5;
79 0   0     0 $check_interval ||= 10;
80              
81             my $connpool = AnyEvent::ConnPool->new(
82             init => 1,
83             size => $pool_size,
84             check => {
85             cb => sub {
86 0     0   0 my $unit = shift;
87 0         0 $unit->conn()->ping();
88             },
89             interval => $check_interval,
90             },
91             constructor => sub {
92 0     0   0 return AnyEvent::MySQL->connect(@conn_args);
93             },
94 0         0 );
95 1     1   21 };
96             }
97              
98             1;
99              
100             __END__