Search in sources :

Example 61 with EventLoop

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

the class SimpleChannelPool method release.

@Override
public Future<Void> release(final Channel channel, final Promise<Void> promise) {
    try {
        checkNotNull(channel, "channel");
        checkNotNull(promise, "promise");
        EventLoop loop = channel.eventLoop();
        if (loop.inEventLoop()) {
            doReleaseChannel(channel, promise);
        } else {
            loop.execute(new Runnable() {

                @Override
                public void run() {
                    doReleaseChannel(channel, promise);
                }
            });
        }
    } catch (Throwable cause) {
        closeAndFail(channel, cause, promise);
    }
    return promise;
}
Also used : EventLoop(io.netty.channel.EventLoop)

Example 62 with EventLoop

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

the class SimpleChannelPool method acquireHealthyFromPoolOrNew.

/**
 * Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
 * @param promise the promise to provide acquire result.
 * @return future for acquiring a channel.
 */
private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise) {
    try {
        final Channel ch = pollChannel();
        if (ch == null) {
            // No Channel left in the pool bootstrap a new Channel
            Bootstrap bs = bootstrap.clone();
            bs.attr(POOL_KEY, this);
            ChannelFuture f = connectChannel(bs);
            if (f.isDone()) {
                notifyConnect(f, promise);
            } else {
                f.addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        notifyConnect(future, promise);
                    }
                });
            }
        } else {
            EventLoop loop = ch.eventLoop();
            if (loop.inEventLoop()) {
                doHealthCheck(ch, promise);
            } else {
                loop.execute(new Runnable() {

                    @Override
                    public void run() {
                        doHealthCheck(ch, promise);
                    }
                });
            }
        }
    } catch (Throwable cause) {
        promise.tryFailure(cause);
    }
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 63 with EventLoop

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

the class DefaultDnsCacheTest method testAddMultipleAddressesForSameHostname.

@Test
public void testAddMultipleAddressesForSameHostname() throws Exception {
    InetAddress addr1 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 });
    InetAddress addr2 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 });
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultDnsCache cache = new DefaultDnsCache();
        cache.cache("netty.io", null, addr1, 1, loop);
        cache.cache("netty.io", null, addr2, 10000, loop);
        List<? extends DnsCacheEntry> entries = cache.get("netty.io", null);
        assertEquals(2, entries.size());
        assertEntry(entries.get(0), addr1);
        assertEntry(entries.get(1), addr2);
    } 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) InetAddress(java.net.InetAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 64 with EventLoop

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

the class DefaultDnsCacheTest method testDotHandling.

@Test
public void testDotHandling() throws Exception {
    InetAddress addr1 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 });
    InetAddress addr2 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 });
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultDnsCache cache = new DefaultDnsCache(1, 100, 100);
        cache.cache("netty.io", null, addr1, 10000, loop);
        cache.cache("netty.io.", null, addr2, 10000, loop);
        List<? extends DnsCacheEntry> entries = cache.get("netty.io", null);
        assertEquals(2, entries.size());
        assertEntry(entries.get(0), addr1);
        assertEntry(entries.get(1), addr2);
        List<? extends DnsCacheEntry> entries2 = cache.get("netty.io.", null);
        assertEquals(2, entries2.size());
        assertEntry(entries2.get(0), addr1);
        assertEntry(entries2.get(1), addr2);
    } 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) InetAddress(java.net.InetAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 65 with EventLoop

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

the class DnsNameResolverTest method testCachesClearedOnClose.

@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testCachesClearedOnClose() throws Exception {
    final CountDownLatch resolveLatch = new CountDownLatch(1);
    final CountDownLatch authoritativeLatch = new CountDownLatch(1);
    DnsNameResolver resolver = newResolver().resolveCache(new DnsCache() {

        @Override
        public void clear() {
            resolveLatch.countDown();
        }

        @Override
        public boolean clear(String hostname) {
            return false;
        }

        @Override
        public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
            return null;
        }

        @Override
        public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, InetAddress address, long originalTtl, EventLoop loop) {
            return null;
        }

        @Override
        public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, Throwable cause, EventLoop loop) {
            return null;
        }
    }).authoritativeDnsServerCache(new DnsCache() {

        @Override
        public void clear() {
            authoritativeLatch.countDown();
        }

        @Override
        public boolean clear(String hostname) {
            return false;
        }

        @Override
        public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
            return null;
        }

        @Override
        public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, InetAddress address, long originalTtl, EventLoop loop) {
            return null;
        }

        @Override
        public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, Throwable cause, EventLoop loop) {
            return null;
        }
    }).build();
    resolver.close();
    resolveLatch.await();
    authoritativeLatch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) EventLoop(io.netty.channel.EventLoop) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DnsRecord(io.netty.handler.codec.dns.DnsRecord) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

EventLoop (io.netty.channel.EventLoop)77 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 Channel (io.netty.channel.Channel)10 InetSocketAddress (java.net.InetSocketAddress)10 Bootstrap (io.netty.bootstrap.Bootstrap)9 InetAddress (java.net.InetAddress)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelPromise (io.netty.channel.ChannelPromise)6 LocalAddress (io.netty.channel.local.LocalAddress)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Callable (java.util.concurrent.Callable)3