Search in sources :

Example 6 with ChannelPool

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();
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool)

Example 7 with ChannelPool

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();
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) DefaultPromise(io.netty.util.concurrent.DefaultPromise) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 8 with ChannelPool

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();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPool(io.netty.channel.pool.ChannelPool) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) IOException(java.io.IOException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPromise(io.netty.util.concurrent.DefaultPromise) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 9 with ChannelPool

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);
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) MetricRegistry(com.codahale.metrics.MetricRegistry) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) IOException(java.io.IOException) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 10 with ChannelPool

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);
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.Test)

Aggregations

ChannelPool (io.netty.channel.pool.ChannelPool)19 Channel (io.netty.channel.Channel)13 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)13 SocketChannel (io.netty.channel.socket.SocketChannel)12 Test (org.junit.Test)12 Bootstrap (io.netty.bootstrap.Bootstrap)10 MetricRegistry (com.codahale.metrics.MetricRegistry)7 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)7 InetSocketAddress (java.net.InetSocketAddress)7 EventLoopGroup (io.netty.channel.EventLoopGroup)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Consumer (java.util.function.Consumer)6 WrapperException (com.nike.backstopper.exception.WrapperException)4 Pair (com.nike.internal.util.Pair)4 DownstreamIdleChannelTimeoutHandler (com.nike.riposte.client.asynchttp.netty.downstreampipeline.DownstreamIdleChannelTimeoutHandler)4 DownstreamChannelClosedUnexpectedlyException (com.nike.riposte.server.error.exception.DownstreamChannelClosedUnexpectedlyException)4 DownstreamIdleChannelTimeoutException (com.nike.riposte.server.error.exception.DownstreamIdleChannelTimeoutException)4 HostnameResolutionException (com.nike.riposte.server.error.exception.HostnameResolutionException)4 NativeIoExceptionWrapper (com.nike.riposte.server.error.exception.NativeIoExceptionWrapper)4 DOWNSTREAM_CALL_CONNECTION_SETUP_TIME_NANOS_REQUEST_ATTR_KEY (com.nike.riposte.server.handler.ProxyRouterEndpointExecutionHandler.DOWNSTREAM_CALL_CONNECTION_SETUP_TIME_NANOS_REQUEST_ATTR_KEY)4