From 279559ff976eed37d788502830cb18adb5787b2f Mon Sep 17 00:00:00 2001 From: Nguyen Trung Quan Date: Fri, 7 Oct 2016 11:14:28 +0700 Subject: [PATCH] Fix: Memory leak & connection flood when access /admin/advanced/database --- src/controllers/admin/database.js | 12 +++++++++++- src/database/mongo.js | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/controllers/admin/database.js b/src/controllers/admin/database.js index d15db3f09c..3e89298813 100644 --- a/src/controllers/admin/database.js +++ b/src/controllers/admin/database.js @@ -12,7 +12,17 @@ databaseController.get = function(req, res, next) { redis: function(next) { if (nconf.get('redis')) { var rdb = require('../../database/redis'); - var cxn = rdb.connect(); + // var cxn = rdb.connect(); + // Do not create new client if it exist. + // This will cause memory leak & connection flood + var cxn = rdb.client; + // Redis was initalized. + // Check cxn is unnecessary. + // if (!cxn) { + // cxn = rdb.client; + // rdb.client = cxn; + // } + rdb.info(cxn, next); } else { next(); diff --git a/src/database/mongo.js b/src/database/mongo.js index 25d34ce02b..c731e62d18 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -111,8 +111,14 @@ db: db }); } else { + // Initial Redis database + var rdb = require('./redis'); + // Create a new redis connection and store it in module (skeleton) + rdb.client = rdb.connect(); + + // module.sessionStore = new sessionStore({ - client: require('./redis').connect(), + client: rdb.client, ttl: 60 * 60 * 24 * 14 }); }