Skip to content

Commit 1c22164

Browse files
committed
fix(cmem.l5): use instances cache instead of poolsize
多并发使用连接池发现有队列拥堵现象,使用实例缓存先解决拥堵产生的高延时问题
1 parent 094e32e commit 1c22164

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

bin/tsw/pool/cmem.l5.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ if (!cache) {
2424

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

@@ -57,14 +57,29 @@ module.exports.getCmem = function(opt) {
5757
return null;
5858
}
5959

60+
return fromCache(opt);
61+
};
62+
63+
const fromCache = (opt) => {
6064
const key = [opt.modid, opt.cmd, opt.host].join(':');
6165

6266
if (!cache[key]) {
6367
const Memcached = require('memcached');
64-
cache[key] = queueWrap(new Memcached(opt.host, opt));
68+
const poolSize = opt.poolSize || 1;
69+
const queueWrapList = [];
70+
const option = Object.assign({}, opt, {
71+
poolSize: 1
72+
});
73+
for (let i = 0; i < poolSize; i++) {
74+
queueWrapList.push(queueWrap(new Memcached(opt.host, option)));
75+
}
76+
queueWrapList.curr = 0;
77+
cache[key] = queueWrapList;
78+
} else {
79+
cache[key].curr = (cache[key].curr + 1) % cache[key].length;
6580
}
6681

67-
return cache[key];
82+
return cache[key][cache[key].curr];
6883
};
6984

7085

0 commit comments

Comments
 (0)