public void attachChannel(Channel channel, boolean reuseChannel) { // future could have been cancelled first if (isDone()) { Channels.silentlyCloseChannel(channel); } this.channel = channel; this.reuseChannel = reuseChannel; }
@Override public boolean add(Channel channel) { // Synchronization must occur to avoid add() and close() overlap (thus potentially leaving one channel open). // This could also be done by synchronizing the method itself but using a read lock here (rather than a // synchronized() block) allows multiple concurrent calls to add(). this.lock.readLock().lock(); try { if (this.closed.get()) { // Immediately close channel, as close() was already called. Channels.silentlyCloseChannel(channel); return false; } return super.add(channel); } finally { this.lock.readLock().unlock(); } } }
public void closeChannel(Channel channel) { // The channel may have already been removed from the future if a timeout occurred, and this method may be called just after. LOGGER.debug("Closing Channel {} ", channel); try { removeAll(channel); Channels.setDiscard(channel); Channels.silentlyCloseChannel(channel); } catch (Throwable t) { LOGGER.debug("Error closing a connection", t); } openChannels.remove(channel); }
private boolean abortOnThrowable(Throwable cause, Channel channel) { if (cause != null && future.getState() != NettyResponseFuture.STATE.NEW) { // The write operation failed. If the channel was cached, it means it got asynchronously closed. // Let's retry a second time. if (cause instanceof IllegalStateException || cause instanceof ClosedChannelException || StackTraceInspector.recoverOnReadOrWriteException(cause)) { LOGGER.debug(cause == null ? "" : cause.getMessage(), cause); Channels.silentlyCloseChannel(channel); } else { future.abort(cause); } return true; } return false; }
@Override public boolean cancel(boolean force) { cancelTimeouts(); if (isCancelled.getAndSet(true)) return false; // cancel could happen before channel was attached if (channel != null) { Channels.setDiscard(channel); Channels.silentlyCloseChannel(channel); } if (!onThrowableCalled.getAndSet(true)) { try { asyncHandler.onThrowable(new CancellationException()); } catch (Throwable t) { LOGGER.warn("cancel", t); } } latch.countDown(); runListeners(); return true; }
private void close(Channel channel) { // FIXME pity to have to do this here Channels.setDiscard(channel); channelId2Creation.remove(channel.getId()); Channels.silentlyCloseChannel(channel); }
@Override public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) throws Exception { // call super to reset the read timeout super.messageReceived(ctx, e); Channel channel = ctx.getChannel(); Object attribute = Channels.getAttribute(channel); if (attribute instanceof Callback) { Object message = e.getMessage(); Callback ac = (Callback) attribute; if (message instanceof HttpChunk) { // the AsyncCallable is to be processed on the last chunk if (HttpChunk.class.cast(message).isLast()) // process the AsyncCallable before passing the message to the protocol ac.call(); // FIXME remove attribute? } else { LOGGER.info("Received unexpected message while expecting a chunk: " + message); ac.call(); Channels.setDiscard(channel); } } else if (attribute instanceof NettyResponseFuture<?>) { NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute; protocol.handle(channel, future, e.getMessage()); } else if (attribute != DiscardEvent.INSTANCE) { // unhandled message LOGGER.debug("Orphan channel {} with attribute {} received message {}, closing", channel, attribute, e.getMessage()); Channels.silentlyCloseChannel(channel); } }
if (!requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel)) Channels.silentlyCloseChannel(channel); return;
public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) { NettyRequest nettyRequest = future.getNettyRequest(); HttpRequest httpRequest = nettyRequest.getHttpRequest(); AsyncHandler<T> handler = future.getAsyncHandler(); // if the channel is dead because it was pooled and the remote // server decided to close it, // we just let it go and the channelInactive do its work if (!Channels.isChannelValid(channel)) return; try { if (handler instanceof TransferCompletionHandler) configureTransferAdapter(handler, httpRequest); if (!future.isHeadersAlreadyWrittenOnContinue()) { if (future.getAsyncHandler() instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onSendRequest(nettyRequest); channel.write(httpRequest).addListener(new ProgressListener(config, future.getAsyncHandler(), future, true)); } if (nettyRequest.getBody() != null && !future.isDontWriteBodyBecauseExpectContinue() && httpRequest.getMethod() != HttpMethod.CONNECT) nettyRequest.getBody().write(channel, future, config); // don't bother scheduling timeouts if channel became invalid if (Channels.isChannelValid(channel)) scheduleTimeouts(future); } catch (Throwable t) { LOGGER.error("Can't write request", t); Channels.silentlyCloseChannel(channel); } }