Browse Source

Fix busy waiting when checking heartbeat (#488) (#493)

Signed-off-by: CodeInDreams <hui068323@gmail.com>
Co-authored-by: CodeInDreams <hui068323@gmail.com>
groot 2 years ago
parent
commit
7404e48ed5
1 changed files with 10 additions and 1 deletions
  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);
+                    }
                 }
             }