Skip to content

Commit b39ffed

Browse files
brucelwlnormanmaurer
authored andcommitted
Fix incorrect calculation of next buffer size in AdaptiveRecvByteBufAllocator (netty#9555)
Motivation: Due a bug we did not always correctly calculate the next buffer size in AdaptiveRecvByteBufAllocator. Modification: Fix calculation and add unit test Result: Correct calculation is always used.
1 parent 85a663f commit b39ffed

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public int guess() {
122122
}
123123

124124
private void record(int actualReadBytes) {
125-
if (actualReadBytes <= SIZE_TABLE[max(0, index - INDEX_DECREMENT - 1)]) {
125+
if (actualReadBytes <= SIZE_TABLE[max(0, index - INDEX_DECREMENT)]) {
126126
if (decreaseNow) {
127127
index = max(index - INDEX_DECREMENT, minIndex);
128128
nextReceiveBufferSize = SIZE_TABLE[index];

transport/src/test/java/io/netty/channel/AdaptiveRecvByteBufAllocatorTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ public void rampUpBeforeReadCompleteWhenLargeDataPending() {
5454
allocReadExpected(handle, alloc, 8388608);
5555
}
5656

57+
@Test
58+
public void memoryAllocationIntervalsTest() {
59+
computingNext(512, 512);
60+
computingNext(8192, 1110);
61+
computingNext(8192, 1200);
62+
computingNext(4096, 1300);
63+
computingNext(4096, 1500);
64+
computingNext(2048, 1700);
65+
computingNext(2048, 1550);
66+
computingNext(2048, 2000);
67+
computingNext(2048, 1900);
68+
}
69+
70+
private void computingNext(long expectedSize, int actualReadBytes) {
71+
assertEquals(expectedSize, handle.guess());
72+
handle.reset(config);
73+
handle.lastBytesRead(actualReadBytes);
74+
handle.readComplete();
75+
}
76+
5777
@Test
5878
public void lastPartialReadDoesNotRampDown() {
5979
allocReadExpected(handle, alloc, 512);

0 commit comments

Comments
 (0)