Search in sources :

Example 71 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class TcpClientTests method tcpClientHandlesLineFeedData.

@Test
void tcpClientHandlesLineFeedData() throws InterruptedException {
    final int messages = 100;
    final CountDownLatch latch = new CountDownLatch(messages);
    final List<String> strings = new ArrayList<>();
    Connection client = TcpClient.create().host("localhost").port(echoServerPort).doOnConnected(c -> c.addHandlerLast("codec", new LineBasedFrameDecoder(8 * 1024))).handle((in, out) -> out.sendString(Flux.range(1, messages).map(i -> "Hello World!" + i + "\n").subscribeOn(Schedulers.parallel())).then(in.receive().asString().take(100).flatMapIterable(s -> Arrays.asList(s.split("\\n"))).doOnNext(s -> {
        strings.add(s);
        latch.countDown();
    }).then())).wiretap(true).connectNow(Duration.ofSeconds(15));
    assertThat(latch.await(15, TimeUnit.SECONDS)).as("Expected messages not received. Received " + strings.size() + " messages: " + strings).isTrue();
    assertThat(strings).hasSize(messages);
    client.disposeNow();
}
Also used : AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) AbortedException(reactor.netty.channel.AbortedException) Retry(reactor.util.retry.Retry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Future(java.util.concurrent.Future) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Logger(reactor.util.Logger) NameResolverProvider(reactor.netty.transport.NameResolverProvider) LoopResources(reactor.netty.resources.LoopResources) ChannelOperations(reactor.netty.channel.ChannelOperations) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyOutbound(reactor.netty.NettyOutbound) DisposableServer(reactor.netty.DisposableServer) ChannelOption(io.netty.channel.ChannelOption) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Publisher(org.reactivestreams.Publisher) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ArrayList(java.util.ArrayList) Connection(reactor.netty.Connection) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 72 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class TcpClientTests method writeIdleDoesNotFireWhileDataIsBeingSent.

@Test
void writeIdleDoesNotFireWhileDataIsBeingSent() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    long start = System.currentTimeMillis();
    Connection client = TcpClient.create().host("localhost").port(echoServerPort).handle((in, out) -> {
        log.debug("hello");
        out.withConnection(c -> c.onWriteIdle(500, latch::countDown));
        List<Publisher<Void>> allWrites = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            allWrites.add(out.sendString(Flux.just("a").delayElements(Duration.ofMillis(750))));
        }
        return Flux.merge(allWrites);
    }).wiretap(true).connectNow();
    log.debug("Started");
    assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch await").isTrue();
    long duration = System.currentTimeMillis() - start;
    assertThat(duration).isGreaterThanOrEqualTo(500L);
    client.disposeNow();
}
Also used : Connection(reactor.netty.Connection) ArrayList(java.util.ArrayList) Publisher(org.reactivestreams.Publisher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 73 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class TcpClientTests method testIssue585_1.

@Test
void testIssue585_1() throws Exception {
    DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
    CountDownLatch latch = new CountDownLatch(1);
    byte[] bytes = "test".getBytes(Charset.defaultCharset());
    ByteBuf b1 = Unpooled.wrappedBuffer(bytes);
    ByteBuf b2 = Unpooled.wrappedBuffer(bytes);
    ByteBuf b3 = Unpooled.wrappedBuffer(bytes);
    WeakReference<ByteBuf> refCheck1 = new WeakReference<>(b1);
    WeakReference<ByteBuf> refCheck2 = new WeakReference<>(b2);
    WeakReference<ByteBuf> refCheck3 = new WeakReference<>(b3);
    Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
    NettyOutbound out = conn.outbound();
    Flux.concatDelayError(out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b1).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b2).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b3).then()).doOnError(t -> latch.countDown()).subscribe(conn.disposeSubscriber());
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(b1.refCnt()).isEqualTo(0);
    b1 = null;
    checkReference(refCheck1);
    assertThat(b2.refCnt()).isEqualTo(0);
    b2 = null;
    checkReference(refCheck2);
    assertThat(b3.refCnt()).isEqualTo(0);
    b3 = null;
    checkReference(refCheck3);
    server.disposeNow();
    conn.disposeNow();
}
Also used : AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) AbortedException(reactor.netty.channel.AbortedException) Retry(reactor.util.retry.Retry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Future(java.util.concurrent.Future) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Logger(reactor.util.Logger) NameResolverProvider(reactor.netty.transport.NameResolverProvider) LoopResources(reactor.netty.resources.LoopResources) ChannelOperations(reactor.netty.channel.ChannelOperations) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyOutbound(reactor.netty.NettyOutbound) DisposableServer(reactor.netty.DisposableServer) ChannelOption(io.netty.channel.ChannelOption) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Publisher(org.reactivestreams.Publisher) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) NettyOutbound(reactor.netty.NettyOutbound) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 74 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class UdpServerBind method bind.

@Override
public Mono<? extends Connection> bind() {
    UdpServerConfig conf = configuration();
    ConnectionObserver observer = config.defaultConnectionObserver().then(config.connectionObserver());
    Mono<? extends Connection> mono = ConnectionProvider.newConnection().acquire(conf, observer, null, null);
    if (conf.doOnBind() != null) {
        mono = mono.doOnSubscribe(s -> conf.doOnBind().accept(conf));
    }
    return mono;
}
Also used : ConnectionObserver(reactor.netty.ConnectionObserver) ConnectionObserver(reactor.netty.ConnectionObserver) ChannelOption(io.netty.channel.ChannelOption) NetUtil(io.netty.util.NetUtil) Mono(reactor.core.publisher.Mono) Connection(reactor.netty.Connection) ConnectionProvider(reactor.netty.resources.ConnectionProvider) InetSocketAddress(java.net.InetSocketAddress) Collections(java.util.Collections)

Example 75 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class PooledConnectionProvider method acquire.

@Override
public final Mono<? extends Connection> acquire(TransportConfig config, ConnectionObserver connectionObserver, @Nullable Supplier<? extends SocketAddress> remote, @Nullable AddressResolverGroup<?> resolverGroup) {
    Objects.requireNonNull(config, "config");
    Objects.requireNonNull(connectionObserver, "connectionObserver");
    Objects.requireNonNull(remote, "remoteAddress");
    Objects.requireNonNull(resolverGroup, "resolverGroup");
    return Mono.create(sink -> {
        SocketAddress remoteAddress = Objects.requireNonNull(remote.get(), "Remote Address supplier returned null");
        PoolKey holder = new PoolKey(remoteAddress, config.channelHash());
        PoolFactory<T> poolFactory = poolFactory(remoteAddress);
        InstrumentedPool<T> pool = MapUtils.computeIfAbsent(channelPools, holder, poolKey -> {
            if (log.isDebugEnabled()) {
                log.debug("Creating a new [{}] client pool [{}] for [{}]", name, poolFactory, remoteAddress);
            }
            InstrumentedPool<T> newPool = createPool(config, poolFactory, remoteAddress, resolverGroup);
            if (poolFactory.metricsEnabled || config.metricsRecorder() != null) {
                // registrar is null when metrics are enabled on HttpClient level or
                // with the `metrics(boolean metricsEnabled)` method on ConnectionProvider
                String id = poolKey.hashCode() + "";
                if (poolFactory.registrar != null) {
                    poolFactory.registrar.get().registerMetrics(name, id, remoteAddress, new DelegatingConnectionPoolMetrics(newPool.metrics()));
                } else if (Metrics.isInstrumentationAvailable()) {
                    // work directly with the pool otherwise a weak reference is needed to ConnectionPoolMetrics
                    // we don't want to keep another map with weak references
                    registerDefaultMetrics(id, remoteAddress, newPool.metrics());
                }
            }
            return newPool;
        });
        EventLoop eventLoop = config.loopResources().onClient(config.isPreferNative()).next();
        pool.acquire(Duration.ofMillis(poolFactory.pendingAcquireTimeout)).contextWrite(ctx -> ctx.put(CONTEXT_CALLER_EVENTLOOP, eventLoop)).subscribe(createDisposableAcquire(config, connectionObserver, poolFactory.pendingAcquireTimeout, pool, sink));
    });
}
Also used : ReactorNetty(reactor.netty.ReactorNetty) SamplingAllocationStrategy(reactor.pool.introspection.SamplingAllocationStrategy) Disposable(reactor.core.Disposable) SocketAddress(java.net.SocketAddress) ReactorNetty.format(reactor.netty.ReactorNetty.format) InstrumentedPool(reactor.pool.InstrumentedPool) MonoSink(reactor.core.publisher.MonoSink) HashMap(java.util.HashMap) Nullable(reactor.util.annotation.Nullable) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) InetAddress(java.net.InetAddress) ConnectionObserver(reactor.netty.ConnectionObserver) BiPredicate(java.util.function.BiPredicate) CoreSubscriber(reactor.core.CoreSubscriber) TransportConfig(reactor.netty.transport.TransportConfig) AllocationStrategy(reactor.pool.AllocationStrategy) Loggers(reactor.util.Loggers) Metrics(reactor.netty.internal.util.Metrics) Logger(reactor.util.Logger) Duration(java.time.Duration) Map(java.util.Map) Schedulers(reactor.core.scheduler.Schedulers) MapUtils(reactor.netty.internal.util.MapUtils) PoolConfig(reactor.pool.PoolConfig) PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED(reactor.netty.resources.ConnectionProvider.ConnectionPoolSpec.PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED) Connection(reactor.netty.Connection) PooledRef(reactor.pool.PooledRef) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PoolBuilder(reactor.pool.PoolBuilder) Mono(reactor.core.publisher.Mono) Pool(reactor.pool.Pool) GracefulShutdownInstrumentedPool(reactor.pool.decorators.GracefulShutdownInstrumentedPool) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Channel(io.netty.channel.Channel) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) List(java.util.List) Clock(java.time.Clock) PooledRefMetadata(reactor.pool.PooledRefMetadata) InstrumentedPoolDecorators(reactor.pool.decorators.InstrumentedPoolDecorators) EventLoop(io.netty.channel.EventLoop) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

Connection (reactor.netty.Connection)137 Test (org.junit.jupiter.api.Test)69 CountDownLatch (java.util.concurrent.CountDownLatch)52 Duration (java.time.Duration)49 Mono (reactor.core.publisher.Mono)47 InetSocketAddress (java.net.InetSocketAddress)41 TimeUnit (java.util.concurrent.TimeUnit)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 LoopResources (reactor.netty.resources.LoopResources)38 ByteBuf (io.netty.buffer.ByteBuf)35 Flux (reactor.core.publisher.Flux)35 DisposableServer (reactor.netty.DisposableServer)35 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 List (java.util.List)31 IOException (java.io.IOException)30 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)30 ChannelOption (io.netty.channel.ChannelOption)29 Unpooled (io.netty.buffer.Unpooled)28 Charset (java.nio.charset.Charset)28