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:
parent
ff9667b1f9
commit
574e8102b6
@ -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);
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user