use of io.netty.channel.Channel in project async-http-client by AsyncHttpClient.
the class EventPipelineTest method asyncPipelineTest.
@Test
public void asyncPipelineTest() throws Exception {
Consumer<Channel> httpAdditionalPipelineInitializer = channel -> channel.pipeline().addBefore("inflater", "copyEncodingHeader", new CopyEncodingHandler());
try (AsyncHttpClient p = asyncHttpClient(config().setHttpAdditionalChannelInitializer(httpAdditionalPipelineInitializer))) {
final CountDownLatch l = new CountDownLatch(1);
p.executeRequest(get(getTargetUrl()), new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) {
try {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getHeader("X-Original-Content-Encoding"), "<original encoding>");
} finally {
l.countDown();
}
return response;
}
}).get();
if (!l.await(TIMEOUT, TimeUnit.SECONDS)) {
fail("Timeout out");
}
}
}
use of io.netty.channel.Channel in project async-http-client by AsyncHttpClient.
the class NettyRequestSender method pollPooledChannel.
private Channel pollPooledChannel(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) {
try {
asyncHandler.onConnectionPoolAttempt();
} catch (Exception e) {
LOGGER.error("onConnectionPoolAttempt crashed", e);
}
Uri uri = request.getUri();
String virtualHost = request.getVirtualHost();
final Channel channel = channelManager.poll(uri, virtualHost, proxy, request.getChannelPoolPartitioning());
if (channel != null) {
LOGGER.debug("Using pooled Channel '{}' for '{}' to '{}'", channel, request.getMethod(), uri);
}
return channel;
}
use of io.netty.channel.Channel in project async-http-client by AsyncHttpClient.
the class NettyRequestSender method sendRequestThroughProxy.
/**
* 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)) {
// 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);
}
use of io.netty.channel.Channel in project async-http-client by AsyncHttpClient.
the class NettyRequestSender method sendRequestWithCertainForceConnect.
/**
* 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);
}
use of io.netty.channel.Channel in project async-http-client by AsyncHttpClient.
the class NettyChannelConnector method connect0.
private void connect0(Bootstrap bootstrap, final NettyConnectListener<?> connectListener, InetSocketAddress remoteAddress) {
bootstrap.connect(remoteAddress, localAddress).addListener(new SimpleChannelFutureListener() {
@Override
public void onSuccess(Channel channel) {
try {
asyncHandler.onTcpConnectSuccess(remoteAddress, channel);
} catch (Exception e) {
LOGGER.error("onTcpConnectSuccess crashed", e);
connectListener.onFailure(channel, e);
return;
}
connectListener.onSuccess(channel, remoteAddress);
}
@Override
public void onFailure(Channel channel, Throwable t) {
try {
asyncHandler.onTcpConnectFailure(remoteAddress, t);
} catch (Exception e) {
LOGGER.error("onTcpConnectFailure crashed", e);
connectListener.onFailure(channel, e);
return;
}
boolean retry = pickNextRemoteAddress();
if (retry) {
NettyChannelConnector.this.connect(bootstrap, connectListener);
} else {
connectListener.onFailure(channel, t);
}
}
});
}
Aggregations