Yuki Takei 5 лет назад
Родитель
Сommit
219eb140bc

+ 2 - 2
src/server/middlewares/auto-reconnect-to-config-pubsub.js

@@ -1,8 +1,8 @@
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   const { configPubsub } = crowi;
   const { configPubsub } = crowi;
 
 
-  return async(req, res, next) => {
-    if (configPubsub != null) {
+  return (req, res, next) => {
+    if (configPubsub != null && configPubsub.shouldResubscribe()) {
       configPubsub.subscribe();
       configPubsub.subscribe();
     }
     }
 
 

+ 4 - 0
src/server/service/config-pubsub/base.js

@@ -8,6 +8,10 @@ class ConfigPubsubDelegator {
     }
     }
   }
   }
 
 
+  shouldResubscribe() {
+    throw new Error('implement this');
+  }
+
   subscribe(forceReconnect) {
   subscribe(forceReconnect) {
     throw new Error('implement this');
     throw new Error('implement this');
   }
   }

+ 23 - 17
src/server/service/config-pubsub/nchan.js

@@ -17,12 +17,23 @@ class NchanDelegator extends ConfigPubsubDelegator {
 
 
     this.channelId = channelId;
     this.channelId = channelId;
     this.messageHandlers = [];
     this.messageHandlers = [];
-    this.isReconnecting = false;
+    this.isConnecting = false;
 
 
     this.client = null;
     this.client = null;
     this.connection = null;
     this.connection = null;
   }
   }
 
 
+  /**
+   * @inheritdoc
+   */
+  shouldResubscribe() {
+    if (this.connection != null && this.connection.connected) {
+      return false;
+    }
+
+    return !this.isConnecting;
+  }
+
   /**
   /**
    * @inheritdoc
    * @inheritdoc
    */
    */
@@ -33,23 +44,19 @@ class NchanDelegator extends ConfigPubsubDelegator {
       }
       }
     }
     }
 
 
-    // init client and connect
+    // init client
     if (this.client == null) {
     if (this.client == null) {
       this.initClient();
       this.initClient();
-      const url = this.constructUrl(this.subscribePath).toString();
-      this.client.connect(url.toString());
-
-      return;
     }
     }
 
 
-    // reconnect
-    if (this.connection != null && !this.connection.connected && !this.isReconnecting) {
+    if (this.shouldResubscribe()) {
       logger.info('The connection to config pubsub server is offline. Try to reconnect...');
       logger.info('The connection to config pubsub server is offline. Try to reconnect...');
-      this.isReconnecting = true;
-
-      const url = this.constructUrl(this.subscribePath).toString();
-      this.client.connect(url.toString());
     }
     }
+
+    // connect
+    this.isConnecting = true;
+    const url = this.constructUrl(this.subscribePath).toString();
+    this.client.connect(url.toString());
   }
   }
 
 
   /**
   /**
@@ -92,24 +99,23 @@ class NchanDelegator extends ConfigPubsubDelegator {
 
 
     client.on('connectFailed', (error) => {
     client.on('connectFailed', (error) => {
       logger.warn(`Connect Error: ${error.toString()}`);
       logger.warn(`Connect Error: ${error.toString()}`);
-      this.isReconnecting = false;
+      this.isConnecting = false;
     });
     });
 
 
     client.on('connect', (connection) => {
     client.on('connect', (connection) => {
-      this.isReconnecting = false;
+      this.isConnecting = false;
+      this.connection = connection;
 
 
       logger.info('WebSocket client connected');
       logger.info('WebSocket client connected');
 
 
       connection.on('error', (error) => {
       connection.on('error', (error) => {
-        this.isReconnecting = false;
+        this.isConnecting = false;
         logger.error(`Connection Error: ${error.toString()}`);
         logger.error(`Connection Error: ${error.toString()}`);
       });
       });
       connection.on('close', () => {
       connection.on('close', () => {
         logger.info('WebSocket connection closed');
         logger.info('WebSocket connection closed');
       });
       });
 
 
-      this.connection = connection;
-
       // register all message handlers
       // register all message handlers
       this.messageHandlers.forEach(handler => this.addMessageHandler(handler));
       this.messageHandlers.forEach(handler => this.addMessageHandler(handler));
     });
     });