use of io.netty.channel.pool.ChannelPool in project reactor-netty by reactor.
the class DefaultPoolResources method selectOrCreate.
@Override
public ChannelPool selectOrCreate(SocketAddress remote, Supplier<? extends Bootstrap> bootstrap, Consumer<? super Channel> onChannelCreate, EventLoopGroup group) {
SocketAddressHolder holder = new SocketAddressHolder(remote);
for (; ; ) {
Pool pool = channelPools.get(holder);
if (pool != null) {
return pool;
}
if (log.isDebugEnabled()) {
log.debug("New {} client pool for {}", name, remote);
}
pool = new Pool(bootstrap.get().remoteAddress(remote), provider, onChannelCreate, group);
if (channelPools.putIfAbsent(holder, pool) == null) {
return pool;
}
pool.close();
}
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method streamChannelAcquireReleaseTest.
/**
* Channel acquire and release test.
*/
@Test
public void streamChannelAcquireReleaseTest() throws Exception {
Channel channel = newHttp2Channel();
try {
ChannelPool connectionPool = Mockito.mock(ChannelPool.class);
loopGroup.register(channel).awaitUninterruptibly();
Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
channelPromise.setSuccess(channel);
Mockito.when(connectionPool.acquire()).thenReturn(channelPromise);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, new HashSet<>(), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
Channel streamChannel1 = h2Pool.acquire().awaitUninterruptibly().getNow();
assertTrue(streamChannel1 instanceof Http2StreamChannel);
Mockito.verify(connectionPool, Mockito.times(1)).acquire();
Channel streamChannel2 = h2Pool.acquire().awaitUninterruptibly().getNow();
assertTrue(streamChannel2 instanceof Http2StreamChannel);
Mockito.verify(connectionPool, Mockito.times(1)).acquire();
// Verify number of numOfAvailableStreams
MultiplexedChannelRecord multiplexedChannelRecord = streamChannel2.parent().attr(Http2MultiplexedChannelPool.MULTIPLEXED_CHANNEL).get();
assertEquals(maxConcurrentStreamsPerConnection - 2, multiplexedChannelRecord.getNumOfAvailableStreams().get());
h2Pool.release(streamChannel1).getNow();
h2Pool.release(streamChannel2).getNow();
assertEquals(maxConcurrentStreamsPerConnection, multiplexedChannelRecord.getNumOfAvailableStreams().get());
} finally {
channel.close();
}
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method interruptDuringClosePreservesFlag.
/**
* Interrupt flag is preserved if pool close is interrupted.
*/
@Test(timeout = 5_000)
public void interruptDuringClosePreservesFlag() throws InterruptedException {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
channelPromise.setSuccess(channel);
ChannelPool connectionPool = mock(ChannelPool.class);
Promise<Void> releasePromise = Mockito.spy(new DefaultPromise<>(loopGroup.next()));
when(connectionPool.release(eq(channel))).thenReturn(releasePromise);
MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
CompletableFuture<Boolean> interrupteFlagPreserved = new CompletableFuture<>();
Thread t = new Thread(() -> {
try {
h2Pool.close();
} catch (Exception e) {
if (e.getCause() instanceof InterruptedException && Thread.currentThread().isInterrupted()) {
interrupteFlagPreserved.complete(true);
}
}
});
t.start();
t.interrupt();
t.join();
assertTrue(interrupteFlagPreserved.join());
} finally {
channel.close().awaitUninterruptibly();
}
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method failedConnectionAcquireNotifiesPromise.
/**
* Channel acquire should fail if parent channel pool acquire fails.
*/
@Test
public void failedConnectionAcquireNotifiesPromise() throws InterruptedException {
IOException exception = new IOException();
ChannelPool connectionPool = mock(ChannelPool.class);
when(connectionPool.acquire()).thenReturn(new FailedFuture<>(loopGroup.next(), exception));
ChannelPool pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), new HashSet<>(), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
Future<Channel> acquirePromise = pool.acquire().await();
assertFalse(acquirePromise.isSuccess());
assertEquals(acquirePromise.cause(), exception);
}
use of io.netty.channel.pool.ChannelPool in project reactor-netty by reactor.
the class DefaultPoolResourcesTest method disposeLaterDefers.
@Test
public void disposeLaterDefers() {
DefaultPoolResources.Pool pool = new DefaultPoolResources.Pool(new Bootstrap(), (b, handler, checker) -> channelPool, null, new DefaultEventLoopGroup());
DefaultPoolResources poolResources = new DefaultPoolResources("test", (b, handler, checker) -> channelPool);
// "register" our fake Pool
poolResources.channelPools.put(new DefaultPoolResources.SocketAddressHolder(InetSocketAddress.createUnresolved("localhost", 80)), pool);
Mono<Void> disposer = poolResources.disposeLater();
assertThat(closed.get()).as("pool closed by disposeLater()").isEqualTo(0);
disposer.subscribe();
assertThat(closed.get()).as("pool closed by disposer subscribe()").isEqualTo(1);
}
Aggregations