mirror of
https://github.com/sofastack/sofa-rpc-node.git
synced 2026-01-25 16:06:26 +00:00
refactor: support child class overwrite connections mng (#75)
pick from https://github.com/sofastack/sofa-rpc-node/pull/75
This commit is contained in:
parent
6717e4f9d9
commit
ef9ffa8ac7
@ -46,13 +46,25 @@ class ConnectionManager extends Base {
|
||||
* @return {Connection} 连接对象
|
||||
*/
|
||||
get(address) {
|
||||
const conn = this._connections.get(address.host);
|
||||
const conn = this.doGetConn(address);
|
||||
if (conn && conn.isConnected) {
|
||||
return conn;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
doGetConn(address) {
|
||||
return this._connections.get(address.host);
|
||||
}
|
||||
|
||||
doSetConn(address, conn) {
|
||||
return this._connections.set(address.host, conn);
|
||||
}
|
||||
|
||||
doDeleteConn(address) {
|
||||
this._connections.delete(address.host);
|
||||
}
|
||||
|
||||
/**
|
||||
* 若不存在,则创建新的连接
|
||||
*
|
||||
@ -63,14 +75,14 @@ class ConnectionManager extends Base {
|
||||
*/
|
||||
async createAndGet(address, options, connectionClass) {
|
||||
connectionClass = connectionClass || this.options.connectionClass;
|
||||
let conn = this._connections.get(address.host);
|
||||
let conn = this.doGetConn(address);
|
||||
if (!conn) {
|
||||
conn = new connectionClass(Object.assign({
|
||||
address,
|
||||
logger: this.logger,
|
||||
}, options));
|
||||
this._connections.set(address.host, conn);
|
||||
conn.once('close', () => { this._connections.delete(address.host); });
|
||||
this.doSetConn(address, conn);
|
||||
conn.once('close', () => { this.doDeleteConn(address); });
|
||||
conn.once('error', err => { this.emit('error', err); });
|
||||
}
|
||||
if (!conn.isConnected) {
|
||||
@ -84,7 +96,7 @@ class ConnectionManager extends Base {
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.warn('[ConnectionManager] create connection: ' + address.href + ' failed, caused by ' + err.message);
|
||||
this._connections.delete(address.host);
|
||||
this.doDeleteConn(address);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user