Skip to content

Commit

Permalink
feat: add an enableThreshold for connectionPoolConfig (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Mar 12, 2019
1 parent 2575a3f commit b0a21ae
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
18 changes: 17 additions & 1 deletion lib/client/address_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ class AddressGroup extends Base {
}
}

_needElasticControl(addressCount) {
const connectionPoolConfig = this.connectionPoolConfig;
if (!connectionPoolConfig) return false;

// 如果地址不够,禁用弹性控制
if (addressCount < connectionPoolConfig.minAddressCount) {
return false;
}

const enableThreshold = connectionPoolConfig.enableThreshold || 50;
// 开启弹性控制有两个条件
// 1. 配置 elasticControl = true
// 2. 当前分组的地址数量要大于开启的阈值(enableThreshold)
return connectionPoolConfig.elasticControl && addressCount > enableThreshold;
}

_chooseAddresses(addressList) {
const newSet = new Set();
const oldSet = new Set();
Expand All @@ -177,7 +193,7 @@ class AddressGroup extends Base {
this._totalSize = this._allAddressList.length;
this._unChoosedAddressList = [];
// 禁用弹性控制直接返回 或者 地址太少,直接返回
if (!this.connectionPoolConfig.elasticControl || this._totalSize < this.connectionPoolConfig.minAddressCount) {
if (!this._needElasticControl(this._totalSize)) {
return this._allAddressList;
}
if (this._connectionPoolSize > this._totalSize) {
Expand Down
4 changes: 2 additions & 2 deletions lib/client/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class RpcConsumer extends Base {
await this._addressGroup.ready();
}

createAddressGroup(key) {
return new AddressGroup(Object.assign({ key }, this.options));
createAddressGroup(key, options) {
return new AddressGroup(Object.assign({ key }, this.options, options));
}

createRequest(method, args, options) {
Expand Down
1 change: 1 addition & 0 deletions lib/client/dynamic_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = {

// 连接池配置
connectionPoolConfig: {
enableThreshold: 50,
minAddressCount: 5,
maxAddressCount: 50,
initConnectionSize: 6,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
"coffee": "^5.2.1",
"contributors": "^0.5.1",
"dubbo-remoting": "^2.1.4",
"egg-bin": "^4.11.0",
"egg-bin": "^4.11.1",
"eslint": "^5.15.1",
"eslint-config-egg": "^7.1.0",
"mm": "^2.4.1",
"eslint-config-egg": "^7.2.0",
"mm": "^2.5.0",
"node-zookeeper-client": "^0.2.2",
"pedding": "^1.1.0"
},
Expand All @@ -68,6 +68,6 @@
},
"ci": {
"type": "travis",
"version": "8, 10"
"version": "8, 10, 11"
}
}
18 changes: 18 additions & 0 deletions test/client/address_group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,5 +1520,23 @@ describe('test/address_group.test.js', () => {
assert(addressGroup.addressList.length === 50);
assert(addressGroup._choosedSize === 50);
});

it('弹性控制', () => {
mm(addressGroup, 'connectionPoolConfig', null);
assert(!addressGroup._needElasticControl(100));
assert(!addressGroup._needElasticControl(10));

mm.restore();

mm(addressGroup.connectionPoolConfig, 'minAddressCount', 10);
assert(!addressGroup._needElasticControl(9));
assert(!addressGroup._needElasticControl(2));

mm(addressGroup.connectionPoolConfig, 'enableThreshold', 50);

assert(addressGroup._needElasticControl(51));
assert(!addressGroup._needElasticControl(50));
assert(!addressGroup._needElasticControl(49));
});
});
});
6 changes: 3 additions & 3 deletions test/client/elastic_control.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const logger = console;
describe('test/client/elastic_control.test.js', () => {
let connectionManager;
let addressGroup;
const count = 10;
const count = 51;

const addressList = [];

Expand Down Expand Up @@ -52,13 +52,13 @@ describe('test/client/elastic_control.test.js', () => {
assert(addressGroup.addressList.length === 2);
mm(utils, 'shuffle', arr => arr);

const newAddress = urlparse('rpc://127.0.0.11:12200');
const newAddress = urlparse('rpc://127.0.0.52:12200');
MockConnection.addAvailableAddress(newAddress);

const newAddressList = [ newAddress ].concat(addressList);
addressGroup.addressList = newAddressList;

assert(addressGroup.addressList.length === 2);
assert(addressGroup.addressList[0].href === 'rpc://127.0.0.11:12200');
assert(addressGroup.addressList[0].href === 'rpc://127.0.0.52:12200');
});
});

0 comments on commit b0a21ae

Please sign in to comment.