Sofa-rpc版本5.6.1
如下ConnectionManager定义了创建/删除Connection的方法,默认实现时DefaultConnectionManager
如下List-1和List-2,创建ConnectionPool实际是在ConectionPoolCall(实现了Callable)里面执行的,之后把ConnectionPool放入connTasks中,这是全局变量,ConnectionPool中再加入Connection
List-1
public void add(Connection connection, String poolKey) {
ConnectionPool pool = null;
try {
// get or create an empty connection pool
pool = this.getConnectionPoolAndCreateIfAbsent(poolKey, new ConnectionPoolCall());
} catch (Exception e) {
// should not reach here.
logger.error(
"[NOTIFYME] Exception occurred when getOrCreateIfAbsent an empty ConnectionPool!",
e);
}
if (pool != null) {
pool.add(connection);
}
...
}
List-2
private ConnectionPool getConnectionPoolAndCreateIfAbsent(String poolKey,
Callable<ConnectionPool> callable)
throws RemotingException,
InterruptedException {
RunStateRecordedFutureTask<ConnectionPool> initialTask = null;
ConnectionPool pool = null;
int retry = DEFAULT_RETRY_TIMES;
int timesOfResultNull = 0;
int timesOfInterrupt = 0;
for (int i = 0; (i < retry) && (pool == null); ++i) {
initialTask = this.connTasks.get(poolKey);
if (null == initialTask) {
initialTask = new RunStateRecordedFutureTask<ConnectionPool>(callable);
initialTask = this.connTasks.putIfAbsent(poolKey, initialTask);
if (null == initialTask) {
initialTask = this.connTasks.get(poolKey);
initialTask.run();
}
}
...
return pool;
}
create方法如下,内部其实是使用ConnectionFactory进行创建的。
List-3
@Override
public Connection create(String ip, int port, int connectTimeout) throws RemotingException {
Connection conn = null;
try {
conn = this.connectionFactory.createConnection(ip, port, connectTimeout);
} catch (Exception e) {
throw new RemotingException("Create connection failed. The address is " + ip + ":"
+ port, e);
}
return conn;
}
/**
* @see com.alipay.remoting.ConnectionManager#create(java.lang.String, int)
*/
@Override
public Connection create(String address, int connectTimeout) throws RemotingException {
Url url = this.addressParser.parse(address);
url.setConnectTimeout(connectTimeout);
return create(url);
}