Subject: | patch: add ping_interval to Source.pm |
Adds a ping interval to Fey::DBIManager::Source. The attached patch
uses a 60-second default. If you decide to have it default to 0
instead, you can disregard the change made to the unit tests.
HTH,
Bryan Deeney
bdeeney@pobox.com
Subject: | Fey-DBIManager-0.10-ping_interval.patch |
diff -rupN Fey-DBIManager-0.10.orig/lib/Fey/DBIManager/Source.pm Fey-DBIManager-0.10/lib/Fey/DBIManager/Source.pm
--- Fey-DBIManager-0.10.orig/lib/Fey/DBIManager/Source.pm 2009-02-06 19:28:08.000000000 -0500
+++ Fey-DBIManager-0.10/lib/Fey/DBIManager/Source.pm 2009-06-04 11:29:51.000000000 -0400
@@ -68,6 +68,21 @@ has 'auto_refresh' =>
default => 1,
);
+has 'ping_interval' =>
+ ( is => 'ro',
+ isa => 'Int',
+ default => 60,
+ );
+
+has '_last_ping' =>
+ ( is => 'rw',
+ isa => 'Int',
+ default => 0,
+ clearer => '_clear_last_ping',
+ lazy => 1,
+ init_arg => undef,
+ );
+
has 'allows_nested_transactions' =>
( is => 'ro',
isa => 'Bool',
@@ -238,8 +253,7 @@ sub _ensure_fresh_dbh
$self->_unset_dbh();
}
- # Maybe consider only pinging once ever X seconds/minutes?
- unless ( $dbh->{Active} && $dbh->ping() )
+ unless ( $dbh->{Active} && $self->_ping_dbh() )
{
$dbh->disconnect();
$self->_unset_dbh();
@@ -248,6 +262,23 @@ sub _ensure_fresh_dbh
$self->_build_dbh() unless $self->_has_dbh();
}
+sub _ping_dbh()
+{
+ my $self = shift;
+ my $now = time();
+
+ return 1 if $self->ping_interval < 0;
+ return 1 if ( $now - $self->_last_ping() ) < $self->ping_interval();
+
+ if ( $self->_dbh()->ping() ) {
+ $self->_set_last_ping($now);
+ return 1;
+ } else {
+ $self->_clear_last_ping();
+ return 0;
+ }
+}
+
no Moose;
__PACKAGE__->meta()->make_immutable();
@@ -326,6 +357,15 @@ A boolean value. The default is true, wh
call C<< $source->dbh() >>, the source ensures that the database
handle is still active. See L<HANDLE FRESHNESS> for more details.
+=item * ping_interval
+
+An integer value representing the minimum number of seconds between
+successive pings of the database handle. See L<HANDLE FRESHNESS> for
+more details. The default value is 60 (seconds). A value of 0 causes
+the source to ping the database handle each time you call
+C<< $source->dbh() >>. A negative value disables the use of ping in
+freshness checks.
+
=back
=head2 $source->dbh()
@@ -391,8 +431,13 @@ created. If it has, we set C<InactiveDes
and disconnect it. If the thread has changed, we just disconnect the
handle.
-Finally, we check C<< $dbh->{Active] >> and call C<< $dbh->ping()
->>. If either of these is false, we disconnect the handle.
+Next, we check C<< $dbh->{Active] >> and, if this is false, we
+disconnect the handle.
+
+Finally, we check that the handle has responded to C<< $dbh->ping() >>
+within the past C<< $source->ping_interval() >> seconds. If it hasn't,
+we call C<< $dbh->ping() >> and, if it returns false, we disconnect the
+handle.
If the handle is not fresh, a new one is created.
diff -rupN Fey-DBIManager-0.10.orig/t/Source.t Fey-DBIManager-0.10/t/Source.t
--- Fey-DBIManager-0.10.orig/t/Source.t 2009-02-06 19:28:08.000000000 -0500
+++ Fey-DBIManager-0.10/t/Source.t 2009-06-05 14:18:00.000000000 -0400
@@ -76,12 +76,13 @@ EOF
my $count = 0;
my $sub = sub { $post_connect = shift; $count++ };
- my $source = Fey::DBIManager::Source->new( dsn => $DSN,
- username => $Username,
- password => $Password,
- attributes => { AutoCommit => 0,
+ my $source = Fey::DBIManager::Source->new( dsn => $DSN,
+ username => $Username,
+ password => $Password,
+ attributes => { AutoCommit => 0,
},
- post_connect => $sub,
+ post_connect => $sub,
+ ping_interval => 0
);
my $dbh = $source->dbh();