在memcached中的连接池,是com.schooner.MemCached.SchoonerSockIOPool
在SchoonerSockIOPool中,
// store instances of pools
private static ConcurrentMap<String, SchoonerSockIOPool> pools = new ConcurrentHashMap<String, SchoonerSockIOPool>();
pools保存目前所有SchoonerSockIOPool,是个ConcurrentMap对象;key=poolName,value=Pool;
缺省的名字叫做default;
在设置好服务器后,需要调用初始化函数:
/**
* Initializes the pool.
*/
public void initialize() {
initDeadLock.lock();
try {
// if servers is not set, or it empty, then
// throw a runtime exception
if (servers == null || servers.length <= 0) {
log.error("++++ trying to initialize with no servers");
throw new IllegalStateException("++++ trying to initialize with no servers");
}
// pools
socketPool = new HashMap<String, GenericObjectPool>(servers.length);
hostDead = new ConcurrentHashMap<String, Date>();
hostDeadDur = new ConcurrentHashMap<String, Long>();
// only create up to maxCreate connections at once
// initalize our internal hashing structures
if (this.hashingAlg == CONSISTENT_HASH)
populateConsistentBuckets();
else
populateBuckets();
// mark pool as initialized
this.initialized = true;
} finally {
initDeadLock.unlock();
}
}
this.initialized = true;
-------------------------------------------------------------------------------------------
两者差别在于初始化的对象不同:
populateConsistentBuckets:初始化consistentBuckets(TreeMap<Long, String>)
populateBuckets : 初始化buckets(List<String>)
populateConsistentBuckets或者populateBuckets这2个函数在连接Server的处理上是一样的处理方式:
// Create a socket pool for each host
// Create an object pool to contain our active connections
GenericObjectPool gop;
SchoonerSockIOFactory factory;
if (authInfo != null) {
factory = new AuthSchoonerSockIOFactory(servers[i], isTcp, bufferSize, socketTO,
socketConnectTO,nagle, authInfo);
} else {
factory = new SchoonerSockIOFactory(servers[i], isTcp, bufferSize, socketTO,
socketConnectTO, nagle);
}
gop = new GenericObjectPool(factory, maxConn, GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
maxIdle, maxConn);
factory.setSockets(gop);
socketPool.put(servers[i], gop);
}
创建Socket的2个Factory类: AuthSchoonerSockIOFactory SchoonerSockIOFactory
AuthSchoonerSockIOFactory 继承 SchoonerSockIOFactory 继承 BasePoolableObjectFactory
BasePoolableObjectFactory 是 Apache 的commonpool对象了