Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/client/address_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,34 @@ class AddressGroup extends Base {
return weight;
}

async getFilterConnection(req) {
try {
return await this.getConnectionDefault(req, true);
} catch (error) {
// 兜底,connect异常,说明host不可用
if (error.message === "Cannot read property 'host' of undefined" && error.name === 'TypeError') {
Comment thread
tyouzu1 marked this conversation as resolved.
Outdated
this.logger.info('[AddressGroup] getFilterConnection error, will retry getConnectionDefault', error);
return null;
}
throw error;
}
}

async getConnection(req) {
const hasFilter = typeof this.options.balancerFilter === 'function';
// 增加一层filter判断,若正常则直接返回,否则走原 getConnection 逻辑
if (hasFilter) {
const filterConnection = await this.getFilterConnection(req);
if (filterConnection) return filterConnection;
}
return await this.getConnectionDefault(req);
}

async getConnectionDefault(req, needFilter) {
const meta = req.meta;
meta.connectionGroup = this.key;

const address = this._loadbalancer.select(req);
const address = this._loadbalancer.select(req, needFilter);
if (!address) return null;

const { connectionOpts, connectionClass } = this.options;
Expand Down
16 changes: 15 additions & 1 deletion lib/client/loadbalancer/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ class LoadBalancer extends Base {
return this.addressGroup.logger;
}

select(request) {
select(request, needFilter) {
// 需要时才需要过滤
const hasFilter = needFilter && typeof this.addressGroup.options.balancerFilter === 'function';
// 透出addressList,供外部进行一次优先筛选
if (hasFilter) {
const list = this.addressGroup.options.balancerFilter(
this.addressList
);
if (Array.isArray(list)) {
// 若优先筛选后无可用,重新尝试全部地址
if (list.length === 0) return this._doSelect(request, this.addressList);
// list.length === 1 也进行一次 doSelect,有可能getWeight异常
return this._doSelect(request, list);
}
}
if (this.size === 0) return null;
if (this.size === 1) return this.addressList[0];

Expand Down
71 changes: 71 additions & 0 deletions test/client/address_group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,77 @@ describe('test/client/address_group.test.js', () => {
await utils.closeAll();
});

it('balancerFilter 优先匹配', async function() {
await Promise.all([
utils.startServer(13201),
utils.startServer(13202),
]);

const addressGroup = new AddressGroup({
key: 'xxx',
logger,
connectionManager,
balancerFilter: addressList => {
return addressList.filter(v => {
return v.host === '127.0.0.1:13202';
});
},
});
addressGroup.addressList = [
urlparse('bolt://127.0.0.1:13201', true),
urlparse('bolt://127.0.0.1:13202', true),
];
let count = 10;
while (count--) {
const connection = await addressGroup.getConnection(req);
assert(connection && connection.isConnected);
// 优先匹配
assert(connection.url === 'bolt://127.0.0.1:13202');
}
addressGroup.addressList = [
urlparse('bolt://127.0.0.1:13201', true),
];
count = 3;
while (count--) {
const connection = await addressGroup.getConnection(req);
assert(connection && connection.isConnected);
// 优先匹配
assert(connection.url === 'bolt://127.0.0.1:13201');
}
addressGroup.close();
await connectionManager.closeAllConnections();
await utils.closeAll();
});

it('balancerFilter 优先匹配 异常', async function() {
await Promise.all([
utils.startServer(13201),
utils.startServer(13202),
]);

const addressGroup = new AddressGroup({
key: 'xxx',
logger,
connectionManager,
balancerFilter: addressList => {
return addressList.map(() => [ 1 ]);
},
});
// 使用错误地址
addressGroup.addressList = [
urlparse('bolt://127.0.0.1:132011', true),
urlparse('bolt://127.0.0.1:132022', true),
];
try {
await addressGroup.getConnection(req);
} catch (error) {
assert(error.code === 'ERR_SOCKET_BAD_PORT');
}
addressGroup.close();
await connectionManager.closeAllConnections();
await utils.closeAll();
});

describe('对于连不上地址的处理', () => {
const mod = 2;
const count = 10;
Expand Down