public NettyAsyncHttpProvider(AsyncHttpClientConfig config) { this.config = config; nettyConfig = config.getAsyncHttpProviderConfig() instanceof NettyAsyncHttpProviderConfig ? // (NettyAsyncHttpProviderConfig) config.getAsyncHttpProviderConfig() : new NettyAsyncHttpProviderConfig(); allowStopNettyTimer = nettyConfig.getNettyTimer() == null; nettyTimer = allowStopNettyTimer ? newNettyTimer() : nettyConfig.getNettyTimer(); channelManager = new ChannelManager(config, nettyConfig, nettyTimer); requestSender = new NettyRequestSender(config, nettyConfig, channelManager, nettyTimer, closed); channelManager.configureBootstraps(requestSender, closed); }
private void abortChannelPreemption() { if (channelPreempted) channelManager.abortChannelPreemption(partitionKey); }
public void close() { if (closed.compareAndSet(false, true)) { try { channelManager.close(); // FIXME shouldn't close if not allowed config.executorService().shutdown(); if (allowStopNettyTimer) nettyTimer.stop(); } catch (Throwable t) { LOGGER.warn("Unexpected error on close", t); } } }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
public void abort(Channel channel, NettyResponseFuture<?> future, Throwable t) { if (channel != null) channelManager.closeChannel(channel); if (!future.isDone()) { LOGGER.debug("Aborting Future {}\n", future); LOGGER.debug(t.getMessage(), t); future.abort(t); } }
ClientBootstrap bootstrap = channelManager.getBootstrap(request.getUri().getScheme(), useProxy, useSSl); channelManager.preemptChannel(partitionKey); channelPreempted = true; channelManager.abortChannelPreemption(partitionKey);
ClientBootstrap bootstrap = channelManager.getBootstrap(request.getURI().getScheme(), useProxy, useSSl); poolKey = channelManager.getPoolKey(future); channelManager.abortChannelPreemption(poolKey);
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme) throws IOException, GeneralSecurityException { boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline); if (isSecure(scheme)) { if (!sslHandlerConfigured) pipeline.addFirst(SSL_HANDLER, new SslInitializer(this)); } else if (sslHandlerConfigured) pipeline.remove(SSL_HANDLER); }
public void drainChannel(final Channel channel, final NettyResponseFuture<?> future) { Channels.setAttribute(channel, newDrainCallback(future, channel, future.isKeepAlive(), getPoolKey(future))); } }
@Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { if (requestSender.isClosed()) return; Channel channel = ctx.getChannel(); channelManager.removeAll(channel); try { super.channelClosed(ctx, e); } 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<?>) attribute; future.touch(); if (!config.getIOExceptionFilters().isEmpty() && requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel)) return; protocol.onClose(channel); if (future == null || future.isDone()) channelManager.closeChannel(channel); else if (!requestSender.retry(future, ctx.getChannel())) requestSender.abort(future, REMOTELY_CLOSED_EXCEPTION); } }
private boolean tryAcquirePerHost(Object partitionKey) { return !maxConnectionsPerHostEnabled || getFreeConnectionsForHost(partitionKey).tryAcquire(); }
future.setReuseChannel(true); } else { channelManager.drainChannelAndOffer(channel, future, initialConnectionKeepAlive, initialPartitionKey); channelManager.closeChannel(channel);
public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { InetSocketAddress remoteInetSocketAddress = (InetSocketAddress) e.getValue(); String peerHost = remoteInetSocketAddress.getHostString(); int peerPort = remoteInetSocketAddress.getPort(); SslHandler sslHandler = channelManager.createSslHandler(peerHost, peerPort); ctx.getPipeline().replace(ChannelManager.SSL_HANDLER, ChannelManager.SSL_HANDLER, sslHandler); ctx.sendDownstream(e); } }
public void drainChannelAndOffer(final Channel channel, final NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey) { Channels.setAttribute(channel, newDrainCallback(future, channel, keepAlive, partitionKey)); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)){ pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) pipeline.replace(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); }
public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture<?> future) { if (future.isDone()) channelManager.closeChannel(channel); else if (!retry(future)) abort(channel, future, REMOTELY_CLOSED_EXCEPTION); }
@Override public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException { final RandomAccessFile raf = new RandomAccessFile(file, "r"); try { ChannelFuture writeFuture; if (ChannelManager.isSslHandlerConfigured(channel.getPipeline()) || nettyConfig.isDisableZeroCopy()) { writeFuture = channel.write(new ChunkedFile(raf, offset, raf.length(), nettyConfig.getChunkedFileChunkSize())); } else { final FileRegion region = new OptimizedFileRegion(raf, offset, raf.length()); writeFuture = channel.write(region); } writeFuture.addListener(new ProgressListener(config, future.getAsyncHandler(), future, false) { public void operationComplete(ChannelFuture cf) { closeSilently(raf); super.operationComplete(cf); } }); } catch (IOException ex) { closeSilently(raf); throw ex; } } }
final String initialPoolKey = channelManager.getPoolKey(future); Callback callback = channelManager.newDrainCallback(future, channel, initialConnectionKeepAlive, initialPoolKey);
public void abortChannelPreemption(Object partitionKey) { if (maxTotalConnectionsEnabled) freeChannels.release(); if (maxConnectionsPerHostEnabled) getFreeConnectionsForHost(partitionKey).release(); }
public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { InetSocketAddress remoteInetSocketAddress = (InetSocketAddress) e.getValue(); String peerHost = remoteInetSocketAddress.getHostName(); int peerPort = remoteInetSocketAddress.getPort(); SslHandler sslHandler = channelManager.createSslHandler(peerHost, peerPort); ctx.getPipeline().replace(ChannelManager.SSL_HANDLER, ChannelManager.SSL_HANDLER, sslHandler); ctx.sendDownstream(e); } }