Merge buffers for performance reasons if possible when encode http requests/responses.
This commit is contained in:
parent
387f6917af
commit
dab954dfb5
@ -57,6 +57,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
|
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
|
||||||
|
ByteBuf buf = null;
|
||||||
if (msg instanceof HttpMessage) {
|
if (msg instanceof HttpMessage) {
|
||||||
if (state != ST_INIT) {
|
if (state != ST_INIT) {
|
||||||
throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
|
throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
|
||||||
@ -65,12 +66,11 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
@SuppressWarnings({ "unchecked", "CastConflictsWithInstanceof" })
|
@SuppressWarnings({ "unchecked", "CastConflictsWithInstanceof" })
|
||||||
H m = (H) msg;
|
H m = (H) msg;
|
||||||
|
|
||||||
ByteBuf buf = ctx.alloc().buffer();
|
buf = ctx.alloc().buffer();
|
||||||
// Encode the message.
|
// Encode the message.
|
||||||
encodeInitialLine(buf, m);
|
encodeInitialLine(buf, m);
|
||||||
HttpHeaders.encode(m.headers(), buf);
|
HttpHeaders.encode(m.headers(), buf);
|
||||||
buf.writeBytes(CRLF);
|
buf.writeBytes(CRLF);
|
||||||
out.add(buf);
|
|
||||||
state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
|
state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
|
||||||
}
|
}
|
||||||
if (msg instanceof HttpContent || msg instanceof ByteBuf || msg instanceof FileRegion) {
|
if (msg instanceof HttpContent || msg instanceof ByteBuf || msg instanceof FileRegion) {
|
||||||
@ -81,21 +81,41 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
int contentLength = contentLength(msg);
|
int contentLength = contentLength(msg);
|
||||||
if (state == ST_CONTENT_NON_CHUNK) {
|
if (state == ST_CONTENT_NON_CHUNK) {
|
||||||
if (contentLength > 0) {
|
if (contentLength > 0) {
|
||||||
out.add(encodeAndRetain(msg));
|
if (buf != null && buf.writableBytes() >= contentLength && msg instanceof HttpContent) {
|
||||||
|
// merge into other buffer for performance reasons
|
||||||
|
buf.writeBytes(((HttpContent) msg).content());
|
||||||
|
out.add(buf);
|
||||||
|
} else {
|
||||||
|
if (buf != null) {
|
||||||
|
out.add(buf);
|
||||||
|
}
|
||||||
|
out.add(encodeAndRetain(msg));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Need to produce some output otherwise an
|
if (buf != null) {
|
||||||
// IllegalStateException will be thrown
|
out.add(buf);
|
||||||
out.add(EMPTY_BUFFER);
|
} else {
|
||||||
|
// Need to produce some output otherwise an
|
||||||
|
// IllegalStateException will be thrown
|
||||||
|
out.add(EMPTY_BUFFER);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg instanceof LastHttpContent) {
|
if (msg instanceof LastHttpContent) {
|
||||||
state = ST_INIT;
|
state = ST_INIT;
|
||||||
}
|
}
|
||||||
} else if (state == ST_CONTENT_CHUNK) {
|
} else if (state == ST_CONTENT_CHUNK) {
|
||||||
|
if (buf != null) {
|
||||||
|
out.add(buf);
|
||||||
|
}
|
||||||
encodeChunkedContent(ctx, msg, contentLength, out);
|
encodeChunkedContent(ctx, msg, contentLength, out);
|
||||||
} else {
|
} else {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (buf != null) {
|
||||||
|
out.add(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user