Limit the number of DB queries from the main thread

Basically, we want to make the interval we ask for configs and for now() longer. The now may use the DB time only to calibrate local clock. This is a proposed diff (with some other changes to report transactions taking a long time, but we may want that too, possibly with a longer time):

 import threading
 import traceback
 import time
+import datetime
 from master_config import get

 logger = logging.getLogger(name='database')
@@ -33,6 +34,7 @@ class __CursorContext:
        def __init__(self, connection):
                self.__connection = connection
                self.__depth = 0
+               self.__start_time = None
                self.reuse()

        def reuse(self):
@@ -41,6 +43,7 @@ class __CursorContext:
        def __enter__(self):
                if not self.__depth:
                        logger.debug('Entering transaction %s', self)
+                       self.__start_time = time.time()
                self.__depth += 1
                return self._cursor

@@ -48,6 +51,8 @@ class __CursorContext:
                self.__depth -= 1
                if self.__depth:
                        return # Didn't exit all the contexts yet
+               if time.time() - self.__start_time > 2:
+                       logger.error("Transaction took a long time: %s", traceback.format_stack())
                if exc_type:
                        logger.error('Rollback of transaction %s:%s/%s/%s', self, exc_type, exc_val, trace
                        self.__connection.rollback()
@@ -115,9 +120,10 @@ def now():
        global __time_update
        global __time_db
        t = time.time()
-       if __time_update + 2 < t:
+       diff = t - __time_update
+       if diff > 600000:
                __time_update = t
                with transaction() as t:
                        t.execute("SELECT CURRENT_TIMESTAMP AT TIME ZONE 'UTC'");
                        (__time_db,) = t.fetchone()
-       return __time_db
+       return __time_db + datetime.timedelta(seconds=diff)