Search code examples
nettyhttp-chunked

Netty HttpChunckAggregator stateful --> race conditions?


Maybe this is a obvious ask, but I'm too new with netty.

Taking a look to HttpChunckAggregator class I see that it's stateful. That make me doubt... given a specific Channel with the following pipeline:

private MyServerHandler handler;

public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = pipeline();                   
    pipeline.addLast("decoder",new HttpRequestDecoder());       
    pipeline.addLast("chunkAggregator",new HttpChunkAggregator(4194304));       
    pipeline.addLast("encoder",new HttpResponseEncoder());              
    pipeline.addLast("chunkSeparator",new HttpChunkSeparator(4194304));         
    pipeline.addLast("handler", handler); //Singleton       
    return pipeline;
}

and an NIO Netty server, could I get race conditions in case of chunked message and multi-threading?

I see that every new channel creates a new chunk Aggregator but... all the chunk messages will be received in the same channel?


Solution

  • Its safe as its not shared by different Channels. In netty only one thread is executing upstream events, so its safe to store states in fields without any synchronization as long as these are not accessed/modified from a downstream event.