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 Norman Maurer
parent ff9667b1f9
commit 574e8102b6
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) { if (msg instanceof StompFrame) {
StompFrame frame = (StompFrame) msg; StompFrame frame = (StompFrame) msg;
ByteBuf frameBuf = encodeFrame(frame, ctx); ByteBuf frameBuf = encodeFrame(frame, ctx);
if (frame.content().isReadable()) {
out.add(frameBuf); out.add(frameBuf);
ByteBuf contentBuf = encodeContent(frame, ctx); ByteBuf contentBuf = encodeContent(frame, ctx);
out.add(contentBuf); out.add(contentBuf);
} else {
frameBuf.writeByte(StompConstants.NUL);
out.add(frameBuf);
}
} else if (msg instanceof StompHeadersSubframe) { } else if (msg instanceof StompHeadersSubframe) {
StompHeadersSubframe frame = (StompHeadersSubframe) msg; StompHeadersSubframe frame = (StompHeadersSubframe) msg;
ByteBuf buf = encodeFrame(frame, ctx); ByteBuf buf = encodeFrame(frame, ctx);

View File

@ -83,4 +83,18 @@ public class StompSubframeEncoderTest {
assertTrue(fullFrame.release()); 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());
}
} }