Fix a bug where adaptive recvbuf size prediction doesn't work correctly when maxMessagesPerRead is > 1

This commit is contained in:
Trustin Lee 2013-12-21 19:56:36 +09:00
parent 4e05c52c2e
commit 3ca3efac52

View File

@ -111,8 +111,10 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
int messages = 0;
boolean close = false;
try {
int byteBufCapacity = allocHandle.guess();
int totalReadAmount = 0;
do {
byteBuf = allocHandle.allocate(allocator);
byteBuf = allocator.ioBuffer(byteBufCapacity);
int writable = byteBuf.writableBytes();
int localReadAmount = doReadBytes(byteBuf);
if (localReadAmount <= 0) {
@ -121,16 +123,26 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
close = localReadAmount < 0;
break;
}
pipeline.fireChannelRead(byteBuf);
byteBuf = null;
allocHandle.record(localReadAmount);
if (totalReadAmount >= Integer.MAX_VALUE - localReadAmount) {
// Avoid overflow.
totalReadAmount = Integer.MAX_VALUE;
break;
}
totalReadAmount += localReadAmount;
if (localReadAmount < writable) {
// we read less then what the buffer can hold so it seems like we drained it completely
// Read less than what the buffer can hold,
// which might mean we drained the recv buffer completely.
break;
}
} while (++ messages < maxMessagesPerRead);
pipeline.fireChannelReadComplete();
allocHandle.record(totalReadAmount);
if (close) {
closeOnRead(pipeline);