@Override public void onResponse(Void aVoid) { CloseableChannel.closeChannel(channel); }
/** * Closes the channel. * * @param channel to close * @param blocking indicates if we should block on channel close */ static <C extends CloseableChannel> void closeChannel(C channel, boolean blocking) { closeChannels(Collections.singletonList(channel), blocking); }
/** * Closes the channels. * * @param channels to close * @param blocking indicates if we should block on channel close */ static <C extends CloseableChannel> void closeChannels(List<C> channels, boolean blocking) { try { IOUtils.close(channels); } catch (IOException e) { // The CloseableChannel#close method does not throw IOException, so this should not occur. throw new AssertionError(e); } if (blocking) { ArrayList<ActionFuture<Void>> futures = new ArrayList<>(channels.size()); for (final C channel : channels) { PlainActionFuture<Void> closeFuture = PlainActionFuture.newFuture(); channel.addCloseListener(closeFuture); futures.add(closeFuture); } blockOnFutures(futures); } }
@Override public Releasable openConnection(DiscoveryNode node, ConnectionProfile profile, ActionListener<Transport.Connection> listener) { Objects.requireNonNull(profile, "connection profile cannot be null"); if (node == null) { throw new ConnectTransportException(null, "can't open connection to a null node"); } ConnectionProfile finalProfile = maybeOverrideConnectionProfile(profile); closeLock.readLock().lock(); // ensure we don't open connections while we are closing try { ensureOpen(); List<TcpChannel> pendingChannels = initiateConnection(node, finalProfile, listener); return () -> CloseableChannel.closeChannels(pendingChannels, false); } finally { closeLock.readLock().unlock(); } }
/** * Closes the channel without blocking. * * @param channel to close */ static <C extends CloseableChannel> void closeChannel(C channel) { closeChannel(channel, false); }
@Override public void close() { if (isClosing.compareAndSet(false, true)) { try { boolean block = lifecycle.stopped() && Transports.isTransportThread(Thread.currentThread()) == false; CloseableChannel.closeChannels(channels, block); } finally { // Call the super method to trigger listeners super.close(); } } }
@Override public void onFailure(Exception e) { logger.debug("failed to send message to httpOnTransport channel", e); CloseableChannel.closeChannel(channel); } };
e -> logger.warn(() -> new ParameterizedMessage("Error closing serverChannel for profile [{}]", profile), e)); channels.forEach(c -> c.addCloseListener(closeFailLogger)); CloseableChannel.closeChannels(channels, true); CloseableChannel.closeChannels(new ArrayList<>(acceptedChannels), true); acceptedChannels.clear();
public void onException(TcpChannel channel, Exception e) { if (!lifecycle.started()) { CloseableChannel.closeChannel(channel); return; "close connection exception caught on transport layer [{}], disconnecting from relevant node", channel), e); CloseableChannel.closeChannel(channel); } else if (isConnectException(e)) { logger.trace(() -> new ParameterizedMessage("connect exception caught on transport layer [{}]", channel), e); CloseableChannel.closeChannel(channel); } else if (e instanceof BindException) { logger.trace(() -> new ParameterizedMessage("bind exception caught on transport layer [{}]", channel), e); CloseableChannel.closeChannel(channel); } else if (e instanceof CancelledKeyException) { logger.trace(() -> new ParameterizedMessage( "cancelled key exception caught on transport layer [{}], disconnecting from relevant node", channel), e); CloseableChannel.closeChannel(channel); } else if (e instanceof TcpTransport.HttpOnTransportException) { logger.warn(() -> new ParameterizedMessage("exception caught on transport layer [{}], closing connection", channel), e); CloseableChannel.closeChannel(channel);
@Override public void onFailure(Exception ex) { if (countDown.fastForward()) { CloseableChannel.closeChannels(channels, false); listener.onFailure(new ConnectTransportException(node, "connect_exception", ex)); } }
@Override public void onFailure(Exception e) { CloseableChannel.closeChannels(channels, false); if (e instanceof ConnectTransportException) { listener.onFailure(e); } else { listener.onFailure(new ConnectTransportException(node, "general node connection failure", e)); } } });
CloseableChannel.closeChannels(channels, false); listener.onFailure(ex);
public void onTimeout() { if (countDown.fastForward()) { CloseableChannel.closeChannels(channels, false); listener.onFailure(new ConnectTransportException(node, "connect_timeout[" + connectionProfile.getConnectTimeout() + "]")); } } }
private List<TcpChannel> initiateConnection(DiscoveryNode node, ConnectionProfile connectionProfile, ActionListener<Transport.Connection> listener) { int numConnections = connectionProfile.getNumConnections(); assert numConnections > 0 : "A connection profile must be configured with at least one connection"; final List<TcpChannel> channels = new ArrayList<>(numConnections); for (int i = 0; i < numConnections; ++i) { try { TcpChannel channel = initiateChannel(node); logger.trace(() -> new ParameterizedMessage("Tcp transport client channel opened: {}", channel)); channels.add(channel); } catch (ConnectTransportException e) { CloseableChannel.closeChannels(channels, false); listener.onFailure(e); return channels; } catch (Exception e) { CloseableChannel.closeChannels(channels, false); listener.onFailure(new ConnectTransportException(node, "general node connection failure", e)); return channels; } } ChannelsConnectedListener channelsConnectedListener = new ChannelsConnectedListener(node, connectionProfile, channels, listener); for (TcpChannel channel : channels) { channel.addConnectListener(channelsConnectedListener); } TimeValue connectTimeout = connectionProfile.getConnectTimeout(); threadPool.schedule(connectTimeout, ThreadPool.Names.GENERIC, channelsConnectedListener::onTimeout); return channels; }