Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netty 4.1 Optimizations #7942

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frameworks/Java/netty/netty.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ COPY --from=maven /netty/target/netty-example-0.1-jar-with-dependencies.jar app.

EXPOSE 8080

CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-jar", "app.jar"]
CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-Dio.netty.buffer.checkBounds=false", "-Dio.netty.buffer.checkAccessible=false", "-jar", "app.jar"]
5 changes: 3 additions & 2 deletions frameworks/Java/netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<netty.version>4.1.86.Final</netty.version>
<netty.version>4.1.89.Final</netty.version>
<io_uring.version>0.0.18.Final</io_uring.version>
</properties>

<packaging>jar</packaging>
Expand Down Expand Up @@ -41,7 +42,7 @@
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<version>0.0.15.Final</version>
<version>${io_uring.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>

Expand Down
16 changes: 16 additions & 0 deletions frameworks/Java/netty/src/main/java/hello/HelloServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
Expand Down Expand Up @@ -88,6 +90,20 @@ public void run() {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// fast path
if (msg == LastHttpContent.EMPTY_LAST_CONTENT) {
return;
}
if (msg.getClass() == DefaultHttpRequest.class) {
DefaultHttpRequest request = (DefaultHttpRequest) msg;
process(ctx, request);
} else {
channelReadSlowPath(ctx, msg);
}
}

private void channelReadSlowPath(ChannelHandlerContext ctx, Object msg) throws Exception {
// slow path
if (msg instanceof HttpRequest) {
try {
HttpRequest request = (HttpRequest) msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpVersion;

public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {

private ScheduledExecutorService service;
private final ScheduledExecutorService service;

public HelloServerInitializer(ScheduledExecutorService service) {
this.service = service;
Expand All @@ -18,8 +23,29 @@ public HelloServerInitializer(ScheduledExecutorService service) {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("encoder", new HttpResponseEncoder())
.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false))
.addLast("encoder", new HttpResponseEncoder() {
@Override
public boolean acceptOutboundMessage(final Object msg) throws Exception {
if (msg.getClass() == DefaultFullHttpResponse.class) {
return true;
}
return super.acceptOutboundMessage(msg);
}
})
.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false) {

@Override
protected HttpMessage createMessage(final String[] initialLine) throws Exception {
return new DefaultHttpRequest(
HttpVersion.valueOf(initialLine[2]),
HttpMethod.valueOf(initialLine[0]), initialLine[1], validateHeaders);
}

@Override
protected boolean isContentAlwaysEmpty(final HttpMessage msg) {
return false;
}
})
.addLast("handler", new HelloServerHandler(service));
}
}