|
@@ -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 {
|
|
class ReconnectContext {
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
|
|
+ this.lastEvalDate = null;
|
|
|
|
|
+
|
|
|
this.reset(true);
|
|
this.reset(true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -18,16 +27,44 @@ class ReconnectContext {
|
|
|
this.stage++;
|
|
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, ...
|
|
const thresholdOfThisStage = 10 * Math.log2(this.stage); // 0, 10, 15.9, 20, 23.2, 25.9, 28.1, 30, ...
|
|
|
return this.counter > thresholdOfThisStage;
|
|
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();
|
|
context.incrementCount();
|
|
|
- return context.shouldReconnect;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (context.shouldReconnect) {
|
|
|
|
|
+ await reconnectHandler();
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ context.incrementStage();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|