瀏覽代碼

Fix busy waiting when checking heartbeat (#488)

Signed-off-by: CodeInDreams <hui068323@gmail.com>
CodeInDreams 2 年之前
父節點
當前提交
aba71028c7
共有 1 個文件被更改,包括 10 次插入1 次删除
  1. 10 1
      src/main/java/io/milvus/connection/ServerMonitor.java

+ 10 - 1
src/main/java/io/milvus/connection/ServerMonitor.java

@@ -56,8 +56,9 @@ public class ServerMonitor {
         public void run() {
             while (isRunning) {
                 long startTime = System.currentTimeMillis();
+                long timeTillNextCheck;
 
-                if (null == lastHeartbeat || startTime - lastHeartbeat > heartbeatInterval) {
+                if (null == lastHeartbeat || (timeTillNextCheck = lastHeartbeat + heartbeatInterval - startTime) <= 0) {
 
                     lastHeartbeat = startTime;
 
@@ -78,6 +79,14 @@ public class ServerMonitor {
                     } else {
                         logger.debug("Milvus Server Heartbeat. Master is Running.");
                     }
+                } else {
+                    try {
+                        Thread.sleep(timeTillNextCheck);
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                        logger.warn("Milvus Server Heartbeat. Interrupted.");
+                        throw new RuntimeException(e);
+                    }
                 }
             }