Add a default database connect timeout

Fixes #1760
This commit is contained in:
Joseph Schorr 2016-08-25 15:01:18 -04:00
parent 2174fe589b
commit 715fc27474

View file

@ -28,6 +28,7 @@ from util.names import urn_generator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
DEFAULT_DB_CONNECT_TIMEOUT = 10 # seconds
_SCHEME_DRIVERS = { _SCHEME_DRIVERS = {
'mysql': MySQLDatabase, 'mysql': MySQLDatabase,
@ -37,7 +38,6 @@ _SCHEME_DRIVERS = {
'postgresql+psycopg2': PostgresqlDatabase, 'postgresql+psycopg2': PostgresqlDatabase,
} }
SCHEME_RANDOM_FUNCTION = { SCHEME_RANDOM_FUNCTION = {
'mysql': fn.Rand, 'mysql': fn.Rand,
'mysql+pymysql': fn.Rand, 'mysql+pymysql': fn.Rand,
@ -46,7 +46,6 @@ SCHEME_RANDOM_FUNCTION = {
'postgresql+psycopg2': fn.Random, 'postgresql+psycopg2': fn.Random,
} }
def pipes_concat(arg1, arg2, *extra_args): def pipes_concat(arg1, arg2, *extra_args):
""" Concat function for sqlite, since it doesn't support fn.Concat. """ Concat function for sqlite, since it doesn't support fn.Concat.
Concatenates clauses with || characters. Concatenates clauses with || characters.
@ -215,9 +214,8 @@ db_concat_func = CallableProxy()
def validate_database_url(url, db_kwargs, connect_timeout=5): def validate_database_url(url, db_kwargs, connect_timeout=5):
db_kwargs = db_kwargs.copy() db_kwargs = db_kwargs.copy()
db_kwargs['connect_timeout'] = connect_timeout
driver = _db_from_url(url, db_kwargs) driver = _db_from_url(url, db_kwargs, connect_timeout=connect_timeout)
driver.connect() driver.connect()
driver.close() driver.close()
@ -226,7 +224,7 @@ def _wrap_for_retry(driver):
return type('Retrying' + driver.__class__.__name__, (RetryOperationalError, driver), {}) return type('Retrying' + driver.__class__.__name__, (RetryOperationalError, driver), {})
def _db_from_url(url, db_kwargs): def _db_from_url(url, db_kwargs, connect_timeout=DEFAULT_DB_CONNECT_TIMEOUT):
parsed_url = make_url(url) parsed_url = make_url(url)
if parsed_url.host: if parsed_url.host:
@ -239,8 +237,8 @@ def _db_from_url(url, db_kwargs):
db_kwargs['password'] = parsed_url.password db_kwargs['password'] = parsed_url.password
# Note: sqlite does not support connect_timeout. # Note: sqlite does not support connect_timeout.
if parsed_url.drivername == 'sqlite' and 'connect_timeout' in db_kwargs: if parsed_url.drivername != 'sqlite':
del db_kwargs['connect_timeout'] db_kwargs['connect_timeout'] = db_kwargs.get('connect_timeout', connect_timeout)
driver = _SCHEME_DRIVERS[parsed_url.drivername] driver = _SCHEME_DRIVERS[parsed_url.drivername]
wrapped_driver = _wrap_for_retry(driver) wrapped_driver = _wrap_for_retry(driver)