use of com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey 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();
}
use of com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey 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);
}
use of com.linkedin.r2.transport.http.client.common.ChannelPoolManagerKey in project rest.li by linkedin.
the class HttpClientFactory method createChannelPoolManagerKey.
/**
* Creates a {@link ChannelPoolManagerFactory} given the properties
*/
private ChannelPoolManagerKey createChannelPoolManagerKey(Map<String, ? extends Object> properties, SSLContext sslContext, SSLParameters sslParameters) {
String poolStatsNamePrefix = chooseNewOverDefault((String) properties.get(HTTP_POOL_STATS_NAME_PREFIX), DEFAULT_POOL_STATS_NAME_PREFIX);
Integer maxPoolSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_SIZE), DEFAULT_POOL_SIZE);
long idleTimeout = chooseNewOverDefault(getLongValue(properties, HTTP_IDLE_TIMEOUT), DEFAULT_IDLE_TIMEOUT);
long sslIdleTimeout = chooseNewOverDefault(getLongValue(properties, HTTP_SSL_IDLE_TIMEOUT), DEFAULT_SSL_IDLE_TIMEOUT);
long maxResponseSize = chooseNewOverDefault(getLongValue(properties, HTTP_MAX_RESPONSE_SIZE), DEFAULT_MAX_RESPONSE_SIZE);
Integer poolWaiterSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_WAITER_SIZE), DEFAULT_POOL_WAITER_SIZE);
Integer poolMinSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_MIN_SIZE), DEFAULT_POOL_MIN_SIZE);
Integer maxHeaderSize = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_HEADER_SIZE), DEFAULT_MAX_HEADER_SIZE);
Integer maxChunkSize = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_CHUNK_SIZE), DEFAULT_MAX_CHUNK_SIZE);
Boolean tcpNoDelay = chooseNewOverDefault(getBooleanValue(properties, HTTP_TCP_NO_DELAY), DEFAULT_TCP_NO_DELAY);
Integer maxConcurrentConnectionInitializations = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_CONCURRENT_CONNECTIONS), DEFAULT_MAX_CONCURRENT_CONNECTIONS);
AsyncPoolImpl.Strategy strategy = chooseNewOverDefault(getStrategy(properties), DEFAULT_POOL_STRATEGY);
Integer gracefulShutdownTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_GRACEFUL_SHUTDOWN_TIMEOUT), DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT);
return new ChannelPoolManagerKeyBuilder().setMaxPoolSize(maxPoolSize).setGracefulShutdownTimeout(gracefulShutdownTimeout).setIdleTimeout(idleTimeout).setSslIdleTimeout(sslIdleTimeout).setMaxResponseSize(maxResponseSize).setSSLContext(sslContext).setPoolWaiterSize(poolWaiterSize).setSSLParameters(sslParameters).setStrategy(strategy).setMinPoolSize(poolMinSize).setMaxHeaderSize(maxHeaderSize).setMaxChunkSize(maxChunkSize).setMaxConcurrentConnectionInitializations(maxConcurrentConnectionInitializations).setTcpNoDelay(tcpNoDelay).setPoolStatsNamePrefix(poolStatsNamePrefix).build();
}
Aggregations