From 518ef68a53698f4f1986e21c55e819cfc5da7ae0 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Tue, 26 May 2015 09:19:20 +0200 Subject: [PATCH] Use connect_timeout=3s and statement_timeout=2s everywhere (even for local connections) --- helpers/postgresql.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/helpers/postgresql.py b/helpers/postgresql.py index 720d0775..4726ddc4 100644 --- a/helpers/postgresql.py +++ b/helpers/postgresql.py @@ -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