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?

有帮助吗?

解决方案

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.

其他提示

The getPipeline is called for each incoming message. So for each HttpRequest you will be creating a new HttpChunkSeparator.

If however you had done something like this, it would be totally thread UNSAFE.

private MyServerHandler handler;

// THIS IS WRONG AS getPipeline() will keep giving the same instance to multiple threads.
private HttpChunkAggregator httpChunkAggregator;

public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = pipeline();                   
    pipeline.addLast("decoder",new HttpRequestDecoder()); 

    // This is WRONG. DO NO DO THIS. INSTEAD DO new HttpChunkAggregator().      
    pipeline.addLast("chunkAggregator",httpChunkAggregator);      

    pipeline.addLast("encoder",new HttpResponseEncoder());              
    pipeline.addLast("chunkSeparator",new HttpChunkSeparator(4194304));         
    pipeline.addLast("handler", handler); //Singleton       
    return pipeline;
}

Arun

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top