Search in sources :

Example 36 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class AbstractHttp2StreamChannel method doWrite.

@Override
protected final void doWrite(ChannelOutboundBuffer in) throws Exception {
    if (closed) {
        throw CLOSED_CHANNEL_EXCEPTION;
    }
    EventExecutor preferredExecutor = preferredEventExecutor();
    // https://github.com/netty/netty/issues/4941
    if (preferredExecutor.inEventLoop()) {
        for (; ; ) {
            Object msg = in.current();
            if (msg == null) {
                break;
            }
            try {
                doWrite(ReferenceCountUtil.retain(msg));
            } catch (Throwable t) {
                // It would be nice to fail the future, but we can't do that if not on the event
                // loop. So we instead opt for a solution that is consistent.
                pipeline().fireExceptionCaught(t);
            }
            in.remove();
        }
        doWriteComplete();
    } else {
        // Use a copy because the original msgs will be recycled by AbstractChannel.
        final Object[] msgsCopy = new Object[in.size()];
        for (int i = 0; i < msgsCopy.length; i++) {
            msgsCopy[i] = ReferenceCountUtil.retain(in.current());
            in.remove();
        }
        preferredExecutor.execute(new Runnable() {

            @Override
            public void run() {
                for (Object msg : msgsCopy) {
                    try {
                        doWrite(msg);
                    } catch (Throwable t) {
                        pipeline().fireExceptionCaught(t);
                    }
                }
                doWriteComplete();
            }
        });
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Example 37 with EventExecutor

use of io.netty.util.concurrent.EventExecutor in project netty by netty.

the class Http2MultiplexCodec method flushFromStreamChannel.

void flushFromStreamChannel() {
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        flush(ctx);
    } else {
        Runnable task = flushTask;
        if (task == null) {
            task = flushTask = new Runnable() {

                @Override
                public void run() {
                    flush(ctx);
                }
            };
        }
        executor.execute(task);
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor)

Aggregations

EventExecutor (io.netty.util.concurrent.EventExecutor)37 OrderedEventExecutor (io.netty.util.concurrent.OrderedEventExecutor)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 ChannelPromise (io.netty.channel.ChannelPromise)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelPromiseNotifier (io.netty.channel.ChannelPromiseNotifier)4 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)4 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)3 Test (org.junit.Test)3 LocalChannel (io.netty.channel.local.LocalChannel)2 AbstractEventExecutor (io.netty.util.concurrent.AbstractEventExecutor)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2 UnorderedThreadPoolEventExecutor (io.netty.util.concurrent.UnorderedThreadPoolEventExecutor)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelHandler (io.netty.channel.ChannelHandler)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 ChannelGroup (io.netty.channel.group.ChannelGroup)1 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)1 ServerSocketChannel (io.netty.channel.socket.ServerSocketChannel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1