Created
February 17, 2015 23:50
-
-
Save timbunce/085f810cc7ce24d7b6e9 to your computer and use it in GitHub Desktop.
DBIx::RetryConnection to add DBI connection retry logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Typical usage: | |
use DBIx::RetryConnection qw(Pg); | |
or: | |
use DBIx::RetryConnection Pg => { ... }; # options for Type::Tiny::Retry | |
DBI->connect('dbi:Pg:...', $user, $pass, { private_dbix_retry_connection_delay_exp => [...] }); | |
The monkey patching: | |
use Try::Tiny::Retry; | |
my $orig_dbd_pg_dr_connect = \&DBD::Pg::dr::connect; | |
*DBD::Pg::dr::connect = sub { | |
my ($drh, $dsn, $user, $pass, $attr) = @_; | |
return retry { | |
$orig_dbd_pg_dr_connect->($drh, $dsn, $user, $pass, $attr) | |
or die $dbh->errstr; | |
} | |
delay_exp { @{ $attr->{private_dbix_retry_connection_delay_exp} || [ 10, 1e5 ] } | |
catch { die $_ } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment