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); }
CachedConnection cachedConnection = getCache().get(ttk).reserveAny(); if (cachedConnection != null) { final String serverAddr = ttk.getServer().toString(); log.trace("Using existing connection to {}", serverAddr); return new Pair<>(serverAddr, cachedConnection.transport); CachedConnection cachedConnection = cachedConns.reserveAny(); if (cachedConnection != null) { final String serverAddr = ttk.getServer().toString(); return new Pair<>(serverAddr, cachedConnection.transport); return new Pair<>(ttk.getServer().toString(), createNewTransport(ttk)); } catch (TTransportException tte) { log.debug("Failed to connect to {}", servers.get(index), tte);
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; }