All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net_io.core.NetChannelPool Maven / Gradle / Ivy

The newest version!
package net_io.core;

import java.io.IOException;
import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import net_io.utils.MemoryQueueCache;
import net_io.utils.NetLog;

class NetChannelPool {
	// 管理线程ID(所有管理线程中唯一)
	private static AtomicLong threadID = new AtomicLong(0);
	// 内存缓存时间:5秒
	protected static final long CACHE_TIME = 5 * 1000;
	// 新注册的Channel的序列号(全局唯一)
	private static AtomicLong requestChannelSequence = new AtomicLong(0);
	// 管理线程(定时器)
	private Timer timer = new Timer("NetChannelPool-"+threadID.incrementAndGet(), true);
	// Channel信息
	private ConcurrentHashMap chMap;
	//内存缓存对象
	protected MemoryQueueCache memoryCache;
	
	private static final long[] TASK_INTERVAL = {1*1000, 5*1000, 25*1000, 120*1000, 600*1000};
	private Map[] delayCheckList;
	

	@SuppressWarnings("unchecked")
	protected NetChannelPool() {
		chMap = new ConcurrentHashMap(1024); //初始化固定大小的HashMap。key list伸缩
		delayCheckList = new ConcurrentHashMap[TASK_INTERVAL.length];
		for(int i=0; i();
		}
		//缓存队列
		memoryCache = new MemoryQueueCache(timer, CACHE_TIME);
		//设置定时器
		for(int i=0; i map : delayCheckList) {
			map.remove(channel);
		}
		return channel;
	}
	
	
	/**
	 * 检查各个通道的状态,并自动处理
	 */
	private void checkChannelStatus(int index) {
		long currentTime = System.currentTimeMillis();
		for(NetChannel channel : delayCheckList[index].keySet()) {
			long expireTime = channel._checkRemainingAliveTime(currentTime);
			//关闭过期连接
			if(expireTime < 0) {
				try {
					channel.close();
				} catch(Exception e) {
					NetLog.logError("NetChannelPool Exception(1): ");
					NetLog.logError(e);
				}
				remove(channel.socket); //从过期检查队列直接移除
			}
			//根据过期时间移位
			int findIndex = findCheckListIndex(expireTime);
			if(index != findIndex) {
				Boolean flag = delayCheckList[index].remove(channel);
				if(flag == null) {
					continue; //其它线程已移除(仅关闭连接的场合,20180103)
				}
				delayCheckList[findIndex].put(channel, flag);
			}
		}
	}
	
	
	/** 找一个合适的位置 **/
	private int findCheckListIndex(long expireTime) {
		int findIndex = 0;
		for(int i=0; i 0) {
					findIndex = i - 1;
				}
				break;
			}
		}
		return findIndex;
		
	}
	
	/**
	 * 获取 所有的 NetChannel 的数量(含 CLOSE_WAIT )
	 */
	protected int getTotalChannelCount() {
		return chMap.size();
	}
	
}