Forráskód Böngészése

[bugfix] remove unused connection from manager

B Thibault 8 éve
szülő
commit
9a085688fd
2 módosított fájl, 30 hozzáadás és 47 törlés
  1. 11 40
      srv/src/slack.js
  2. 19 7
      srv/src/slackManager.js

+ 11 - 40
srv/src/slack.js

@@ -37,7 +37,6 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
         }
     }
     ,HISTORY_LENGTH = 35
-    ,INACTIVE_DELAY = 120000
 
     ,UPDATE_LIVE = [
         "message"
@@ -50,30 +49,15 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
     ] // Message type that affect live history
 ;
 
-var
-    SLACK_SESSIONS = []
-;
-
-setInterval(function() {
-    var t = Date.now();
-    SLACK_SESSIONS.forEach(function(slackInst) {
-        if (!slackInst.closeIfInnactive(t) && slackInst.active)
-            slackInst.sendActive();
-    });
-}, 60000);
-
-function Slack(sess) {
+function Slack(sess, manager) {
     this.token = sess.slackToken;
+    this.sessId = sess.id;
+    this.manager = manager;
     this.rtm = null;
     this.data = new SlackData(this);
     this.history = {};
     this.connected = false;
-    this.active = true;
-    this.lastPing = Date.now();
-}
-
-Slack.prototype.onUserInterract = function(t) {
-    this.lastPing = Math.max(t, this.lastPing);
+    this.closing = false;
 }
 
 Slack.prototype.onRequest = function(knownVersion, cb) {
@@ -282,7 +266,6 @@ Slack.prototype.connectRtm = function(url, cb) {
         }
         if (!_this.connected) {
             _this.connected = true;
-            SLACK_SESSIONS.push(_this);
         }
         _this.onMessage(JSON.parse(msg), Date.now());
     });
@@ -290,8 +273,6 @@ Slack.prototype.connectRtm = function(url, cb) {
         _this.connected = false;
         console.error(e);
         _this.close();
-        var arrIndex = SLACK_SESSIONS.indexOf(_this);
-        if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
     });
     this.rtm.once("end", function() {
         console.error("RTM hang up");
@@ -300,30 +281,20 @@ Slack.prototype.connectRtm = function(url, cb) {
 };
 
 Slack.prototype.onClose = function() {
-    var arrIndex = SLACK_SESSIONS.indexOf(this);
-    this.connected = false;
-    if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
+    this.manager.suicide(this);
 };
 
-/**
- * @return {boolean} true if innactive and closeding
-**/
-Slack.prototype.closeIfInnactive = function(t) {
-    if (t - this.lastPing >INACTIVE_DELAY) {
-        this.close();
-        return true;
-    }
-    return false;
-};
-
-Slack.prototype.sendActive = function() {
+Slack.prototype.ping = function() {
     httpsRequest(SLACK_ENDPOINT+GETAPI.setActive
         +"?token=" +this.token);
 };
 
 Slack.prototype.close = function() {
-    this.rtm.close();
-    this.onClose();
+    if (!this.closing) {
+        this.closing = true;
+        this.rtm.close();
+        this.onClose();
+    }
 };
 
 Slack.getOauthToken = function(code, redirectUri, cb) {

+ 19 - 7
srv/src/slackManager.js

@@ -1,12 +1,20 @@
 
-const cache = require('memory-cache')
-    ,Slack = require('./slack.js').Slack;
+const Slack = require('./slack.js').Slack
+    ,INACTIVE_DELAY = 120000
 
 function SlackManager() {
+    this.cache = require('memory-cache');
 }
 
 var instance = new SlackManager();
 
+setInterval(function() {
+    var keys = instance.cache.keys();
+    keys.forEach(function(instKey) {
+        instance.cache.get(instKey).ping();
+    });
+}, 60000);
+
 SlackManager.prototype.getScope = function() {
     return [
         "client"
@@ -15,18 +23,18 @@ SlackManager.prototype.getScope = function() {
 
 SlackManager.prototype.lazyGet = function(sess, reqT) {
     var key = "SLACK_" +sess.sessId
-        ,val = cache.get(key);
+        ,val = this.cache.get(key);
     if (val) {
-        cache.put(key, val, 1000 * 60 * 60 * 24, function(key, val) {
+        this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
             instance.removed(key, val);
         });
     } else {
-        val = new Slack(sess);
-        cache.put(key, val, 1000 * 60 * 60 * 24, function(key, val) {
+        val = new Slack(sess, this);
+        this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
             instance.removed(key, val);
+            val.close();
         });
     }
-    val.onUserInterract(reqT);
     return val;
 };
 
@@ -34,5 +42,9 @@ SlackManager.prototype.removed = function(key, slack) {
     slack.close();
 };
 
+SlackManager.prototype.suicide = function(slack) {
+    this.cache.del("SLACK_" +slack.sessId);
+};
+
 module.exports.SlackManager = instance;