2012-08-02 2 views
1

어쩌면 이것은 분명히 묻는 질문 일 수도 있지만, 나는 너무 새로운 것입니다.Netty HttpChunckAggregator stateful -> 경합 조건?

HttpChunckAggregator 클래스를 살펴보면 상태가 안정하다는 것을 알 수 있습니다. 나는 청크 메시지 및 멀티 스레딩의 경우 경쟁 조건을 얻을 수,

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; 
} 

와 NIO의 Netty 서버 : 그 날 의심 ... 다음 파이프 라인에 특정 채널을 제공 할?

모든 새로운 채널이 새로운 청크 애그리 게이터를 생성하지만 모든 청크 메시지가 동일한 채널에서 수신되는 것을 볼 수 있습니까?

답변

1

다른 채널에서 공유하지 않기 때문에 안전합니다. netty에서는 하나의 스레드 만 업스트림 이벤트를 실행하므로 다운 스트림 이벤트에서 액세스/수정되지 않는 한 동기화없이 필드에 상태를 저장하는 것이 안전합니다.

1

수신 메시지마다 getPipeline이 호출됩니다. 따라서 각 HttpRequest에 대해 새로운 HttpChunkSeparator를 생성하게 될 것입니다.

그러나 이와 같은 작업을 수행했다면 완전히 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; 
} 

아룬