Yuki Takei 5 лет назад
Родитель
Сommit
01f9f6e424
1 измененных файлов с 40 добавлено и 3 удалено
  1. 40 3
      src/server/service/search-reconnect-context/reconnect-context.js

+ 40 - 3
src/server/service/search-reconnect-context/reconnect-context.js

@@ -1,6 +1,15 @@
+const loggerFactory = require('@alias/logger');
+
+const logger = loggerFactory('growi:service:search-reconnect-context:reconnect-context');
+
+
+const RECONNECT_INTERVAL_SEC = 120;
+
 class ReconnectContext {
 
   constructor() {
+    this.lastEvalDate = null;
+
     this.reset(true);
   }
 
@@ -18,16 +27,44 @@ class ReconnectContext {
     this.stage++;
   }
 
-  get shouldReconnect() {
+  get shouldReconnectByCount() {
     const thresholdOfThisStage = 10 * Math.log2(this.stage); // 0, 10, 15.9, 20, 23.2, 25.9, 28.1, 30, ...
     return this.counter > thresholdOfThisStage;
   }
 
+  get shouldReconnectByTime() {
+    if (this.lastEvalDate == null) {
+      this.lastEvalDate = new Date();
+      return true;
+    }
+
+    const thres = this.lastEvalDate.setSeconds(this.lastEvalDate.getSeconds() + RECONNECT_INTERVAL_SEC);
+    return thres < new Date();
+  }
+
+  get shouldReconnect() {
+    if (this.shouldReconnectByTime) {
+      logger.info('Server should reconnect by time');
+      return true;
+    }
+    if (this.shouldReconnectByCount) {
+      logger.info('Server should reconnect by count');
+      return true;
+    }
+    return false;
+  }
+
 }
 
-function nextTick(context) {
+async function nextTick(context, reconnectHandler) {
   context.incrementCount();
-  return context.shouldReconnect;
+
+  if (context.shouldReconnect) {
+    await reconnectHandler();
+  }
+  else {
+    context.incrementStage();
+  }
 }
 
 module.exports = {