]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
rpc: Refactor keepalive timer code
authorJiri Denemark <jdenemar@redhat.com>
Tue, 12 Jun 2012 21:41:25 +0000 (23:41 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Wed, 13 Jun 2012 13:43:45 +0000 (15:43 +0200)
The code that needs to be run every keepalive interval of inactivity was
only called from a timer and thus from the main event loop. We will need
to call the code directly from another place.

src/rpc/virkeepalive.c

index a5c2b1aadaa3cda8969899f0ba47979fa9517cde..b2e260e8b968b080f035e47c9c12c41b8a48de8e 100644 (file)
@@ -152,51 +152,64 @@ virKeepAliveScheduleResponse(virKeepAlivePtr ka)
 }
 
 
-static void
-virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
+static bool
+virKeepAliveTimerInternal(virKeepAlivePtr ka,
+                          virNetMessagePtr *msg)
 {
-    virKeepAlivePtr ka = opaque;
     time_t now = time(NULL);
 
-    virKeepAliveLock(ka);
+    if (now - ka->lastPacketReceived < ka->interval - 1) {
+        int timeout = ka->interval - (now - ka->lastPacketReceived);
+        virEventUpdateTimeout(ka->timer, timeout * 1000);
+        return false;
+    }
 
     PROBE(RPC_KEEPALIVE_TIMEOUT,
           "ka=%p client=%p countToDeath=%d idle=%d",
           ka, ka->client, ka->countToDeath,
           (int) (now - ka->lastPacketReceived));
 
-    if (now - ka->lastPacketReceived < ka->interval - 1) {
-        int timeout = ka->interval - (now - ka->lastPacketReceived);
-        virEventUpdateTimeout(ka->timer, timeout * 1000);
-        goto cleanup;
-    }
 
     if (ka->countToDeath == 0) {
-        virKeepAliveDeadFunc deadCB = ka->deadCB;
-        void *client = ka->client;
-
         VIR_WARN("No response from client %p after %d keepalive messages in"
                  " %d seconds",
                  ka->client,
                  ka->count,
                  (int) (now - ka->lastPacketReceived));
+        return true;
+    } else {
+        ka->countToDeath--;
+        *msg = virKeepAliveMessage(KEEPALIVE_PROC_PING);
+        virEventUpdateTimeout(ka->timer, ka->interval * 1000);
+        return false;
+    }
+}
+
+
+static void
+virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
+{
+    virKeepAlivePtr ka = opaque;
+    virNetMessagePtr msg = NULL;
+    bool dead;
+
+    virKeepAliveLock(ka);
+
+    dead = virKeepAliveTimerInternal(ka, &msg);
+
+    if (dead) {
+        virKeepAliveDeadFunc deadCB = ka->deadCB;
+        void *client = ka->client;
+
         ka->refs++;
         virKeepAliveUnlock(ka);
         deadCB(client);
         virKeepAliveLock(ka);
         ka->refs--;
-    } else {
-        virNetMessagePtr msg;
-
-        ka->countToDeath--;
-        if (!(msg = virKeepAliveMessage(KEEPALIVE_PROC_PING)))
-            VIR_WARN("Failed to generate keepalive request");
-        else
-            virKeepAliveSend(ka, msg);
-        virEventUpdateTimeout(ka->timer, ka->interval * 1000);
+    } else if (msg) {
+        virKeepAliveSend(ka, msg);
     }
 
-cleanup:
     virKeepAliveUnlock(ka);
 }