Use connect_timeout=3s and statement_timeout=2s everywhere (even for local connections)

This commit is contained in:
Alexander Kukushkin
2015-05-26 09:19:20 +02:00
parent 45c4ba85b4
commit 518ef68a53
+10 -5
View File
@@ -17,15 +17,19 @@ logger = logging.getLogger(__name__)
def parseurl(url):
r = urlparse(url)
return {
ret = {
'host': r.hostname,
'port': r.port or 5432,
'user': r.username,
'password': r.password,
'database': r.path[1:],
'fallback_application_name': 'Governor',
'connect_timeout': 5,
'connect_timeout': 3,
'options': '-c statement_timeout=2000',
}
if r.username:
ret['user'] = r.username
if r.password:
ret['password'] = r.password
return ret
class Postgresql:
@@ -55,7 +59,8 @@ class Postgresql:
def connection(self):
if not self._connection or self._connection.closed != 0:
self._connection = psycopg2.connect('postgres://{}/postgres'.format(self.local_address))
r = parseurl('postgres://{}/postgres'.format(self.local_address))
self._connection = psycopg2.connect(**r)
self._connection.autocommit = True
return self._connection