Fix allocate additional buffer for encoding stompFrame without readab… (#10150)

Motivation:

Not always STOMP frames contain any payload some times it just headers. So we wan't allocate additional buffer with NULL content for this situation.

Modification:

Modify StompSubframeEncoder to check if content is readable or not. If content is not readable just add NULL byte to encoded header buffer.

Result:

Less allocations
This commit is contained in:
Andrey Mizurov 2020-03-31 17:02:06 +03:00 committed by GitHub
parent fbc6709686
commit 7309f2a13d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -34,9 +34,14 @@ public class StompSubframeEncoder extends MessageToMessageEncoder<StompSubframe>
if (msg instanceof StompFrame) {
StompFrame frame = (StompFrame) msg;
ByteBuf frameBuf = encodeFrame(frame, ctx);
out.add(frameBuf);
ByteBuf contentBuf = encodeContent(frame, ctx);
out.add(contentBuf);
if (frame.content().isReadable()) {
out.add(frameBuf);
ByteBuf contentBuf = encodeContent(frame, ctx);
out.add(contentBuf);
} else {
frameBuf.writeByte(StompConstants.NUL);
out.add(frameBuf);
}
} else if (msg instanceof StompHeadersSubframe) {
StompHeadersSubframe frame = (StompHeadersSubframe) msg;
ByteBuf buf = encodeFrame(frame, ctx);

View File

@ -83,4 +83,18 @@ public class StompSubframeEncoderTest {
assertTrue(fullFrame.release());
}
@Test
public void testOneBufferForStompFrameWithEmptyContent() {
StompFrame connectedFrame = new DefaultStompFrame(StompCommand.CONNECTED);
connectedFrame.headers().set(StompHeaders.VERSION, "1.2");
assertTrue(channel.writeOutbound(connectedFrame));
ByteBuf stompBuffer = channel.readOutbound();
assertNotNull(stompBuffer);
assertNull(channel.readOutbound());
assertEquals("CONNECTED\nversion:1.2\n\n\0", stompBuffer.toString(CharsetUtil.UTF_8));
assertTrue(stompBuffer.release());
}
}