Search in sources :

Example 81 with Channel

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");
        }
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Response(org.asynchttpclient.Response) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) Dsl(org.asynchttpclient.Dsl) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest) Response(org.asynchttpclient.Response) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 82 with Channel

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;
}
Also used : Channel(io.netty.channel.Channel) Uri(org.asynchttpclient.uri.Uri) FilterException(org.asynchttpclient.filter.FilterException) IOException(java.io.IOException) PoolAlreadyClosedException(org.asynchttpclient.exception.PoolAlreadyClosedException) RemotelyClosedException(org.asynchttpclient.exception.RemotelyClosedException)

Example 83 with 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);
}
Also used : CONNECT(org.asynchttpclient.util.HttpConstants.Methods.CONNECT) GET(org.asynchttpclient.util.HttpConstants.Methods.GET) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) Channel(io.netty.channel.Channel)

Example 84 with Channel

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);
}
Also used : CONNECT(org.asynchttpclient.util.HttpConstants.Methods.CONNECT) GET(org.asynchttpclient.util.HttpConstants.Methods.GET) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) Channel(io.netty.channel.Channel)

Example 85 with Channel

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);
            }
        }
    });
}
Also used : Channel(io.netty.channel.Channel) SimpleChannelFutureListener(org.asynchttpclient.netty.SimpleChannelFutureListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

Channel (io.netty.channel.Channel)884 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)226 ChannelFuture (io.netty.channel.ChannelFuture)204 Test (org.junit.Test)203 Bootstrap (io.netty.bootstrap.Bootstrap)199 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 InetSocketAddress (java.net.InetSocketAddress)165 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)132 IOException (java.io.IOException)126 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)106 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)96 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89