[virt-tools-list] [vhostmd PATCH 3/3] Add a timeout to the metrics request and fix the timeout value.

Michael Trapp Michael.Trapp at sap.com
Thu Feb 7 15:41:37 UTC 2019


A timeout in the send request part of dump_virtio_metrics()
should prevent the send from looping forever.
The timeout value is fixed to a reasonable value of 5 seconds.
---
 libmetrics/libmetrics.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/libmetrics/libmetrics.c b/libmetrics/libmetrics.c
index d79a023..08a233f 100644
--- a/libmetrics/libmetrics.c
+++ b/libmetrics/libmetrics.c
@@ -788,7 +788,8 @@ static char *get_virtio_metrics(void)
     size_t pos;
     size_t buf_size = (1 << 16);
     const size_t req_len = (size_t) strlen(request);
-    const time_t start_time = time(NULL);
+    const time_t timeout = 5;
+    time_t end_time;
 
     response = calloc(1UL, buf_size);
     if (response == NULL)
@@ -803,19 +804,27 @@ static char *get_virtio_metrics(void)
     }
 
     pos = 0;
+    end_time = time(NULL) + timeout;
     while (pos < req_len) {
         ssize_t len = write(fd, &request[pos], req_len - pos);
         if (len > 0)
             pos += (size_t) len;
         else {
-            if (errno == EAGAIN)
+            if (errno == EAGAIN) {
                 usleep(10000);
+                if (time(NULL) > end_time) {
+                    libmsg("%s(): Unable to send metrics request"
+                            " - timeout after %us\n", __func__, timeout);
+                    goto error;
+                }
+            }
             else
                 goto error;
         }
     }
 
     pos = 0;
+    end_time = time(NULL) + timeout;
     do {
         ssize_t len = read(fd, &response[pos], buf_size - pos - 1);
         if (len > 0) {
@@ -836,9 +845,9 @@ static char *get_virtio_metrics(void)
         } else {
             if (errno == EAGAIN) {
                 usleep(10000);
-                if (time(NULL) > (start_time + 30)) {
-                    libmsg("Error, unable to read metrics"
-                            " - timeout after 30s\n");
+                if (time(NULL) > end_time) {
+                    libmsg("%s(): Unable to read metrics"
+                            " - timeout after %us\n", __func__, timeout);
                     goto error;
                 }
             } else
-- 
2.17.2 (Apple Git-113)




More information about the virt-tools-list mailing list