Skip to content

Commit

Permalink
fix(cmem.l5): use instances cache instead of poolsize
Browse files Browse the repository at this point in the history
多并发使用连接池发现有队列拥堵现象,使用实例缓存先解决拥堵产生的高延时问题
  • Loading branch information
RobinzZH committed Jul 11, 2018
1 parent 094e32e commit 1c22164
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions bin/tsw/pool/cmem.l5.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ if (!cache) {

module.exports = function(opt) {
/**
* 这里像这样写的目的主要是为了进行测试
因为在使用sinon.js时, 如果你exports的是一个function,你就无法进行stub,
*/
* 这里像这样写的目的主要是为了进行测试
因为在使用sinon.js时, 如果你exports的是一个function,你就无法进行stub,
*/
return module.exports.getCmem(opt);
};

Expand Down Expand Up @@ -57,14 +57,29 @@ module.exports.getCmem = function(opt) {
return null;
}

return fromCache(opt);
};

const fromCache = (opt) => {
const key = [opt.modid, opt.cmd, opt.host].join(':');

if (!cache[key]) {
const Memcached = require('memcached');
cache[key] = queueWrap(new Memcached(opt.host, opt));
const poolSize = opt.poolSize || 1;
const queueWrapList = [];
const option = Object.assign({}, opt, {
poolSize: 1
});
for (let i = 0; i < poolSize; i++) {
queueWrapList.push(queueWrap(new Memcached(opt.host, option)));
}
queueWrapList.curr = 0;
cache[key] = queueWrapList;
} else {
cache[key].curr = (cache[key].curr + 1) % cache[key].length;
}

return cache[key];
return cache[key][cache[key].curr];
};


Expand Down

0 comments on commit 1c22164

Please sign in to comment.