Search in sources :

Example 21 with EventLoop

use of io.netty.channel.EventLoop in project netty by netty.

the class DefaultAuthoritativeDnsServerCacheTest method testUnresolvedReplacedByResolved.

@Test
public void testUnresolvedReplacedByResolved() throws Exception {
    InetSocketAddress unresolved = InetSocketAddress.createUnresolved("ns1", 53);
    InetSocketAddress resolved1 = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53);
    InetSocketAddress resolved2 = new InetSocketAddress(InetAddress.getByAddress("ns1", new byte[] { 10, 0, 0, 1 }), 53);
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultAuthoritativeDnsServerCache cache = new DefaultAuthoritativeDnsServerCache();
        cache.cache("netty.io", unresolved, 100, loop);
        cache.cache("netty.io", resolved1, 10000, loop);
        DnsServerAddressStream entries = cache.get("netty.io");
        assertEquals(2, entries.size());
        assertEquals(unresolved, entries.next());
        assertEquals(resolved1, entries.next());
        cache.cache("netty.io", resolved2, 100, loop);
        DnsServerAddressStream entries2 = cache.get("netty.io");
        assertEquals(2, entries2.size());
        assertEquals(resolved2, entries2.next());
        assertEquals(resolved1, entries2.next());
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 22 with EventLoop

use of io.netty.channel.EventLoop in project netty by netty.

the class DefaultAuthoritativeDnsServerCacheTest method testExpire.

@Test
public void testExpire() throws Throwable {
    InetSocketAddress resolved1 = new InetSocketAddress(InetAddress.getByAddress("ns1", new byte[] { 10, 0, 0, 1 }), 53);
    InetSocketAddress resolved2 = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53);
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultAuthoritativeDnsServerCache cache = new DefaultAuthoritativeDnsServerCache();
        cache.cache("netty.io", resolved1, 1, loop);
        cache.cache("netty.io", resolved2, 10000, loop);
        Throwable error = loop.schedule(new Callable<Throwable>() {

            @Override
            public Throwable call() {
                try {
                    assertNull(cache.get("netty.io"));
                    return null;
                } catch (Throwable cause) {
                    return cause;
                }
            }
        }, 1, TimeUnit.SECONDS).get();
        if (error != null) {
            throw error;
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 23 with EventLoop

use of io.netty.channel.EventLoop in project netty by netty.

the class DefaultDnsCnameCacheTest method testExpire.

@Test
public void testExpire() throws Throwable {
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
        cache.cache("netty.io", "mapping.netty.io", 1, loop);
        Throwable error = loop.schedule(new Callable<Throwable>() {

            @Override
            public Throwable call() {
                try {
                    assertNull(cache.get("netty.io"));
                    return null;
                } catch (Throwable cause) {
                    return cause;
                }
            }
        }, 1, TimeUnit.SECONDS).get();
        if (error != null) {
            throw error;
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 24 with EventLoop

use of io.netty.channel.EventLoop in project netty by netty.

the class DefaultDnsCnameCacheTest method testExpireWithTTL0.

private static void testExpireWithTTL0(int days) {
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
        cache.cache("netty.io", "mapping.netty.io", TimeUnit.DAYS.toSeconds(days), loop);
        assertEquals("mapping.netty.io", cache.get("netty.io"));
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup)

Example 25 with EventLoop

use of io.netty.channel.EventLoop in project netty by netty.

the class LocalChannel method doClose.

@Override
protected void doClose() throws Exception {
    final LocalChannel peer = this.peer;
    State oldState = state;
    try {
        if (oldState != State.CLOSED) {
            // Update all internal state before the closeFuture is notified.
            if (localAddress != null) {
                if (parent() == null) {
                    LocalChannelRegistry.unregister(localAddress);
                }
                localAddress = null;
            }
            // State change must happen before finishPeerRead to ensure writes are released either in doWrite or
            // channelRead.
            state = State.CLOSED;
            // Preserve order of event and force a read operation now before the close operation is processed.
            if (writeInProgress && peer != null) {
                finishPeerRead(peer);
            }
            ChannelPromise promise = connectPromise;
            if (promise != null) {
                // Use tryFailure() instead of setFailure() to avoid the race against cancel().
                promise.tryFailure(new ClosedChannelException());
                connectPromise = null;
            }
        }
        if (peer != null) {
            this.peer = null;
            // Always call peer.eventLoop().execute() even if peer.eventLoop().inEventLoop() is true.
            // This ensures that if both channels are on the same event loop, the peer's channelInActive
            // event is triggered *after* this peer's channelInActive event
            EventLoop peerEventLoop = peer.eventLoop();
            final boolean peerIsActive = peer.isActive();
            try {
                peerEventLoop.execute(new Runnable() {

                    @Override
                    public void run() {
                        peer.tryClose(peerIsActive);
                    }
                });
            } catch (Throwable cause) {
                logger.warn("Releasing Inbound Queues for channels {}-{} because exception occurred!", this, peer, cause);
                if (peerEventLoop.inEventLoop()) {
                    peer.releaseInboundBuffers();
                } else {
                    // inboundBuffers is a SPSC so we may leak if the event loop is shutdown prematurely or
                    // rejects the close Runnable but give a best effort.
                    peer.close();
                }
                PlatformDependent.throwException(cause);
            }
        }
    } finally {
        // Release all buffers if the Channel was already registered in the past and if it was not closed before.
        if (oldState != null && oldState != State.CLOSED) {
            // We need to release all the buffers that may be put into our inbound queue since we closed the Channel
            // to ensure we not leak any memory. This is fine as it basically gives the same guarantees as TCP which
            // means even if the promise was notified before its not really guaranteed that the "remote peer" will
            // see the buffer at all.
            releaseInboundBuffers();
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

EventLoop (io.netty.channel.EventLoop)79 EventLoopGroup (io.netty.channel.EventLoopGroup)27 Test (org.junit.jupiter.api.Test)27 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)18 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)17 Bootstrap (io.netty.bootstrap.Bootstrap)11 Channel (io.netty.channel.Channel)11 InetSocketAddress (java.net.InetSocketAddress)11 InetAddress (java.net.InetAddress)9 ChannelFuture (io.netty.channel.ChannelFuture)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelPromise (io.netty.channel.ChannelPromise)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 LocalAddress (io.netty.channel.local.LocalAddress)5 ClosedChannelException (java.nio.channels.ClosedChannelException)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 List (java.util.List)4 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)3 IOException (java.io.IOException)3