Search in sources :

Example 6 with PoolConfig

use of reactor.netty.internal.shaded.reactor.pool.PoolConfig in project reactor-netty by reactor.

the class Http2PoolTest method evictClosedConnection.

@Test
void evictClosedConnection() throws Exception {
    PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.fromSupplier(() -> {
        Channel channel = new EmbeddedChannel(new TestChannelId(), Http2FrameCodecBuilder.forClient().build());
        return Connection.from(channel);
    })).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
    Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
    Connection connection = null;
    try {
        PooledRef<Connection> acquired1 = http2Pool.acquire().block();
        assertThat(acquired1).isNotNull();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        connection = acquired1.poolable();
        ChannelId id1 = connection.channel().id();
        CountDownLatch latch = new CountDownLatch(1);
        ((EmbeddedChannel) connection.channel()).finishAndReleaseAll();
        connection.onDispose(latch::countDown);
        connection.dispose();
        assertThat(latch.await(1, TimeUnit.SECONDS)).as("latch await").isTrue();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        acquired1.invalidate().block();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        assertThat(http2Pool.connections.size()).isEqualTo(0);
        PooledRef<Connection> acquired2 = http2Pool.acquire().block();
        assertThat(acquired2).isNotNull();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        connection = acquired2.poolable();
        ChannelId id2 = connection.channel().id();
        assertThat(id1).isNotEqualTo(id2);
        acquired2.invalidate().block();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        assertThat(http2Pool.connections.size()).isEqualTo(0);
    } finally {
        if (connection != null) {
            ((EmbeddedChannel) connection.channel()).finishAndReleaseAll();
            connection.dispose();
        }
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) Connection(reactor.netty.Connection) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelId(io.netty.channel.ChannelId) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with PoolConfig

use of reactor.netty.internal.shaded.reactor.pool.PoolConfig in project reactor-netty by reactor.

the class Http2PoolTest method acquireRelease.

@Test
void acquireRelease() {
    EmbeddedChannel channel = new EmbeddedChannel(Http2FrameCodecBuilder.forClient().build(), new Http2MultiplexHandler(new ChannelHandlerAdapter() {
    }));
    PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.just(Connection.from(channel))).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
    InstrumentedPool<Connection> http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
    try {
        List<PooledRef<Connection>> acquired = new ArrayList<>();
        http2Pool.acquire().subscribe(acquired::add);
        http2Pool.acquire().subscribe(acquired::add);
        http2Pool.acquire().subscribe(acquired::add);
        assertThat(acquired).hasSize(3);
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(3);
        for (PooledRef<Connection> slot : acquired) {
            slot.release().block(Duration.ofSeconds(1));
        }
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        for (PooledRef<Connection> slot : acquired) {
            // second release() should be ignored and ACQUIRED size should remain the same
            slot.release().block(Duration.ofSeconds(1));
        }
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
    } finally {
        channel.finishAndReleaseAll();
        Connection.from(channel).dispose();
    }
}
Also used : Connection(reactor.netty.Connection) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) PooledRef(reactor.netty.internal.shaded.reactor.pool.PooledRef) Test(org.junit.jupiter.api.Test)

Example 8 with PoolConfig

use of reactor.netty.internal.shaded.reactor.pool.PoolConfig in project reactor-netty by reactor.

the class Http2PoolTest method maxLifeTime.

@Test
void maxLifeTime() throws Exception {
    PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.fromSupplier(() -> {
        Channel channel = new EmbeddedChannel(new TestChannelId(), Http2FrameCodecBuilder.forClient().build());
        return Connection.from(channel);
    })).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
    Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, 10));
    Connection connection1 = null;
    Connection connection2 = null;
    try {
        PooledRef<Connection> acquired1 = http2Pool.acquire().block();
        assertThat(acquired1).isNotNull();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        connection1 = acquired1.poolable();
        ChannelId id1 = connection1.channel().id();
        Thread.sleep(10);
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        acquired1.invalidate().block();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        assertThat(http2Pool.connections.size()).isEqualTo(0);
        PooledRef<Connection> acquired2 = http2Pool.acquire().block();
        assertThat(acquired2).isNotNull();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        assertThat(http2Pool.connections.size()).isEqualTo(1);
        connection2 = acquired2.poolable();
        ChannelId id2 = connection2.channel().id();
        assertThat(id1).isNotEqualTo(id2);
        acquired2.invalidate().block();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        assertThat(http2Pool.connections.size()).isEqualTo(0);
    } finally {
        if (connection1 != null) {
            ((EmbeddedChannel) connection1.channel()).finishAndReleaseAll();
            connection1.dispose();
        }
        if (connection2 != null) {
            ((EmbeddedChannel) connection2.channel()).finishAndReleaseAll();
            connection2.dispose();
        }
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) Connection(reactor.netty.Connection) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelId(io.netty.channel.ChannelId) Test(org.junit.jupiter.api.Test)

Example 9 with PoolConfig

use of reactor.netty.internal.shaded.reactor.pool.PoolConfig in project reactor-netty by reactor.

the class Http2PoolTest method nonHttp2ConnectionEmittedOnce.

@Test
void nonHttp2ConnectionEmittedOnce() {
    EmbeddedChannel channel = new EmbeddedChannel();
    PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.just(Connection.from(channel))).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
    InstrumentedPool<Connection> http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
    try {
        PooledRef<Connection> acquired = http2Pool.acquire().block(Duration.ofSeconds(1));
        assertThat(acquired).isNotNull();
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        http2Pool.acquire(Duration.ofMillis(10)).as(StepVerifier::create).expectError(PoolAcquireTimeoutException.class).verify(Duration.ofSeconds(1));
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
        acquired.invalidate().block(Duration.ofSeconds(1));
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
    } finally {
        channel.finishAndReleaseAll();
        Connection.from(channel).dispose();
    }
}
Also used : Connection(reactor.netty.Connection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) PoolAcquireTimeoutException(reactor.netty.internal.shaded.reactor.pool.PoolAcquireTimeoutException) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 Test (org.junit.jupiter.api.Test)9 Connection (reactor.netty.Connection)9 PoolConfig (reactor.netty.internal.shaded.reactor.pool.PoolConfig)9 Channel (io.netty.channel.Channel)6 ChannelId (io.netty.channel.ChannelId)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 PoolAcquireTimeoutException (reactor.netty.internal.shaded.reactor.pool.PoolAcquireTimeoutException)3 StepVerifier (reactor.test.StepVerifier)3 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)2 Http2MultiplexHandler (io.netty.handler.codec.http2.Http2MultiplexHandler)2 ArrayList (java.util.ArrayList)2 PooledRef (reactor.netty.internal.shaded.reactor.pool.PooledRef)2