Search in sources :

Example 36 with EventLoop

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

the class DefaultDnsCacheTest method testExpire.

@Test
public void testExpire() throws Throwable {
    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);
        Throwable error = loop.schedule(new Callable<Throwable>() {

            @Override
            public Throwable call() {
                try {
                    assertNull(cache.get("netty.io", null));
                    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) InetAddress(java.net.InetAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 37 with EventLoop

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

the class DnsAddressResolverGroupTest method testUseConfiguredEventLoop.

@Test
public void testUseConfiguredEventLoop() throws InterruptedException {
    NioEventLoopGroup group = new NioEventLoopGroup(1);
    final EventLoop loop = group.next();
    DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup(1);
    DnsNameResolverBuilder builder = new DnsNameResolverBuilder().eventLoop(loop).channelType(NioDatagramChannel.class);
    DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder);
    try {
        final Promise<?> promise = loop.newPromise();
        AddressResolver<?> resolver = resolverGroup.getResolver(defaultEventLoopGroup.next());
        resolver.resolve(new SocketAddress() {

            private static final long serialVersionUID = 3169703458729818468L;
        }).addListener(new FutureListener<Object>() {

            @Override
            public void operationComplete(Future<Object> future) {
                try {
                    assertThat(future.cause(), instanceOf(UnsupportedAddressTypeException.class));
                    assertTrue(loop.inEventLoop());
                    promise.setSuccess(null);
                } catch (Throwable cause) {
                    promise.setFailure(cause);
                }
            }
        }).await();
        promise.sync();
    } finally {
        resolverGroup.close();
        group.shutdownGracefully();
        defaultEventLoopGroup.shutdownGracefully();
    }
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) Future(io.netty.util.concurrent.Future) SocketAddress(java.net.SocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 38 with EventLoop

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

the class Bootstrap method doResolveAndConnect0.

private ChannelFuture doResolveAndConnect0(final Channel channel, SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
    try {
        final EventLoop eventLoop = channel.eventLoop();
        AddressResolver<SocketAddress> resolver;
        try {
            resolver = this.resolver.getResolver(eventLoop);
        } catch (Throwable cause) {
            channel.close();
            return promise.setFailure(cause);
        }
        if (!resolver.isSupported(remoteAddress) || resolver.isResolved(remoteAddress)) {
            // Resolver has no idea about what to do with the specified remote address or it's resolved already.
            doConnect(remoteAddress, localAddress, promise);
            return promise;
        }
        final Future<SocketAddress> resolveFuture = resolver.resolve(remoteAddress);
        if (resolveFuture.isDone()) {
            final Throwable resolveFailureCause = resolveFuture.cause();
            if (resolveFailureCause != null) {
                // Failed to resolve immediately
                channel.close();
                promise.setFailure(resolveFailureCause);
            } else {
                // Succeeded to resolve immediately; cached? (or did a blocking lookup)
                doConnect(resolveFuture.getNow(), localAddress, promise);
            }
            return promise;
        }
        // Wait until the name resolution is finished.
        resolveFuture.addListener(new FutureListener<SocketAddress>() {

            @Override
            public void operationComplete(Future<SocketAddress> future) throws Exception {
                if (future.cause() != null) {
                    channel.close();
                    promise.setFailure(future.cause());
                } else {
                    doConnect(future.getNow(), localAddress, promise);
                }
            }
        });
    } catch (Throwable cause) {
        promise.tryFailure(cause);
    }
    return promise;
}
Also used : EventLoop(io.netty.channel.EventLoop) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 39 with EventLoop

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

the class PerServerConnectionPool method remove.

@Override
public boolean remove(PooledConnection conn) {
    if (conn == null) {
        return false;
    }
    if (!conn.isInPool()) {
        return false;
    }
    // Get the eventloop for this channel.
    EventLoop eventLoop = conn.getChannel().eventLoop();
    // Attempt to return connection to the pool.
    Deque<PooledConnection> connections = getPoolForEventLoop(eventLoop);
    if (connections.remove(conn)) {
        conn.setInPool(false);
        connsInPool.decrementAndGet();
        return true;
    } else {
        return false;
    }
}
Also used : EventLoop(io.netty.channel.EventLoop)

Example 40 with EventLoop

use of io.netty.channel.EventLoop in project grpc-java by grpc.

the class NettyHandlerTestBase method newMockContext.

protected final ChannelHandlerContext newMockContext() {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    EventLoop eventLoop = mock(EventLoop.class);
    when(ctx.executor()).thenReturn(eventLoop);
    when(ctx.channel()).thenReturn(channel);
    return ctx;
}
Also used : EventLoop(io.netty.channel.EventLoop) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

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