Browse Source

bit of set_timeout cleanup in cfgmgr
CCSession's check_command now does a non-blocking read to see if there are commands (instead of a blocking+timeout catch)
make default timeout a class constant in python cc.Session


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac312@2829 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen 14 years ago
parent
commit
c7d2c477c3

+ 3 - 4
src/lib/python/isc/cc/session.py

@@ -28,12 +28,10 @@ class SessionError(Exception): pass
 class SessionTimeout(Exception): pass
 
 class Session:
+    MSGQ_DEFAULT_TIMEOUT = 4000
+    
     def __init__(self, socket_file=None):
         self._socket = None
-        # store the current timeout value in seconds (the way
-        # settimeout() wants them, our API takes milliseconds
-        # so that it is consistent with the C++ version)
-        self._socket_timeout = 4;
         self._lname = None
         self._recvbuffer = bytearray()
         self._recvlength = 0
@@ -41,6 +39,7 @@ class Session:
         self._closed = False
         self._queue = []
         self._lock = threading.RLock()
+        self.set_timeout(self.MSGQ_DEFAULT_TIMEOUT);
 
         if socket_file is None:
             if "BIND10_MSGQ_SOCKET_FILE" in os.environ:

+ 5 - 12
src/lib/python/isc/config/ccsession.py

@@ -177,18 +177,11 @@ class ModuleCCSession(ConfigData):
     def check_command(self):
         """Check whether there is a command or configuration update
            on the channel. Call the corresponding callback function if
-           there is."""
-        msg, env = None, None
-        
-        # the module may have set timeout to zero (if it knows it will
-        # simply be doing nothing until it gets a command), but we
-        # cannot be sure of that (and we should change it at this
-        # level), so if we get a timeout, there is simply nothing to
-        # do and we return.
-        try:
-            msg, env = self._session.group_recvmsg(False)
-        except isc.cc.SessionTimeout:
-            return
+           there is. This function does a non-blocking read on the
+           cc session, and returns nothing. It will respond to any
+           command by either an error or the answer message returned
+           by the callback, unless the latter is None."""
+        msg, env = self._session.group_recvmsg(True)
         
         # should we default to an answer? success-by-default? unhandled error?
         if msg is not None and not 'result' in msg:

+ 4 - 11
src/lib/python/isc/config/cfgmgr.py

@@ -285,13 +285,9 @@ class ConfigManager:
             seq = self.cc.group_sendmsg(update_cmd, module_name)
             try:
                 # We have set the timeout to forever, set it now so we won't hang
-                self.cc.set_timeout(4000)
                 answer, env = self.cc.group_recvmsg(False, seq)
             except isc.cc.SessionTimeout:
                 answer = ccsession.create_answer(1, "Timeout waiting for answer from " + module_name)
-            finally:
-                # and set it back
-                self.cc.set_timeout(0)
         else:
             conf_part = data.set(self.config.data, module_name, {})
             data.merge(conf_part[module_name], cmd[1])
@@ -302,11 +298,9 @@ class ConfigManager:
             # replace 'our' answer with that of the module
             # We have set the timeout to forever, set it now so we won't hang
             try:
-                self.cc.set_timeout(4000)
                 answer, env = self.cc.group_recvmsg(False, seq)
             except isc.cc.SessionTimeout:
                 answer = ccsession.create_answer(1, "Timeout waiting for answer from " + module_name)
-            self.cc.set_timeout(0)
         if answer:
             rcode, val = ccsession.parse_answer(answer)
             if rcode == 0:
@@ -328,7 +322,6 @@ class ConfigManager:
                                                       self.config.data[module])
                 seq = self.cc.group_sendmsg(update_cmd, module)
                 try:
-                    self.cc.set_timeout(4000)
                     answer, env = self.cc.group_recvmsg(False, seq)
                     if answer == None:
                         got_error = True
@@ -341,8 +334,6 @@ class ConfigManager:
                 except isc.cc.SessionTimeout:
                     got_error = True
                     err_list.append("CC Timeout waiting on answer message from " + module)
-                finally:
-                    self.cc.set_timeout(0)
         if not got_error:
             self.write_config()
             return ccsession.create_answer(0)
@@ -416,10 +407,12 @@ class ConfigManager:
         self.running = True
         while (self.running):
             # we just wait eternally for any command here, so disable
-            # timeouts
+            # timeouts for this specific recv
             self.cc.set_timeout(0)
             msg, env = self.cc.group_recvmsg(False)
-            # ignore 'None' value (current result of timeout)
+            # and set it back to whatever we default to
+            self.cc.set_timeout(isc.cc.Session.MSGQ_DEFAULT_TIMEOUT)
+            # ignore 'None' value (even though they should not occur)
             # and messages that are answers to questions we did
             # not ask
             if msg is not None and not 'result' in msg:

+ 1 - 1
src/lib/python/isc/config/tests/cfgmgr_test.py

@@ -258,7 +258,7 @@ class TestConfigManager(unittest.TestCase):
         self._handle_msg_helper({ "command": [ "set_config", [ ] ] },
                                 {'result': [1, 'Wrong number of arguments']} )
         self._handle_msg_helper({ "command": [ "set_config", [ self.name, { "test": 125 }] ] },
-                                { 'result': [1, 'Timeout waiting for answer from TestModule']} )
+                                { 'result': [1, 'No answer message from TestModule']} )
 
         #self.assertEqual(len(self.fake_session.message_queue), 1)
         #self.assertEqual({'config_update': {'test': 124}},