Skip to content

Instantly share code, notes, and snippets.

@timbunce
Created February 17, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbunce/085f810cc7ce24d7b6e9 to your computer and use it in GitHub Desktop.
Save timbunce/085f810cc7ce24d7b6e9 to your computer and use it in GitHub Desktop.
DBIx::RetryConnection to add DBI connection retry logic
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