synchronized ChannelBuffer + ChannelBuffer pool

Trustin Lee (이희승) trustin at gmail.com
Mon Oct 12 22:18:57 EDT 2009


Hi Ron,

On Thu, Oct 1, 2009 at 3:51 AM, rzo <rzo at gmx.de> wrote:
> Hello,
>
> first: thanks for this great framework.
> During my tests with netty I have run into issues because I was assuming
> that buffers in netty are synchronized.
>
> I think it would make sense to provide a
> Channels.synchronizedChannel(...) similar to java's
> Collections.synchronized...()
>
> What do you think ?

Channel is already thread safe, and therefore you don't need to do that.

> I have overcome the synchronization issue by creating a new
> ChannelBuffer before sending it downstream.
> How expensive is ChannelBuffer creation/garbage collection ?
> Would a buffer pool make sense ?

Most ChannelBuffers live short and therefore the GC cost is minimal.
Pooling was more expensive due to synchronization from my experience.

> is ChannelBuffer.discardBytes() better than creating a new buffer for
> sending downstream ?

You have to make sure no other party modifies the content of the
buffer when writing a buffer to a channel.  Otherwise, the buffer
content might be accessed by many parties concurrently, resulting in
data corruption.  ChannelBuffer.discardReadBytes() does not create a
new buffer but simply compacts the buffer, so you cannot use it for
reusing the write buffer.

HTH

— Trustin Lee, http://gleamynode.net/



More information about the netty-users mailing list