public boolean removeAll(Channel connection) { return channelPool.removeAll(connection); }
public void flushPartitions(ChannelPoolPartitionSelector selector) { channelPool.flushPartitions(selector); } }
public void flushPartition(String partitionId) { channelPool.flushPartition(partitionId); }
public boolean offer(Channel channel, Object partitionKey) { if (isClosed.get() || (!sslConnectionPoolEnabled && channel.getPipeline().get(SslHandler.class) != null)) return false; long now = millisTime(); if (isTTLExpired(channel, now)) return false; boolean added = partitions.computeIfAbsent(partitionKey, PARTITION_COMPUTER).add(new IdleChannel(channel, now)); if (added) channelId2Creation.putIfAbsent(channel.getId(), new ChannelCreation(now, partitionKey)); return added; }
public DefaultChannelPool(// long maxIdleTime,// int maxConnectionTTL,// boolean sslConnectionPoolEnabled,// Timer nettyTimer) { this.sslConnectionPoolEnabled = sslConnectionPoolEnabled; this.maxIdleTime = maxIdleTime; this.maxConnectionTTL = maxConnectionTTL; maxConnectionTTLDisabled = maxConnectionTTL <= 0; this.nettyTimer = nettyTimer; maxIdleTimeDisabled = maxIdleTime <= 0; cleanerPeriod = Math.min(maxConnectionTTLDisabled ? Long.MAX_VALUE : maxConnectionTTL, maxIdleTimeDisabled ? Long.MAX_VALUE : maxIdleTime); if (!maxConnectionTTLDisabled || !maxIdleTimeDisabled) scheduleNewIdleChannelDetector(new IdleChannelDetector()); }
totalCount += partition.size(); List<IdleChannel> closedChannels = closeChannels(expiredChannels(partition, start));
channelPool = new DefaultChannelPool(config, nettyTimer); } else if (channelPool == null) { channelPool = new NoopChannelPool();
private void flushPartition(Object partitionKey, ConcurrentLinkedQueue<IdleChannel> partition) { if (partition != null) { partitions.remove(partitionKey); for (IdleChannel idleChannel : partition) close(idleChannel.channel); } }
public Channel poll(Uri uri, ProxyServer proxy, ConnectionPoolPartitioning connectionPoolPartitioning) { Object partitionKey = connectionPoolPartitioning.getPartitionKey(uri, proxy); return channelPool.poll(partitionKey); }
@Override public void flushPartitions(ChannelPoolPartitionSelector selector) { for (Map.Entry<Object, ConcurrentLinkedQueue<IdleChannel>> partitionsEntry : partitions.entrySet()) { Object partitionKey = partitionsEntry.getKey(); if (selector.select(partitionKey)) flushPartition(partitionKey, partitionsEntry.getValue()); } } }
public void preemptChannel(Object partitionKey) throws IOException { if (!channelPool.isOpen()) throw poolAlreadyClosed; if (!tryAcquireGlobal()) throw tooManyConnections; if (!tryAcquirePerHost(partitionKey)) { if (maxTotalConnectionsEnabled) freeChannels.release(); throw tooManyConnectionsPerHost; } }
private List<IdleChannel> expiredChannels(ConcurrentLinkedQueue<IdleChannel> partition, long now) { // lazy create List<IdleChannel> idleTimeoutChannels = null; for (IdleChannel idleChannel : partition) { if (isTTLExpired(idleChannel.channel, now) || isIdleTimeoutExpired(idleChannel, now) || !Channels.isChannelValid(idleChannel.channel)) { LOGGER.debug("Adding Candidate expired Channel {}", idleChannel.channel); if (idleTimeoutChannels == null) idleTimeoutChannels = new ArrayList<>(); idleTimeoutChannels.add(idleChannel); } } return idleTimeoutChannels != null ? idleTimeoutChannels : Collections.<IdleChannel> emptyList(); }
public final void tryToOfferChannelToPool(Channel channel, boolean keepAlive, Object partitionKey) { if (channel.isConnected() && keepAlive && channel.isReadable()) { LOGGER.debug("Adding key: {} for channel {}", partitionKey, channel); Channels.setDiscard(channel); if (channelPool.offer(channel, partitionKey)) { if (maxConnectionsPerHostEnabled) channelId2PartitionKey.putIfAbsent(channel.getId(), partitionKey); } else { // rejected by pool closeChannel(channel); } } else { // not offered closeChannel(channel); } }
public void close() { channelPool.destroy(); openChannels.close(); for (Channel channel : openChannels) { Object attribute = Channels.getAttribute(channel); if (attribute instanceof NettyResponseFuture<?>) { NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute; future.cancelTimeouts(); } } // FIXME also shutdown in provider config.executorService().shutdown(); if (allowReleaseSocketChannelFactory) { socketChannelFactory.releaseExternalResources(); plainBootstrap.releaseExternalResources(); secureBootstrap.releaseExternalResources(); webSocketBootstrap.releaseExternalResources(); secureWebSocketBootstrap.releaseExternalResources(); } }
private final List<IdleChannel> closeChannels(List<IdleChannel> candidates) { // lazy create, only if we have a non-closeable channel List<IdleChannel> closedChannels = null; for (int i = 0; i < candidates.size(); i++) { // We call takeOwnership here to avoid closing a channel that has just been taken out // of the pool, otherwise we risk closing an active connection. IdleChannel idleChannel = candidates.get(i); if (idleChannel.takeOwnership()) { LOGGER.debug("Closing Idle Channel {}", idleChannel.channel); close(idleChannel.channel); if (closedChannels != null) { closedChannels.add(idleChannel); } } else if (closedChannels == null) { // first non closeable to be skipped, copy all previously skipped closeable channels closedChannels = new ArrayList<>(candidates.size()); for (int j = 0; j < i; j++) closedChannels.add(candidates.get(j)); } } return closedChannels != null ? closedChannels : candidates; }
@Override public void flushPartition(Object partitionKey) { flushPartition(partitionKey, partitions.get(partitionKey)); }
public boolean removeAll(Channel connection) { return channelPool.removeAll(connection); }
@Override public void destroy() { if (isClosed.getAndSet(true)) return; for (ConcurrentLinkedQueue<IdleChannel> partition : partitions.values()) { for (IdleChannel idleChannel : partition) close(idleChannel.channel); } partitions.clear(); channelId2Creation.clear(); }
public Channel poll(String uri) { return channelPool.poll(uri); }
public Channel poll(Object partitionKey) { IdleChannel idleChannel = null; ConcurrentLinkedQueue<IdleChannel> partition = partitions.get(partitionKey); if (partition != null) { while (idleChannel == null) { idleChannel = partition.poll(); if (idleChannel == null) { // pool is empty break; } else if (!Channels.isChannelValid(idleChannel.channel)) { idleChannel = null; LOGGER.trace("Channel not connected or not opened, probably remotely closed!"); } else if (!idleChannel.takeOwnership()) { idleChannel = null; LOGGER.trace("Couldn't take ownership of channel, probably in the process of being expired!"); } } } return idleChannel != null ? idleChannel.channel : null; }