Created
October 24, 2016 10:47
-
-
Save davidszotten/dcf3fdab739dba38701a06f13388d585 to your computer and use it in GitHub Desktop.
This file contains 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
import psycopg2 | |
original_connect = psycopg2.connect | |
def connect_with_retry(*args, **kwargs): | |
retries = 3 | |
while True: | |
retries -= 1 | |
try: | |
return original_connect(*args, **kwargs) | |
except psycopg2.OperationalError as exc: | |
# Unfortunately exc.pgcode is None for connect_timeout. | |
if exc.args != (u'timeout expired\n',): | |
raise | |
if retries <= 0: | |
raise | |
def patch_psycopg2(): | |
psycopg2.connect = connect_with_retry | |
patch_psycopg2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment