From 5c89c23f5e0de06a17a9263114430674221a1ee0 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Wed, 17 Apr 2024 06:59:08 +0000
Subject: [PATCH 65/69] change default subscribe timeout to 5min

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/daemon/mailbox/message_queue.c |  4 ++--
 src/utils/cutils/blocking_queue.c  | 13 +++++++------
 src/utils/cutils/blocking_queue.h  |  2 +-
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/daemon/mailbox/message_queue.c b/src/daemon/mailbox/message_queue.c
index 7e53301e..699ea0bc 100644
--- a/src/daemon/mailbox/message_queue.c
+++ b/src/daemon/mailbox/message_queue.c
@@ -20,8 +20,8 @@
 
 #include "utils.h"
 
-// default set subscriber timeout to 1000ms, maybe could be configured later
-const int64_t subscribe_timeout = 1000;
+// default set subscriber timeout to 300s, maybe could be configured later
+const int64_t subscribe_timeout = 300;
 
 static void message_queue_subscriber_free(void *key, void *val)
 {
diff --git a/src/utils/cutils/blocking_queue.c b/src/utils/cutils/blocking_queue.c
index 7c9c5f50..9bdb2ca3 100644
--- a/src/utils/cutils/blocking_queue.c
+++ b/src/utils/cutils/blocking_queue.c
@@ -55,12 +55,11 @@ blocking_queue *blocking_queue_create(int64_t timeout, void (*release)(void *))
     queue->release = release;
 
     if (timeout >= 0) {
-        queue->timeout.tv_sec = timeout / (Time_Second / Time_Milli);
-        queue->timeout.tv_nsec = (timeout % (Time_Second / Time_Milli) ) * Time_Milli;
+        queue->timeout = timeout;
     } else {
-        queue->timeout.tv_sec = -1;
+        queue->timeout = -1;
     }
-    
+
     return isula_transfer_ptr(queue);
 }
 
@@ -112,8 +111,10 @@ int blocking_queue_pop(blocking_queue *queue, void **data) {
     lock = &queue->lock;
 
     while (queue->head->next == NULL) {
-        if (queue->timeout.tv_sec >= 0) {
-            int ret = pthread_cond_timedwait(&queue->not_empty, &queue->lock, &queue->timeout);
+        if (queue->timeout >= 0) {
+            struct timespec timeout = { 0 };
+            timeout.tv_sec = queue->timeout + time(NULL);
+            int ret = pthread_cond_timedwait(&queue->not_empty, &queue->lock, &timeout);
             if (ret != 0) {
                 if (ret != ETIMEDOUT) {
                     ERROR("Failed to wait cond");
diff --git a/src/utils/cutils/blocking_queue.h b/src/utils/cutils/blocking_queue.h
index 1c52a9d3..257779c3 100644
--- a/src/utils/cutils/blocking_queue.h
+++ b/src/utils/cutils/blocking_queue.h
@@ -37,7 +37,7 @@ typedef struct blocking_queue {
     blocking_node *head;
     blocking_node *tail;
     pthread_mutex_t lock;
-    struct timespec timeout;
+    int64_t timeout;
     pthread_cond_t not_empty;
     void (*release)(void *);
 } blocking_queue;
-- 
2.34.1

