Concurrency in Netty

I implement ChannelInboundHandlerAdapterand ask a question about concurrency. Do I need to make threads safe? I mean, I have to store a certain state for each client for my sessions.

public class Impl extends ChannelInboundHandlerAdapter{
    private List<Integer> someState;
    //

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        int size = someState.size();  //Should I worry about memory consitency here?
        //...
    }
}

The fact is that if you request a method from a request by channelReaddifferent threads, I will have to put some memory barriers.

It's necessary? Or will Nettyhe take care of this?

+4
source share
1 answer

For Netty 4.x

Well defined flow model

3.x does not have a well-defined flow model, although there has been an attempt to correct its inconsistency in 3.5. 4.0 defines a strict stream model that helps the user write ChannelHandler without worrying about thread safety.

  • Netty ChannelHandler , ChannelHandler @Sharable.. - , .
    • .
    • 4.0 ChannelHandler , @Sharable.
  • - ChannelHandler, Netty.
    • volatile, .
  • EventExecutor, ChannelPipeline.
    • , ChannelHandler EventExecutor.
    • , EventLoop, .
  • EventExecutor EventLoop, , .
    • .
    • EventExecutor EventLoop, , .
    • EventExecutors, . , , .
  • ChannelFutureListeners, ChannelFuture, EventLoop, .
  • ChannelHandlerInvoker . DefaultChannelHandlerInvoker EventLoop Runnable EventExecutor. . , EventLoop .

(, )

+2

Source: https://habr.com/ru/post/1686702/


All Articles