【memcached】中的连接池初始化

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

	}
 

 

主要是 populateConsistentBuckets或者 populateBuckets,
创建实际的Socket连接


不过这个函数有个问题,最后
			this.initialized = true;
但是在入口处没有判断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对象了

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值