DefaultEvictionPolicy類是EvictionPolicy接口的實現主要描述,pool中那些idel對象會被Evict,回收。
關鍵代碼以下:
public boolean evict(EvictionConfig config, PooledObject<T> underTest, int idleCount) { if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() && config.getMinIdle() < idleCount) || config.getIdleEvictTime() < underTest.getIdleTimeMillis()) { return true; } return false; }
ObjectPool 接口:dom
Object obj = null try{ obj = pool.borrowObject(); try{ //...use the object... }catch(Exception e) { pool.invalidateObject(obj);// invalidate the object // do not return the object to the pool twice obj = null }finally{ // make sure the object is returned to the pool if(null!= obj) pool.returnObject(obj);
}
}catch(Exception e) { // failed to borrow an object }
PooledObjectState 定義池對象的全部可能狀態:
public enum PooledObjectState { /** * In the queue, not in use. */ IDLE, /** * In use. */ ALLOCATED, /** * In the queue, currently being tested for possible eviction. */ EVICTION, /** * Not in the queue, currently being tested for possible eviction. An * attempt to borrow the object was made while being tested which removed it * from the queue. It should be returned to the head of the queue once * eviction testing completes. * TODO: Consider allocating object and ignoring the result of the eviction * test. */ EVICTION_RETURN_TO_HEAD, /** * In the queue, currently being validated. */ VALIDATION, /** * Not in queue, currently being validated. The object was borrowed while * being validated and since testOnBorrow was configured, it was removed * from the queue and pre-allocated. It should be allocated once validation * completes. */ VALIDATION_PREALLOCATED, /** * Not in queue, currently being validated. An attempt to borrow the object * was made while previously being tested for eviction which removed it from * the queue. It should be returned to the head of the queue once validation * completes. */ VALIDATION_RETURN_TO_HEAD, /** * Failed maintenance (e.g. eviction test or validation) and will be / has * been destroyed */ INVALID, /** * Deemed abandoned, to be invalidated. */ ABANDONED, /** * Returning to the pool. */ RETURNING }
ThriftClientPool 類剖析:ide
new GenericObjectPool<>(new BasePooledObjectFactory<ThriftClient<T>>() { @Override public ThriftClient<T> create() throws Exception { // get from global list first List<ServiceInfo> serviceList = ThriftClientPool.this.services; ServiceInfo serviceInfo = getRandomService(serviceList); TTransport transport = getTransport(serviceInfo); try { transport.open(); } catch (TTransportException e) { logger.info("transport open fail service: host={}, port={}", serviceInfo.getHost(), serviceInfo.getPort()); if (poolConfig.isFailover()) { while (true) { try { // mark current fail and try next, until none service available serviceList = removeFailService(serviceList, serviceInfo); serviceInfo = getRandomService(serviceList); transport = getTransport(serviceInfo); // while break here logger.info("failover to next service host={}, port={}", serviceInfo.getHost(), serviceInfo.getPort()); transport.open(); break; } catch (TTransportException e2) { logger.warn("failover fail, services left: {}", serviceList.size()); } } } else { throw new ConnectionFailException("host=" + serviceInfo.getHost() + ", ip=" + serviceInfo.getPort(), e); } } ThriftClient<T> client = new ThriftClient<>(clientFactory.createClient(transport), pool, serviceInfo); logger.debug("create new object for pool {}", client); return client; } @Override public PooledObject<ThriftClient<T>> wrap(ThriftClient<T> obj) { return new DefaultPooledObject<>(obj); } @Override public boolean validateObject(PooledObject<ThriftClient<T>> p) { ThriftClient<T> client = p.getObject(); // check if return client in current service list if if (serviceReset) { if (!ThriftClientPool.this.services.contains(client.getServiceInfo())) { logger.warn("not return object cuase it's from previous config {}", client); client.closeClient(); return false; } } return super.validateObject(p); } @Override public void destroyObject(PooledObject<ThriftClient<T>> p) throws Exception { p.getObject().closeClient(); super.destroyObject(p); } }
UML類關係圖:this