-
Notifications
You must be signed in to change notification settings - Fork 992
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
Allow configuration of discardReadBytes through ClientOptions.bufferUsageRatio #916
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95cfaa8
#906 - use buffer usage ratio to optimize memory usage
gavincook d3797ee
#906 - fix overflow issue under buffer usage check
gavincook a5e7a44
#906 - add test for buffer usage ratio
gavincook 66b4efb
#906 - add test for discard read bytes in command handler
gavincook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -588,6 +588,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Interrup | |
|
||
try { | ||
if (!decode(ctx, buffer, command)) { | ||
discardReadBytesIfNecessary(buffer); | ||
return; | ||
} | ||
} catch (Exception e) { | ||
|
@@ -614,9 +615,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Interrup | |
afterDecode(ctx, command); | ||
} | ||
|
||
if (buffer.refCnt() != 0) { | ||
buffer.discardReadBytes(); | ||
} | ||
discardReadBytesIfNecessary(buffer); | ||
} | ||
|
||
/** | ||
|
@@ -840,6 +839,19 @@ private static long nanoTime() { | |
return System.nanoTime(); | ||
} | ||
|
||
/** | ||
* try discard read bytes when buffer usage reach {@code bufferUsageRatio / bufferUsageRatio + 1} | ||
* @param buffer | ||
*/ | ||
private void discardReadBytesIfNecessary(ByteBuf buffer) { | ||
int bufferUsageRatio = clientOptions.getBufferUsageRatio(); | ||
if ((float)buffer.writerIndex() / buffer.capacity() >= (float)bufferUsageRatio / (bufferUsageRatio + 1)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should rather check for |
||
if (buffer.refCnt() != 0) { | ||
buffer.discardReadBytes(); | ||
} | ||
} | ||
} | ||
|
||
public enum LifecycleState { | ||
NOT_CONNECTED, REGISTERED, CONNECTED, ACTIVATING, ACTIVE, DISCONNECTED, DEACTIVATING, DEACTIVATED, CLOSED, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,4 +430,30 @@ void shouldIgnoreNonReadableBuffers() throws Exception { | |
|
||
verify(byteBufMock, never()).release(); | ||
} | ||
|
||
@Test | ||
void shouldDiscardReadBytes() throws Exception { | ||
|
||
ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE); | ||
channelPromise.setSuccess(); | ||
sut.channelRegistered(context); | ||
sut.channelActive(context); | ||
//set the command handler buffer capacity to 1024, make it easy to test | ||
ReflectionTestUtils.setField(sut, "buffer", context.alloc().buffer(1024)); | ||
ByteBuf buffer = (ByteBuf) ReflectionTestUtils.getField(sut, "buffer"); | ||
|
||
//mock a multi reply, which will reach the buffer usage ratio | ||
ByteBuf msg = context.alloc().buffer(1024); | ||
while ((float)msg.writerIndex() / msg.capacity() <= (float)ClientOptions.DEFAULT_BUFFER_USAGE_RATIO / ( | ||
ClientOptions.DEFAULT_BUFFER_USAGE_RATIO + 1)) { | ||
sut.write(context, command, channelPromise); | ||
msg.writeBytes("*1\r\n+OK\r\n".getBytes()); | ||
} | ||
|
||
sut.channelRead(context, msg); | ||
|
||
assertThat(buffer.readerIndex() == 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a valid assertion. |
||
assertThat(buffer.writerIndex() == 0); | ||
sut.channelUnregistered(context); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd propose to make cleaning more customizable. This method could return a discard consumer (
BiConsumer<ByteBuf, DecodeProgress>
) instead of exposing details how discard of buffer bytes is configured.See below for the full explanation.