Search in sources :

Example 1 with Connection

use of reactor.netty.Connection in project spring-framework by spring-projects.

the class ReactorServerHttpRequest method initSslInfo.

@Override
@Nullable
protected SslInfo initSslInfo() {
    Channel channel = ((Connection) this.request).channel();
    SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
    if (sslHandler == null && channel.parent() != null) {
        // HTTP/2
        sslHandler = channel.parent().pipeline().get(SslHandler.class);
    }
    if (sslHandler != null) {
        SSLSession session = sslHandler.engine().getSession();
        return new DefaultSslInfo(session);
    }
    return null;
}
Also used : Channel(io.netty.channel.Channel) Connection(reactor.netty.Connection) SSLSession(javax.net.ssl.SSLSession) SslHandler(io.netty.handler.ssl.SslHandler) Nullable(org.springframework.lang.Nullable)

Example 2 with Connection

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

the class Http2Pool method drainLoop.

void drainLoop() {
    recordInteractionTimestamp();
    int maxPending = poolConfig.maxPending();
    for (; ; ) {
        @SuppressWarnings("unchecked") ConcurrentLinkedQueue<Slot> resources = CONNECTIONS.get(this);
        @SuppressWarnings("unchecked") ConcurrentLinkedDeque<Borrower> borrowers = PENDING.get(this);
        if (resources == null || borrowers == TERMINATED) {
            return;
        }
        int borrowersCount = borrowers.size();
        if (borrowersCount != 0) {
            // find a connection that can be used for opening a new stream
            Slot slot = findConnection(resources);
            if (slot != null) {
                Borrower borrower = borrowers.pollFirst();
                if (borrower == null) {
                    resources.offer(slot);
                    continue;
                }
                if (isDisposed()) {
                    borrower.fail(new PoolShutdownException());
                    return;
                }
                if (slot.incrementConcurrencyAndGet() > 1) {
                    borrower.stopPendingCountdown();
                    if (log.isDebugEnabled()) {
                        log.debug(format(slot.connection.channel(), "Channel activated"));
                    }
                    ACQUIRED.incrementAndGet(this);
                    // we are ready here, the connection can be used for opening another stream
                    slot.deactivate();
                    poolConfig.acquisitionScheduler().schedule(() -> borrower.deliver(new Http2PooledRef(slot)));
                } else {
                    borrowers.offerFirst(borrower);
                    continue;
                }
            } else {
                int permits = poolConfig.allocationStrategy().getPermits(1);
                if (permits <= 0) {
                    if (maxPending >= 0) {
                        borrowersCount = borrowers.size();
                        int toCull = borrowersCount - maxPending;
                        for (int i = 0; i < toCull; i++) {
                            Borrower extraneous = borrowers.pollFirst();
                            if (extraneous != null) {
                                pendingAcquireLimitReached(extraneous, maxPending);
                            }
                        }
                    }
                } else {
                    Borrower borrower = borrowers.pollFirst();
                    if (borrower == null) {
                        continue;
                    }
                    if (isDisposed()) {
                        borrower.fail(new PoolShutdownException());
                        return;
                    }
                    borrower.stopPendingCountdown();
                    Mono<Connection> allocator = poolConfig.allocator();
                    Mono<Connection> primary = allocator.doOnEach(sig -> {
                        if (sig.isOnNext()) {
                            Connection newInstance = sig.get();
                            assert newInstance != null;
                            Slot newSlot = new Slot(this, newInstance);
                            if (log.isDebugEnabled()) {
                                log.debug(format(newInstance.channel(), "Channel activated"));
                            }
                            ACQUIRED.incrementAndGet(this);
                            newSlot.incrementConcurrencyAndGet();
                            newSlot.deactivate();
                            borrower.deliver(new Http2PooledRef(newSlot));
                        } else if (sig.isOnError()) {
                            Throwable error = sig.getThrowable();
                            assert error != null;
                            poolConfig.allocationStrategy().returnPermits(1);
                            borrower.fail(error);
                        }
                    }).contextWrite(borrower.currentContext());
                    primary.subscribe(alreadyPropagated -> {
                    }, alreadyPropagatedOrLogged -> drain(), this::drain);
                }
            }
        }
        if (WIP.decrementAndGet(this) == 0) {
            recordInteractionTimestamp();
            break;
        }
    }
}
Also used : AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) PoolShutdownException(reactor.netty.internal.shaded.reactor.pool.PoolShutdownException) PooledRef(reactor.netty.internal.shaded.reactor.pool.PooledRef) ReactorNetty.format(reactor.netty.ReactorNetty.format) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nullable(reactor.util.annotation.Nullable) ConnectionObserver(reactor.netty.ConnectionObserver) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) PooledRefMetadata(reactor.netty.internal.shaded.reactor.pool.PooledRefMetadata) Duration(java.time.Duration) Logger(reactor.util.Logger) Schedulers(reactor.core.scheduler.Schedulers) Disposables(reactor.core.Disposables) Connection(reactor.netty.Connection) Operators(reactor.core.publisher.Operators) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) ChannelOperations(reactor.netty.channel.ChannelOperations) InstrumentedPool(reactor.netty.internal.shaded.reactor.pool.InstrumentedPool) Context(reactor.util.context.Context) Http2FrameCodec(io.netty.handler.codec.http2.Http2FrameCodec) Mono(reactor.core.publisher.Mono) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Subscription(org.reactivestreams.Subscription) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) PoolAcquireTimeoutException(reactor.netty.internal.shaded.reactor.pool.PoolAcquireTimeoutException) Clock(java.time.Clock) Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) PoolAcquirePendingLimitException(reactor.netty.internal.shaded.reactor.pool.PoolAcquirePendingLimitException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) PoolShutdownException(reactor.netty.internal.shaded.reactor.pool.PoolShutdownException) Connection(reactor.netty.Connection)

Example 3 with Connection

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

the class Application method main.

public static void main(String[] args) {
    Connection server = UdpServer.create().bindAddress(// <1>
    Application::newDomainSocketAddress).handle((in, out) -> out.sendObject(in.receiveObject().map(o -> {
        if (o instanceof DomainDatagramPacket) {
            DomainDatagramPacket p = (DomainDatagramPacket) o;
            return new DomainDatagramPacket(p.content().retain(), p.sender());
        } else {
            return Mono.error(new Exception("Unexpected type of the message: " + o));
        }
    }))).bindNow();
    server.onDispose().block();
}
Also used : DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) DomainDatagramPacket(io.netty.channel.unix.DomainDatagramPacket) Mono(reactor.core.publisher.Mono) Connection(reactor.netty.Connection) UdpServer(reactor.netty.udp.UdpServer) File(java.io.File) Connection(reactor.netty.Connection) DomainDatagramPacket(io.netty.channel.unix.DomainDatagramPacket)

Example 4 with Connection

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

the class Application method main.

public static void main(String[] args) {
    Connection server = UdpServer.create().wiretap(// <1>
    true).bindNow(Duration.ofSeconds(30));
    server.onDispose().block();
}
Also used : Connection(reactor.netty.Connection)

Example 5 with Connection

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

the class Application method main.

public static void main(String[] args) {
    Connection connection = UdpClient.create().host("example.com").port(80).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).connectNow(Duration.ofSeconds(30));
    connection.onDispose().block();
}
Also used : Connection(reactor.netty.Connection)

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