jsonrpc: Fix Python implementation of inactivity logic.
[sliver-openvswitch.git] / python / ovs / jsonrpc.py
index 0eda32d..c1540eb 100644 (file)
@@ -186,6 +186,7 @@ class Connection(object):
         self.input = ""
         self.output = ""
         self.parser = None
+        self.received_bytes = 0
 
     def close(self):
         self.stream.close()
@@ -221,6 +222,9 @@ class Connection(object):
         else:
             return len(self.output)
 
+    def get_received_bytes(self):
+        return self.received_bytes
+
     def __log_msg(self, title, msg):
         vlog.dbg("%s: %s %s" % (self.name, title, msg))
 
@@ -271,6 +275,7 @@ class Connection(object):
                     return EOF, None
                 else:
                     self.input += data
+                    self.received_bytes += len(data)
             else:
                 if self.parser is None:
                     self.parser = ovs.json.Parser()
@@ -444,7 +449,18 @@ class Session(object):
                 self.pstream = None
 
         if self.rpc:
+            backlog = self.rpc.get_backlog()
             self.rpc.run()
+            if self.rpc.get_backlog() < backlog:
+                # Data previously caught in a queue was successfully sent (or
+                # there's an error, which we'll catch below).
+                #
+                # We don't count data that is successfully sent immediately as
+                # activity, because there's a lot of queuing downstream from
+                # us, which means that we can push a lot of data into a
+                # connection that has stalled and won't ever recover.
+                self.reconnect.activity(ovs.timeval.msec())
+
             error = self.rpc.get_status()
             if error != 0:
                 self.reconnect.disconnected(ovs.timeval.msec(), error)
@@ -502,16 +518,14 @@ class Session(object):
 
     def recv(self):
         if self.rpc is not None:
-            backlog = self.rpc.get_backlog()
+            received_bytes = self.rpc.get_received_bytes()
             error, msg = self.rpc.recv()
-            if self.rpc.get_backlog() < backlog:
-                # Data previously caught in a queue was successfully sent (or
-                # there's an error, which we'll catch below).
+            if received_bytes != self.rpc.get_received_bytes():
+                # Data was successfully received.
                 #
-                # We don't count data that is successfully sent immediately as
-                # activity, because there's a lot of queuing downstream from
-                # us, which means that we can push a lot of data into a
-                # connection that has stalled and won't ever recover.
+                # Previously we only counted receiving a full message as
+                # activity, but with large messages or a slow connection that
+                # policy could time out the session mid-message.
                 self.reconnect.activity(ovs.timeval.msec())
 
             if not error: