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);
}