vjm
vjm

Reputation: 9

Netty downstream handler buffer management

Had a question about buffer management for Netty downstream buffer sending flow.

I have a Netty SimpleChannelInBoundHandler MainHandler that does 2 things:-

  1. Receives buffer and asynchronously creates a channel that proxies that buffer to another destination D
  2. Does its own processing on the received buffer and returns response to the client without waiting on 1.

As part 1, I believe I need to explicitly retain the buffer for sending it downstream to destination channel D. Do I also need to take care of releasing this buffer after writeAndFlush() for this channel? Or would Netty pipeline take care of releasing it?

Based on Netty docs https://netty.io/wiki/new-and-noteworthy-in-4.0.html, it says When an outbound (a.k.a. downstream) message reaches at the beginning of the pipeline, Netty will release it after writing it out. which makes me think that Netty would take care of releasing the explicitly retained buffer after downstream/destination channel pipeline completes. Is this understanding correct? `

Explicitly releasing the buffer throws ReferenceCountUtilException in the downstream pipeline

Upvotes: 0

Views: 32

Answers (1)

TavenZ
TavenZ

Reputation: 37

  1. Every time WriteAndFlush is called, the reference count refCnt of bytebuf will be set to 0, thereby releasing it. However, when there are multiple client connections, it is not ensured that byteBuff is not released before all messages are sent, and it is necessary to manually add byteBuff .retain() to increase the reference count by 1.

  2. You can add .release() in the try-finally

    int count = byteBuf.refCnt();
    if(count0!=0){
        byteBuf.release();
    }
    

Upvotes: 0

Related Questions