Object attribute = Channels.getAttribute(channel); logger.info("Received unexpected message while expecting a chunk: " + msg); ctx.pipeline().remove(publisher); Channels.setDiscard(channel); Channels.silentlyCloseChannel(channel);
public void drainChannelAndExecuteNextRequest(final Channel channel, final NettyResponseFuture<?> future, Request nextRequest) { Channels.setAttribute(channel, new OnLastHttpContentCallback(future) { @Override public void call() { sendNextRequest(nextRequest, future); } }); }
public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture<?> future) { if (Channels.isActiveTokenSet(channel)) { if (future.isDone()) { channelManager.closeChannel(channel); } else if (future.incrementRetryAndCheck() && retry(future)) { future.pendingException = null; } else { abort(channel, future, future.pendingException != null ? future.pendingException : RemotelyClosedException.INSTANCE); } } }
private void close(Channel channel) { // FIXME pity to have to do this here Channels.setDiscard(channel); Channels.silentlyCloseChannel(channel); }
Object attribute = Channels.getAttribute(channel); if (attribute instanceof StreamedResponsePublisher) { ctx.fireExceptionCaught(e); if (!requestSender.applyIoExceptionFiltersAndReplayRequest(future, ChannelClosedException.INSTANCE, channel)) { Channels.silentlyCloseChannel(channel); Channels.silentlyCloseChannel(channel);
public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (requestSender.isClosed()) return; Channel channel = ctx.channel(); channelManager.removeAll(channel); Object attribute = Channels.getAttribute(channel); logger.debug("Channel Closed: {} with attribute {}", channel, attribute); if (attribute instanceof StreamedResponsePublisher) { // setting `attribute` to be the underlying future so that the retry // logic can kick-in attribute = ((StreamedResponsePublisher) attribute).future(); } if (attribute instanceof OnLastHttpContentCallback) { OnLastHttpContentCallback callback = (OnLastHttpContentCallback) attribute; Channels.setAttribute(channel, callback.future()); callback.call(); } else if (attribute instanceof NettyResponseFuture<?>) { NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute; future.touch(); if (hasIOExceptionFilters && requestSender.applyIoExceptionFiltersAndReplayRequest(future, ChannelClosedException.INSTANCE, channel)) return; handleChannelInactive(future); requestSender.handleUnexpectedClosedChannel(channel, future); } }
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(); }
private boolean futureIsAlreadyCancelled(Channel channel) { // FIXME should we only check isCancelled? if (future.isDone()) { Channels.silentlyCloseChannel(channel); return true; } return false; }
private boolean isHandledByReactiveStreams(ChannelHandlerContext ctx) { return Channels.getAttribute(ctx.channel()) instanceof StreamedResponsePublisher; }
private <T> ListenableFuture<T> sendRequestWithCachedChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) { if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel); future.setState(NettyResponseFuture.STATE.POOLED); future.attachChannel(channel, false); LOGGER.debug("Using cached Channel {} for {} '{}'", channel, future.getNettyRequest().getHttpRequest().getMethod(), future.getNettyRequest().getHttpRequest().getUri()); if (Channels.isChannelValid(channel)) { Channels.setAttribute(channel, future); writeRequest(future, channel); } else { // bad luck, the channel was closed in-between // there's a very good chance onClose was already notified but the // future wasn't already registered handleUnexpectedClosedChannel(channel, future); } return future; }
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.writeAndFlush(httpRequest, channel.newProgressivePromise()).addListener(new ProgressListener(config, future.getAsyncHandler(), future, true, 0L)); } if (!future.isDontWriteBodyBecauseExpectContinue() && httpRequest.getMethod() != HttpMethod.CONNECT && nettyRequest.getBody() != null) nettyRequest.getBody().write(channel, future); // 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); } }
public final void tryToOfferChannelToPool(Channel channel, AsyncHandler<?> asyncHandler, boolean keepAlive, Object partitionKey) { if (channel.isActive() && keepAlive) { LOGGER.debug("Adding key: {} for channel {}", partitionKey, channel); Channels.setDiscard(channel); try { asyncHandler.onConnectionOffer(channel); } catch (Exception e) { LOGGER.error("onConnectionOffer crashed", e); } if (!channelPool.offer(channel, partitionKey)) { // rejected by pool closeChannel(channel); } } else { // not offered closeChannel(channel); } }
Channels.setAttribute(channel, future); if (Channels.isChannelActive(channel)) { writeRequest(future, channel); } else {
public void closeChannel(Channel channel) { LOGGER.debug("Closing Channel {} ", channel); Channels.setDiscard(channel); removeAll(channel); Channels.silentlyCloseChannel(channel); }
public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (requestSender.isClosed()) return; Channel channel = ctx.channel(); channelManager.removeAll(channel); try { super.channelInactive(ctx); } catch (Exception ex) { LOGGER.trace("super.channelClosed", ex); } Object attribute = Channels.getAttribute(channel); LOGGER.debug("Channel Closed: {} with attribute {}", channel, attribute); if (attribute instanceof Callback) { Callback callback = (Callback) attribute; Channels.setAttribute(channel, callback.future()); callback.call(); } else if (attribute instanceof NettyResponseFuture<?>) { NettyResponseFuture<?> future = NettyResponseFuture.class.cast(attribute); future.touch(); if (!config.getIOExceptionFilters().isEmpty() && requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel)) return; protocol.onClose(future); requestSender.handleUnexpectedClosedChannel(channel, future); } }
Object attribute = Channels.getAttribute(channel); if (attribute instanceof StreamedResponsePublisher) { ctx.fireExceptionCaught(e); if (!requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel)) Channels.silentlyCloseChannel(channel); return; Channels.silentlyCloseChannel(channel);
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 void attachChannel(Channel channel, boolean reuseChannel) { // future could have been cancelled first if (isDone()) { Channels.silentlyCloseChannel(channel); } this.channel = channel; this.reuseChannel = reuseChannel; }
private boolean isHandledByReactiveStreams(ChannelHandlerContext ctx) { return Channels.getAttribute(ctx.channel()) instanceof StreamedResponsePublisher; } }
private <T> ListenableFuture<T> sendRequestWithCachedChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) { if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel); future.setState(NettyResponseFuture.STATE.POOLED); future.attachChannel(channel, false); LOGGER.debug("Using cached Channel {} for {} '{}'", channel, future.getNettyRequest().getHttpRequest().getMethod(), future.getNettyRequest().getHttpRequest().getUri()); if (Channels.isChannelValid(channel)) { Channels.setAttribute(channel, future); writeRequest(future, channel); } else { // bad luck, the channel was closed in-between // there's a very good chance onClose was already notified but the // future wasn't already registered handleUnexpectedClosedChannel(channel, future); } return future; }