Add a db-statement-timeout option for postgresql sources
authorJulien Cristau <julien.cristau@logilab.fr>
Tue, 04 Feb 2014 15:06:25 +0100
changeset 9724 e45bf9baa7b7
parent 9718 927d035b6921
child 9725 2614362be11e
Add a db-statement-timeout option for postgresql sources Use that new option to set the statement_timeout option when creating connections, except for maintenance operations (shell and upgrade). Closes #2547026
server/sources/native.py
server/sqlutils.py
--- a/server/sources/native.py	Tue Jan 07 15:10:57 2014 +0100
+++ b/server/sources/native.py	Tue Feb 04 15:06:25 2014 +0100
@@ -323,10 +323,16 @@
                   'want trusted authentication for the database connection',
           'group': 'native-source', 'level': 2,
           }),
+        ('db-statement-timeout',
+         {'type': 'int',
+          'default': 0,
+          'help': 'sql statement timeout, in milliseconds (postgres only)',
+          'group': 'native-source', 'level': 2,
+          }),
     )
 
     def __init__(self, repo, source_config, *args, **kwargs):
-        SQLAdapterMixIn.__init__(self, source_config)
+        SQLAdapterMixIn.__init__(self, source_config, repairing=repo.config.repairing)
         self.authentifiers = [LoginPasswordAuthentifier(self)]
         if repo.config['allow-email-login']:
             self.authentifiers.insert(0, EmailPasswordAuthentifier(self))
--- a/server/sqlutils.py	Tue Jan 07 15:10:57 2014 +0100
+++ b/server/sqlutils.py	Tue Feb 04 15:06:25 2014 +0100
@@ -299,7 +299,7 @@
     """
     cnx_wrap = ConnectionWrapper
 
-    def __init__(self, source_config):
+    def __init__(self, source_config, repairing=False):
         try:
             self.dbdriver = source_config['db-driver'].lower()
             dbname = source_config['db-name']
@@ -328,6 +328,14 @@
         if self.dbdriver == 'sqlite':
             self.cnx_wrap = SqliteConnectionWrapper
             self.dbhelper.dbname = abspath(self.dbhelper.dbname)
+        if not repairing:
+            statement_timeout = int(source_config.get('db-statement-timeout', 0))
+            if statement_timeout > 0:
+                def set_postgres_timeout(cnx):
+                    cnx.cursor().execute('SET statement_timeout to %d' % statement_timeout)
+                    cnx.commit()
+                postgres_hooks = SQL_CONNECT_HOOKS['postgres']
+                postgres_hooks.append(set_postgres_timeout)
 
     def wrapped_connection(self):
         """open and return a connection to the database, wrapped into a class