Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About ByteBuf.internalNioBuffer #6184

Closed
ytcoode opened this issue Jan 9, 2017 · 2 comments
Closed

About ByteBuf.internalNioBuffer #6184

ytcoode opened this issue Jan 9, 2017 · 2 comments

Comments

@ytcoode
Copy link

ytcoode commented Jan 9, 2017

Hi guys,

Why ChannelOutboundBuffer.nioBuffers() use internalNioBuffer instead of nioBuffer ?

If i have a DIRECT byteBuf to write to two channels, and i forgot to slice or duplicate when write, because of the using of internalNioBuffer, the two channels (or two channel's eventloops) use the SAME ByteBuf's ByteBuffer, this will cause many problems.

E.g.

When the first eventLoop thread try to write the ByteBuf to socket but not start, and then, the second eventLoop thread start write the same ByteBuf and succeed, and then the first eventLoop thread resume, but when it write, the ByteBuf's ByteBuffer has no writable bytes, because the second eventLoop thread has already written it.

Now, for the first eventLoop thread, the ByteBuf's readerIndex is unmodified, so it think the ByteBuf has writable bytes, but the ByteBuf's ByteBuffer's position already reach it's limit, no writables bytes, so the eventLoop thread will try to write the ByteBuf, but write nothing, and then try again, and then write nothing, enter an endless loop...

@normanmaurer
Copy link
Member

@wangyuntao its to reduce object allocation.

@ytcoode
Copy link
Author

ytcoode commented Jan 9, 2017

@normanmaurer , thank you for your reply!

But if using internalNioBuffer, there's a chance to lead to EventLoop thread enter endless loop, is't too dangerous ?

After I‘v read the code, I have another question.

If I write a HeapByteBuf, netty will copy it to a DirectByteBuf, and if I write a DirectByteBuf, netty will use it,
then netty will write these DirectByteBufs to socket.

In this process, internalNioBuffer will be used in some setBytes methods, e.g. this, but why these setBytes methods use internalNioBuffer method instead of using buffer directly ? Perhaps for DirectByteBuf, internalNioBuffer method should return buffer instead of the cached one ? Perhaps for DirectByteBuf, the cached one is unnecessary ?

Is there something I'm missed or wrong ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants