Skip to content

Commit

Permalink
Release netty ByteBuf after use (#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoGuorui666 authored Jul 21, 2024
1 parent f84ab32 commit cdb6efe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ public void process(CommandProcess process) {
}

InputStream input = null;
ByteBuf convertResult = null;

try {
input = new FileInputStream(f);
byte[] bytes = IOUtils.getBytes(input);

ByteBuf convertResult = null;
if (this.decode) {
convertResult = Base64.decode(Unpooled.wrappedBuffer(bytes));
} else {
Expand All @@ -138,6 +138,9 @@ public void process(CommandProcess process) {
process.end(1, "read file error: " + e.getMessage());
return;
} finally {
if (convertResult != null) {
convertResult.release();
}
IOUtils.close(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,25 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;

ByteBuf byteBuf = content.content();
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
ByteBuf byteBuf = null;
try{
byteBuf = content.content();
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);

simpleHttpResponse.setContent(bytes);
simpleHttpResponse.setContent(bytes);

promise.setSuccess(simpleHttpResponse);
promise.setSuccess(simpleHttpResponse);

if (content instanceof LastHttpContent) {
ctx.close();
if (content instanceof LastHttpContent) {
ctx.close();
}
}finally {
if (byteBuf != null) {
byteBuf.release();
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,24 @@ public void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws
String targetUrl = targetUrls.get(0);
SimpleHttpResponse simpleHttpResponse = proxyClient.query(targetUrl);

ByteBuf byteBuf = Base64
.encode(Unpooled.wrappedBuffer(SimpleHttpResponse.toBytes(simpleHttpResponse)));
String requestData = byteBuf.toString(CharsetUtil.UTF_8);

QueryStringEncoder queryEncoder = new QueryStringEncoder("");
queryEncoder.addParam(URIConstans.METHOD, MethodConstants.HTTP_PROXY);
queryEncoder.addParam(URIConstans.PROXY_REQUEST_ID, id);
queryEncoder.addParam(URIConstans.PROXY_RESPONSE_DATA, requestData);

String url = queryEncoder.toString();
ctx.writeAndFlush(new TextWebSocketFrame(url));
ByteBuf byteBuf = null;
try{
byteBuf = Base64
.encode(Unpooled.wrappedBuffer(SimpleHttpResponse.toBytes(simpleHttpResponse)));
String requestData = byteBuf.toString(CharsetUtil.UTF_8);

QueryStringEncoder queryEncoder = new QueryStringEncoder("");
queryEncoder.addParam(URIConstans.METHOD, MethodConstants.HTTP_PROXY);
queryEncoder.addParam(URIConstans.PROXY_REQUEST_ID, id);
queryEncoder.addParam(URIConstans.PROXY_RESPONSE_DATA, requestData);

String url = queryEncoder.toString();
ctx.writeAndFlush(new TextWebSocketFrame(url));
}finally {
if (byteBuf != null) {
byteBuf.release();
}
}
}
}

Expand Down

0 comments on commit cdb6efe

Please sign in to comment.