]> cat aescling's git repositories - mastodon.git/commitdiff
Fix streaming server. Redis connection subscribe for each channel. (#3828)
author猫吸血鬼ディフリス / 猫ロキP <deflis@gmail.com>
Tue, 20 Jun 2017 18:41:41 +0000 (03:41 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Tue, 20 Jun 2017 18:41:41 +0000 (20:41 +0200)
streaming/index.js

index 270ed6f70343c8670f4ff51ce2fb0ef7edd72c79..5afdd59619c01ece9533c02cb51863a0e1e16a1e 100644 (file)
@@ -115,7 +115,7 @@ const startWorker = (workerId) => {
 
   const subs = {};
 
-  redisSubscribeClient.on('pmessage', (_, channel, message) => {
+  redisSubscribeClient.on('message', (channel, message) => {
     const callbacks = subs[channel];
 
     log.silly(`New message on channel ${channel}`);
@@ -127,8 +127,6 @@ const startWorker = (workerId) => {
     callbacks.forEach(callback => callback(message));
   });
 
-  redisSubscribeClient.psubscribe(`${redisPrefix}timeline:*`);
-
   const subscriptionHeartbeat = (channel) => {
     const interval = 6*60;
     const tellSubscribed = () => {
@@ -144,12 +142,20 @@ const startWorker = (workerId) => {
   const subscribe = (channel, callback) => {
     log.silly(`Adding listener for ${channel}`);
     subs[channel] = subs[channel] || [];
+    if (subs[channel].length === 0) {
+      log.verbose(`Subscribe ${channel}`);
+      redisSubscribeClient.subscribe(channel);
+    }
     subs[channel].push(callback);
   };
 
   const unsubscribe = (channel, callback) => {
     log.silly(`Removing listener for ${channel}`);
     subs[channel] = subs[channel].filter(item => item !== callback);
+    if (subs[channel].length === 0) {
+      log.verbose(`Unsubscribe ${channel}`);
+      redisSubscribeClient.unsubscribe(channel);
+    }
   };
 
   const allowCrossDomain = (req, res, next) => {