Search in sources :

Example 6 with ChannelPoolManager

use of com.linkedin.r2.transport.http.client.common.ChannelPoolManager in project rest.li by linkedin.

the class ChannelPoolManagerFactoryImpl method buildHttp2Stream.

@Override
public ChannelPoolManager buildHttp2Stream(ChannelPoolManagerKey channelPoolManagerKey) {
    DefaultChannelGroup channelGroup = new DefaultChannelGroup("R2 client channels", _eventLoopGroup.next());
    ChannelPoolFactory channelPoolFactory;
    if (_usePipelineV2) {
        channelPoolFactory = new Http2ChannelPoolFactory(_scheduler, _eventLoopGroup, channelGroup, channelPoolManagerKey.getStrategy(), channelPoolManagerKey.getSslContext(), channelPoolManagerKey.getSslParameters(), channelPoolManagerKey.getMaxPoolSize(), channelPoolManagerKey.getMinPoolSize(), channelPoolManagerKey.getPoolWaiterSize(), MAX_INITIAL_LINE_LENGTH, channelPoolManagerKey.getMaxHeaderSize(), channelPoolManagerKey.getMaxChunkSize(), channelPoolManagerKey.getIdleTimeout(), channelPoolManagerKey.getMaxResponseSize(), channelPoolManagerKey.isTcpNoDelay(), _enableSSLSessionResumption, _connectTimeout, _sslHandShakeTimeout);
    } else {
        channelPoolFactory = new Http2NettyStreamChannelPoolFactory(channelPoolManagerKey.getIdleTimeout(), channelPoolManagerKey.getPoolWaiterSize(), channelPoolManagerKey.getMinPoolSize(), channelPoolManagerKey.isTcpNoDelay(), _scheduler, channelPoolManagerKey.getSslContext(), channelPoolManagerKey.getSslParameters(), channelPoolManagerKey.getGracefulShutdownTimeout(), channelPoolManagerKey.getMaxHeaderSize(), channelPoolManagerKey.getMaxChunkSize(), channelPoolManagerKey.getMaxResponseSize(), _enableSSLSessionResumption, _eventLoopGroup, channelGroup, _connectTimeout, _sslHandShakeTimeout);
    }
    return new ChannelPoolManagerImpl(channelPoolFactory, channelPoolManagerKey.getName() + "-HTTP/2-Stream", channelGroup, _scheduler);
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) HttpNettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamChannelPoolFactory) HttpChannelPoolFactory(com.linkedin.r2.netty.client.http.HttpChannelPoolFactory) Http2NettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamChannelPoolFactory) Http2ChannelPoolFactory(com.linkedin.r2.netty.client.http2.Http2ChannelPoolFactory) HttpNettyChannelPoolFactory(com.linkedin.r2.transport.http.client.rest.HttpNettyChannelPoolFactory) Http2NettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamChannelPoolFactory) Http2ChannelPoolFactory(com.linkedin.r2.netty.client.http2.Http2ChannelPoolFactory)

Example 7 with ChannelPoolManager

use of com.linkedin.r2.transport.http.client.common.ChannelPoolManager in project rest.li by linkedin.

the class ChannelPoolManagerFactoryImpl method buildStream.

@Override
public ChannelPoolManager buildStream(ChannelPoolManagerKey channelPoolManagerKey) {
    DefaultChannelGroup channelGroup = new DefaultChannelGroup("R2 client channels", _eventLoopGroup.next());
    ChannelPoolFactory channelPoolFactory;
    if (_usePipelineV2) {
        channelPoolFactory = new HttpChannelPoolFactory(_scheduler, _eventLoopGroup, channelGroup, channelPoolManagerKey.getStrategy(), channelPoolManagerKey.getSslContext(), channelPoolManagerKey.getSslParameters(), channelPoolManagerKey.getMaxPoolSize(), channelPoolManagerKey.getMinPoolSize(), channelPoolManagerKey.getPoolWaiterSize(), MAX_INITIAL_LINE_LENGTH, channelPoolManagerKey.getMaxHeaderSize(), channelPoolManagerKey.getMaxChunkSize(), channelPoolManagerKey.getMaxConcurrentConnectionInitializations(), channelPoolManagerKey.getIdleTimeout(), channelPoolManagerKey.getMaxResponseSize(), channelPoolManagerKey.isTcpNoDelay(), _enableSSLSessionResumption, _channelPoolWaiterTimeout, _connectTimeout, _sslHandShakeTimeout);
    } else {
        channelPoolFactory = new HttpNettyStreamChannelPoolFactory(channelPoolManagerKey.getMaxPoolSize(), channelPoolManagerKey.getIdleTimeout(), channelPoolManagerKey.getPoolWaiterSize(), channelPoolManagerKey.getStrategy(), channelPoolManagerKey.getMinPoolSize(), channelPoolManagerKey.isTcpNoDelay(), _scheduler, channelPoolManagerKey.getMaxConcurrentConnectionInitializations(), channelPoolManagerKey.getSslContext(), channelPoolManagerKey.getSslParameters(), channelPoolManagerKey.getMaxHeaderSize(), channelPoolManagerKey.getMaxChunkSize(), channelPoolManagerKey.getMaxResponseSize(), _enableSSLSessionResumption, _eventLoopGroup, channelGroup, _channelPoolWaiterTimeout, _connectTimeout, _sslHandShakeTimeout);
    }
    return new ChannelPoolManagerImpl(channelPoolFactory, channelPoolManagerKey.getName() + "-Stream", channelGroup, _scheduler);
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) HttpNettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamChannelPoolFactory) HttpChannelPoolFactory(com.linkedin.r2.netty.client.http.HttpChannelPoolFactory) Http2NettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamChannelPoolFactory) Http2ChannelPoolFactory(com.linkedin.r2.netty.client.http2.Http2ChannelPoolFactory) HttpNettyChannelPoolFactory(com.linkedin.r2.transport.http.client.rest.HttpNettyChannelPoolFactory) HttpChannelPoolFactory(com.linkedin.r2.netty.client.http.HttpChannelPoolFactory) HttpNettyStreamChannelPoolFactory(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamChannelPoolFactory)

Example 8 with ChannelPoolManager

use of com.linkedin.r2.transport.http.client.common.ChannelPoolManager in project rest.li by linkedin.

the class TestHttpsEarlyHandshake method testHttpsEarlyHandshakeHttp1.

@Test
public void testHttpsEarlyHandshakeHttp1() throws Exception {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    ChannelPoolManagerFactoryImpl channelPoolManagerFactory = new ChannelPoolManagerFactoryImpl(eventLoopGroup, scheduler, SSL_SESSION_RESUMPTION_ENABLED, _clientProvider.getUsePipelineV2(), HttpClientFactory.DEFAULT_CHANNELPOOL_WAITER_TIMEOUT, HttpClientFactory.DEFAULT_CONNECT_TIMEOUT, HttpClientFactory.DEFAULT_SSL_HANDSHAKE_TIMEOUT);
    SSLContext context = SslContextUtil.getContext();
    ChannelPoolManagerKey key = new ChannelPoolManagerKeyBuilder().setMinPoolSize(1).setSSLContext(context).setSSLParameters(context.getDefaultSSLParameters()).build();
    ChannelPoolManager channelPoolManager = channelPoolManagerFactory.buildRest(key);
    InetAddress inetAddress = InetAddress.getByName("localhost");
    final SocketAddress address = new InetSocketAddress(inetAddress, _port);
    // get the channel, when it is returned it might not be active yet
    FutureCallback<Channel> futureCallback = new FutureCallback<>();
    AsyncPool<Channel> poolForAddress = channelPoolManager.getPoolForAddress(address);
    poolForAddress.get(futureCallback);
    final Channel channel = futureCallback.get(5, TimeUnit.SECONDS);
    // wait until it gets active
    FutureCallback<Future<? super Void>> futureActiveCallback = new FutureCallback<>();
    channel.newSucceededFuture().addListener(futureActiveCallback::onSuccess);
    futureActiveCallback.get(5, TimeUnit.SECONDS);
    // retrieve the ssl handler from the pipeline and wait till the handshake happens
    SslHandler sslHandler = (SslHandler) channel.pipeline().get(SslHandlerUtil.PIPELINE_SSL_HANDLER);
    FutureCallback<Future<? super Channel>> futureHandshakeCallback = new FutureCallback<>();
    sslHandler.handshakeFuture().addListener(f -> {
        if (f.isSuccess()) {
            futureHandshakeCallback.onSuccess(f);
        } else {
            futureHandshakeCallback.onError(f.cause());
        }
    });
    futureHandshakeCallback.get(5, TimeUnit.SECONDS).get(5, TimeUnit.SECONDS);
    poolForAddress.dispose(channel);
    // shutdown the pool
    FutureCallback<None> futureShutdownCallback = new FutureCallback<>();
    channelPoolManager.shutdown(futureShutdownCallback, () -> {
    }, () -> {
    }, 5000);
    futureShutdownCallback.get(5, TimeUnit.SECONDS);
    // shutdown the client executors
    scheduler.shutdown();
    eventLoopGroup.shutdownGracefully();
}
Also used : ChannelPoolManagerKeyBuilder(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKeyBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelPoolManagerFactoryImpl(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerFactoryImpl) InetSocketAddress(java.net.InetSocketAddress) ChannelPoolManager(com.linkedin.r2.transport.http.client.common.ChannelPoolManager) Channel(io.netty.channel.Channel) SSLContext(javax.net.ssl.SSLContext) SslHandler(io.netty.handler.ssl.SslHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Future(io.netty.util.concurrent.Future) ChannelPoolManagerKey(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) None(com.linkedin.common.util.None) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) AbstractEchoServiceTest(test.r2.integ.clientserver.providers.AbstractEchoServiceTest) Test(org.testng.annotations.Test)

Example 9 with ChannelPoolManager

use of com.linkedin.r2.transport.http.client.common.ChannelPoolManager in project rest.li by linkedin.

the class TestEarlyUpgrade method testEarlyUpgrade.

/**
 * The aim is having the pool upgrading the http1 connection to http2 even before a request comes in
 */
@Test
public void testEarlyUpgrade() throws Exception {
    ChannelPoolManagerFactoryImpl channelPoolManagerFactory = new ChannelPoolManagerFactoryImpl(_eventLoopGroup, _scheduler, SSL_SESSION_RESUMPTION_ENABLED, _newPipelineEnabled, HttpClientFactory.DEFAULT_CHANNELPOOL_WAITER_TIMEOUT, HttpClientFactory.DEFAULT_CONNECT_TIMEOUT, HttpClientFactory.DEFAULT_SSL_HANDSHAKE_TIMEOUT);
    ChannelPoolManagerKey key = new ChannelPoolManagerKeyBuilder().setMinPoolSize(1).build();
    ChannelPoolManager channelPoolManager = channelPoolManagerFactory.buildHttp2Stream(key);
    HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = new HttpServerBuilder.HttpServerStatsProvider();
    Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
    try {
        server.start();
        InetAddress inetAddress = InetAddress.getByName("localhost");
        final SocketAddress address = new InetSocketAddress(inetAddress, HttpServerBuilder.HTTP_PORT);
        // since min pool size is 1, it automatically creates a channel
        channelPoolManager.getPoolForAddress(address);
        // We need the assertWithTimeout because, even if we get the channel,
        // it doesn't mean it connected to the server yet
        AssertionMethods.assertWithTimeout(2000, // it is expected 1 connection to be opened and 1 option request
        () -> Assert.assertEquals(httpServerStatsProvider.clientConnections().size(), 1));
        Assert.assertEquals(httpServerStatsProvider.requestCount(), 1);
    } finally {
        server.stop();
    }
    FutureCallback<None> futureCallback = new FutureCallback<>();
    channelPoolManager.shutdown(futureCallback, () -> {
    }, () -> {
    }, 5);
    futureCallback.get(5, TimeUnit.SECONDS);
}
Also used : ChannelPoolManagerKeyBuilder(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKeyBuilder) ChannelPoolManagerFactoryImpl(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerFactoryImpl) Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) ChannelPoolManager(com.linkedin.r2.transport.http.client.common.ChannelPoolManager) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) ChannelPoolManagerKey(com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)6 ChannelPoolManager (com.linkedin.r2.transport.http.client.common.ChannelPoolManager)4 ChannelPoolManagerKey (com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2 None (com.linkedin.common.util.None)2 HttpChannelPoolFactory (com.linkedin.r2.netty.client.http.HttpChannelPoolFactory)2 Http2ChannelPoolFactory (com.linkedin.r2.netty.client.http2.Http2ChannelPoolFactory)2 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)2 ChannelPoolManagerFactoryImpl (com.linkedin.r2.transport.http.client.common.ChannelPoolManagerFactoryImpl)2 ChannelPoolManagerKeyBuilder (com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKeyBuilder)2 HttpNettyChannelPoolFactory (com.linkedin.r2.transport.http.client.rest.HttpNettyChannelPoolFactory)2 HttpNettyStreamChannelPoolFactory (com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamChannelPoolFactory)2 Http2NettyStreamChannelPoolFactory (com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamChannelPoolFactory)2 Channel (io.netty.channel.Channel)2 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)2 InetAddress (java.net.InetAddress)2 HashMap (java.util.HashMap)2 EventProviderRegistry (com.linkedin.r2.event.EventProviderRegistry)1