@Override public void call() { sendNextRequest(nextRequest, future); } });
@Override public void operationComplete(ChannelFuture future) { operationComplete(future.channel(), future.cause()); } }
/** * We know for sure if we have to force to connect or not, so we can build the * HttpRequest right away This reduces the probability of having a pooled * channel closed by the server by the time we build the request */ private <T> ListenableFuture<T> sendRequestWithCertainForceConnect(Request request, AsyncHandler<T> asyncHandler, NettyResponseFuture<T> future, ProxyServer proxyServer, boolean performConnectRequest) { NettyResponseFuture<T> newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, performConnectRequest); Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler); return Channels.isChannelActive(channel) ? sendRequestWithOpenChannel(newFuture, asyncHandler, channel) : sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler); }
public boolean retry(NettyResponseFuture<?> future) { if (isClosed()) { return false; } if (future.isReplayPossible()) { future.setChannelState(ChannelState.RECONNECTED); LOGGER.debug("Trying to recover request {}\n", future.getNettyRequest().getHttpRequest()); try { future.getAsyncHandler().onRetry(); } catch (Exception e) { LOGGER.error("onRetry crashed", e); abort(future.channel(), future, e); return false; } try { sendNextRequest(future.getCurrentRequest(), future); return true; } catch (Exception e) { abort(future.channel(), future, e); return false; } } else { LOGGER.debug("Unable to recover future {}\n", future); return false; } }
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 writeRequest(Channel channel) { if (futureIsAlreadyCancelled(channel)) { return; } if (LOGGER.isDebugEnabled()) { HttpRequest httpRequest = future.getNettyRequest().getHttpRequest(); LOGGER.debug("Using new Channel '{}' for '{}' to '{}'", channel, httpRequest.method(), httpRequest.uri()); } Channels.setAttribute(channel, future); channelManager.registerOpenChannel(channel); future.attachChannel(channel, false); requestSender.writeRequest(future, channel); }
@Override protected void onFailure(Throwable cause) { abort(null, future, getCause(cause)); } });
public <T> void sendNextRequest(final Request request, final NettyResponseFuture<T> future) { sendRequest(request, future.getAsyncHandler(), future); }
@Override public void call() { Channels.setAttribute(channel, future); requestSender.writeRequest(future, channel); } });
private Timeout newTimeout(TimerTask task, long delay) { return requestSender.isClosed() ? null : nettyTimer.newTimeout(task, delay, TimeUnit.MILLISECONDS); } }
public NettyRequestSender(AsyncHttpClientConfig config, ChannelManager channelManager, Timer nettyTimer, AsyncHttpClientState clientState) { this.config = config; this.channelManager = channelManager; this.connectionSemaphore = config.getConnectionSemaphoreFactory() == null ? new DefaultConnectionSemaphoreFactory().newConnectionSemaphore(config) : config.getConnectionSemaphoreFactory().newConnectionSemaphore(config); this.nettyTimer = nettyTimer; this.clientState = clientState; requestFactory = new NettyRequestFactory(config); }
public void operationComplete(ChannelProgressiveFuture cf) { closeSilently(body); super.operationComplete(cf); } });
private Channel getOpenChannel(NettyResponseFuture<?> future, Request request, ProxyServer proxyServer, AsyncHandler<?> asyncHandler) { if (future != null && future.isReuseChannel() && Channels.isChannelActive(future.channel())) { return future.channel(); } else { return pollPooledChannel(request, proxyServer, asyncHandler); } }
/** * Using CONNECT depends on wither we can fetch a valid channel or not Loop * until we get a valid channel from the pool and it's still valid once the * request is built @ */ private <T> ListenableFuture<T> sendRequestThroughProxy(Request request, AsyncHandler<T> asyncHandler, NettyResponseFuture<T> future, ProxyServer proxyServer) { NettyResponseFuture<T> newFuture = null; for (int i = 0; i < 3; i++) { Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler); if (channel == null) { // pool is empty break; } if (newFuture == null) { newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, false); } if (Channels.isChannelActive(channel)) { // if the channel is still active, we can use it, // otherwise, channel was closed by the time we computed the request, try again return sendRequestWithOpenChannel(newFuture, asyncHandler, channel); } } // couldn't poll an active channel newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, true); return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler); }
private void readFailed(Channel channel, NettyResponseFuture<?> future, Throwable t) { try { requestSender.abort(channel, future, t); } catch (Exception abortException) { logger.debug("Abort failed", abortException); } finally { finishUpdate(future, channel, true); } }
@Override public void call() { whenHandshaked.addListener(f -> { if (f.isSuccess()) { sendNextRequest(nextRequest, future); } else { future.abort(f.cause()); } } ); } });
private <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> asyncHandler) { try { return requestSender.sendRequest(request, asyncHandler, null); } catch (Exception e) { asyncHandler.onThrowable(e); return new ListenableFuture.CompletedFailure<>(e); } }
public void operationComplete(ChannelProgressiveFuture cf) { closeSilently(is); super.operationComplete(cf); } });
void expire(String message, long time) { LOGGER.debug("{} for {} after {} ms", message, nettyResponseFuture, time); requestSender.abort(nettyResponseFuture.channel(), nettyResponseFuture, new TimeoutException(message)); }
@Override public void operationComplete(ChannelProgressiveFuture cf) { operationComplete(cf.channel(), cf.cause()); }