private void shutdown() { Thread ctl; synchronized (this) { if (cache == null) return; // close any connections in the pool... even ones that are in use for (CachedConnections cachedConn : getCache().values()) { for (CachedConnection cc : Iterables.concat(cachedConn.reserved.values(), cachedConn.unreserved)) { try { cc.transport.close(); } catch (Exception e) { log.debug("Error closing transport during shutdown", e); } } } // this will render the pool unusable and cause the background thread to exit this.cache = null; ctl = checkThread; } if (ctl != null) { try { ctl.interrupt(); ctl.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
private TTransport getTransport(ThriftTransportKey cacheKey) throws TTransportException { // compute hash code outside of lock, this lowers the time the lock is held cacheKey.precomputeHashCode(); synchronized (this) { // atomically reserve location if it exist in cache CachedConnections ccl = getCache().get(cacheKey); if (ccl == null) { ccl = new CachedConnections(); getCache().put(cacheKey, ccl); } CachedConnection cachedConnection = ccl.reserveAny(); if (cachedConnection != null) { log.trace("Using existing connection to {}", cacheKey.getServer()); return cachedConnection.transport; } } return createNewTransport(cacheKey); }
serversSet.retainAll(getCache().keySet()); CachedConnection cachedConnection = getCache().get(ttk).reserveAny(); if (cachedConnection != null) { final String serverAddr = ttk.getServer().toString(); CachedConnections cachedConns = getCache().get(ttk); if (cachedConns != null) { CachedConnection cachedConnection = cachedConns.reserveAny();
private TTransport createNewTransport(ThriftTransportKey cacheKey) throws TTransportException { TTransport transport = ThriftUtil.createClientTransport(cacheKey.getServer(), (int) cacheKey.getTimeout(), cacheKey.getSslParams(), cacheKey.getSaslParams()); log.trace("Creating new connection to connection to {}", cacheKey.getServer()); CachedTTransport tsc = new CachedTTransport(transport, cacheKey); CachedConnection cc = new CachedConnection(tsc); cc.reserve(); try { synchronized (this) { CachedConnections cachedConns = getCache().get(cacheKey); if (cachedConns == null) { cachedConns = new CachedConnections(); getCache().put(cacheKey, cachedConns); } cachedConns.reserved.put(cc.transport, cc); } } catch (TransportPoolShutdownException e) { cc.transport.close(); throw e; } return cc.transport; }
CachedConnections cachedConns = getCache().get(ctsc.getCacheKey()); if (cachedConns != null) { CachedConnection cachedConnection = cachedConns.reserved.remove(ctsc);