GeezerVison is showing github.com — LengthFieldBasedFrameDecoder Class Can be optimized · Issue #12171 · netty/netty
LengthFieldBasedFrameDecoder Class Can be optimized #12171DescriptionExpected behavior
LengthFieldBasedFrameDecoder . decode(ChannelHandlerContext ctx, ByteBuf in) method,Can be optimized - Can we keep frameLengthInt in instance variable? so , When there is not enough data in the buffer, the length of the data frame does not need to be recalculated at the next decoding。code:
// LengthFieldBasedFrameDecoder class
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
... // check maxFrame
int frameLengthInt = (int) frameLength;
if (in.readableBytes() < frameLengthInt) { // When there is not enough data in the buffer, The above code will be executed again, and I just need to wait for the buffer to contain enough data . In fact, we can save the frameLengthInt variable without having to
return null;
}
... // extract frame
}
Actual behavior
When there is not enough data in the buffer, the length of the data frame recalculate at the next decoding
Steps to reproduce
Non-Required
Minimal yet complete reproducer code (or URL to code)
Non-Required
Netty version
4.1.X
JVM version (e.g. java -version)
Non-Required
OS version (e.g. uname -a)
Non-Required Activitynormanmaurer commented on Mar 15, 2022 @NickleHuang please provide a PR :) added a commit that references this issue on Mar 15, 2022This issue will close once commit 824a0b8 is merged into the '4.2' branch.824a0b8 added a commit that references this issue on Mar 15, 2022This issue will close once commit 457faee is merged into the '4.2' branch.457faee added a commit that references this issue on Jul 8, 2022This issue will close once commit 110bcaf is merged into the 'master' branch.110bcaf bigT commented on Aug 4, 2022 First off, thanks for making and maintaining Netty. Don't take it as a complaint but as a heads up.
This is a braking change. It regresses expected behaviour of the LengthFieldBasedFrameDecoder.
The protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) is marked protected it is deemed a point of extension and therefore is a part of the public API. Nothing in the documentation of the method states what behaviour should be expected by the implementors i.e. whether it is expected to be called once or multiple times. However, prior to the change it was invoked multiple time. As such it is now (was) part of the expected, albeit undocumented, behaviour.
I have an ASN1 frame decoder, that broker after a recent upgrade that I traced to this change. ASN1 allows frames of be variable length, the length field itself is variable width. Prior to the change, if the buffer contained partial frame that truncated the length filed, it was possible to first return a frame length that would ensure enough bytes were read to decode the length header. With a subsequent call to the getUnadjustedFrameLength decoding a full frame length. I.e. it was possible to progressively decode the length of the frame. After the change this is no-longer possible.
Not a show stopper, but I can no-longer use provided LengthFieldBasedFrameDecoder.
Cheers! AbcCreatorDeep commented on Aug 4, 2022
maybe you can revert frameLengthInt to -1 ,read your new frame , But at the moment, there is no such mechanism,you can override getUnadjustedFrameLength and revert frameLengthInt to -1 . make frameLengthInt property to protected? bigT commented on Aug 4, 2022 I've decided that I will override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception instead and re-use as much of the base class as possible. However most of the static utility methods are private so it makes it hard.
If you decide to keep the change, it may be worth updating the documentation to let folks know what is the expected behaviour of the protected methods in the class. AbcCreatorDeep commented on Aug 4, 2022 @normanmaurer man,I've added an method that allows users to repeatedly read frames by overwriting getUnadjustedFrameLength AbcCreatorDeep commented on Aug 4, 2022
i add resetFrameLengthInt method,you can call it to repeat read frame bigT commented on Aug 4, 2022 See my comments on the pull request. added 2 commits that reference this issue on Aug 22, 2022This issue will close once commit 18428ef is merged into the '4.1' branch.18428ef
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|