Search in sources :

Example 56 with EventLoop

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

the class PerServerConnectionPool method release.

@Override
public boolean release(PooledConnection conn) {
    if (conn == null) {
        return false;
    }
    if (conn.isInPool()) {
        return false;
    }
    // Get the eventloop for this channel.
    EventLoop eventLoop = conn.getChannel().eventLoop();
    Deque<PooledConnection> connections = getPoolForEventLoop(eventLoop);
    CurrentPassport passport = CurrentPassport.fromChannel(conn.getChannel());
    // Discard conn if already at least above waterline in the pool already for this server.
    int poolWaterline = config.perServerWaterline();
    if (poolWaterline > -1 && connections.size() >= poolWaterline) {
        closeAboveHighWaterMarkCounter.increment();
        conn.close();
        conn.setInPool(false);
        return false;
    } else // Attempt to return connection to the pool.
    if (connections.offer(conn)) {
        conn.setInPool(true);
        connsInPool.incrementAndGet();
        passport.add(PassportState.ORIGIN_CH_POOL_RETURNED);
        return true;
    } else {
        // If the pool is full, then close the conn and discard.
        conn.close();
        conn.setInPool(false);
        return false;
    }
}
Also used : CurrentPassport(com.netflix.zuul.passport.CurrentPassport) EventLoop(io.netty.channel.EventLoop)

Example 57 with EventLoop

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

the class NetworkSession method sendWithFuture.

/**
 * Sends a array of {@link Message}s and returns the {@link ChannelFuture}.
 *
 * @param messages The messages
 * @return The channel future
 */
public ChannelFuture sendWithFuture(Message... messages) {
    checkNotNull(messages, "messages");
    checkArgument(messages.length != 0, "messages cannot be empty");
    final ChannelPromise promise = this.channel.newPromise();
    if (!this.channel.isActive()) {
        return promise;
    }
    promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    // there is only one message.
    if (messages.length == 1) {
        this.channel.writeAndFlush(messages[0], promise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        final ChannelPromise voidPromise = this.channel.voidPromise();
        if (eventLoop.inEventLoop()) {
            final int last = messages.length - 1;
            for (int i = 0; i < last; i++) {
                ReferenceCountUtil.retain(messages[i]);
                this.channel.writeAndFlush(messages[i], voidPromise);
            }
            ReferenceCountUtil.retain(messages[last]);
            this.channel.writeAndFlush(messages[last], promise);
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            messages0.forEach(ReferenceCountUtil::retain);
            eventLoop.submit(() -> {
                final Iterator<Message> it0 = messages0.iterator();
                do {
                    final Message message0 = it0.next();
                    // Only use a normal channel promise for the last message
                    this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
                } while (it0.hasNext());
            });
        }
    }
    return promise;
}
Also used : EventLoop(io.netty.channel.EventLoop) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) ChannelPromise(io.netty.channel.ChannelPromise) ReferenceCountUtil(io.netty.util.ReferenceCountUtil)

Example 58 with EventLoop

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

the class NettyStreamingConnectionFactory method connect.

public static NettyStreamingChannel connect(OutboundConnectionSettings template, int messagingVersion, StreamingChannel.Kind kind) throws IOException {
    EventLoop eventLoop = MessagingService.instance().socketFactory.outboundStreamingGroup().next();
    int attempts = 0;
    while (true) {
        Future<Result<StreamingSuccess>> result = initiateStreaming(eventLoop, template.withDefaults(ConnectionCategory.STREAMING), messagingVersion);
        // initiate has its own timeout, so this is "guaranteed" to return relatively promptly
        result.awaitUninterruptibly();
        if (result.isSuccess()) {
            Channel channel = result.getNow().success().channel;
            NettyStreamingChannel streamingChannel = new NettyStreamingChannel(messagingVersion, channel, kind);
            if (kind == StreamingChannel.Kind.CONTROL) {
                ChannelPipeline pipeline = channel.pipeline();
                pipeline.addLast("stream", streamingChannel);
            }
            return streamingChannel;
        }
        if (++attempts == MAX_CONNECT_ATTEMPTS)
            throw new IOException("failed to connect to " + template.to + " for streaming data", result.cause());
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel) StreamingChannel(org.apache.cassandra.streaming.StreamingChannel) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) Result(org.apache.cassandra.net.OutboundConnectionInitiator.Result)

Example 59 with EventLoop

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

the class Dispatcher method flush.

private void flush(FlushItem<?> item) {
    EventLoop loop = item.channel.eventLoop();
    Flusher flusher = flusherLookup.get(loop);
    if (flusher == null) {
        Flusher created = useLegacyFlusher ? Flusher.legacy(loop) : Flusher.immediate(loop);
        Flusher alt = flusherLookup.putIfAbsent(loop, flusher = created);
        if (alt != null)
            flusher = alt;
    }
    flusher.enqueue(item);
    flusher.start();
}
Also used : EventLoop(io.netty.channel.EventLoop)

Example 60 with EventLoop

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

the class AbstractEpollChannel method doClose.

@Override
protected void doClose() throws Exception {
    active = false;
    // Even if we allow half closed sockets we should give up on reading. Otherwise we may allow a read attempt on a
    // socket which has not even been connected yet. This has been observed to block during unit tests.
    inputClosedSeenErrorOnRead = true;
    try {
        ChannelPromise promise = connectPromise;
        if (promise != null) {
            // Use tryFailure() instead of setFailure() to avoid the race against cancel().
            promise.tryFailure(new ClosedChannelException());
            connectPromise = null;
        }
        Future<?> future = connectTimeoutFuture;
        if (future != null) {
            future.cancel(false);
            connectTimeoutFuture = null;
        }
        if (isRegistered()) {
            // Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor
            // if SO_LINGER is used.
            // 
            // See https://github.com/netty/netty/issues/7159
            EventLoop loop = eventLoop();
            if (loop.inEventLoop()) {
                doDeregister();
            } else {
                loop.execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            doDeregister();
                        } catch (Throwable cause) {
                            pipeline().fireExceptionCaught(cause);
                        }
                    }
                });
            }
        }
    } finally {
        socket.close();
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoop(io.netty.channel.EventLoop) ChannelPromise(io.netty.channel.ChannelPromise)

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