Skip to content

Commit 04146de

Browse files
committed
Optimize Netty frame writing
1 parent 87a8c9f commit 04146de

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

src/main/java/com/rabbitmq/client/impl/NettyFrameHandlerFactory.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,28 @@ private void doWriteFrame(Frame frame) throws IOException {
384384
ByteBuffer bbPayload = frame.getByteBufferPayload();
385385
if (bbPayload != null) {
386386
int payloadSize = bbPayload.remaining();
387-
ByteBuf header = this.channel.alloc().buffer(7);
388-
header.writeByte(frame.getType());
389-
header.writeShort(frame.getChannel());
390-
header.writeInt(payloadSize);
391-
392-
ByteBuf body = Unpooled.wrappedBuffer(bbPayload);
393-
394-
ByteBuf end = this.channel.alloc().buffer(1);
395-
end.writeByte(AMQP.FRAME_END);
396-
397-
ByteBuf composite = Unpooled.wrappedBuffer(header, body, end);
398-
this.channel.writeAndFlush(composite, this.channel.voidPromise());
387+
int totalSize = 7 + payloadSize + 1;
388+
ByteBuf bb = this.channel.alloc().buffer(totalSize);
389+
try {
390+
bb.writeByte(frame.getType());
391+
bb.writeShort(frame.getChannel());
392+
bb.writeInt(payloadSize);
393+
bb.writeBytes(bbPayload);
394+
bb.writeByte(AMQP.FRAME_END);
395+
this.channel.writeAndFlush(bb, this.channel.voidPromise());
396+
} catch (RuntimeException e) {
397+
bb.release();
398+
throw e;
399+
}
399400
} else {
400401
ByteBuf bb = this.channel.alloc().buffer(frame.size());
401-
frame.writeToByteBuf(bb);
402-
this.channel.writeAndFlush(bb, this.channel.voidPromise());
402+
try {
403+
frame.writeToByteBuf(bb);
404+
this.channel.writeAndFlush(bb, this.channel.voidPromise());
405+
} catch (RuntimeException | IOException e) {
406+
bb.release();
407+
throw e;
408+
}
403409
}
404410
}
405411

0 commit comments

Comments
 (0)