From 580ac8cd41a426c30aeee363a9bf3dab2f9e303f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 28 Jul 2017 12:52:50 +0200 Subject: [PATCH] Only flush on channelReadComplete(...) in http2 hello world examples. Motivation: In our http1 hello world example we only flush on channelReadComplete(...) to make better use of gathering writes. We should do the same in http2. Modifications: Only flush in channelReadComplete(...) Result: Better performance and more consistent examples. --- .../frame/server/HelloWorldHttp2Handler.java | 11 ++++++++--- .../multiplex/server/HelloWorldHttp2Handler.java | 11 ++++++++--- .../helloworld/server/HelloWorldHttp1Handler.java | 9 +++++++-- .../helloworld/server/HelloWorldHttp2Handler.java | 7 ++----- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java b/example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java index b7d1e383f0..2ca8c869de 100644 --- a/example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java +++ b/example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java @@ -61,10 +61,15 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { } } + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.flush(); + } + /** * If receive a frame with end-of-stream set, send a pre-canned response. */ - public void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { + private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { int consumed = data.padding() + data.content().readableBytes(); int streamId = data.streamId(); @@ -82,7 +87,7 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { /** * If receive a frame with end-of-stream set, send a pre-canned response. */ - public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) + private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception { if (headers.isEndStream()) { ByteBuf content = ctx.alloc().buffer(); @@ -99,6 +104,6 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { // Send a frame for the response status Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText()); ctx.write(new DefaultHttp2HeadersFrame(headers).streamId(streamId)); - ctx.writeAndFlush(new DefaultHttp2DataFrame(payload, true).streamId(streamId)); + ctx.write(new DefaultHttp2DataFrame(payload, true).streamId(streamId)); } } diff --git a/example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/HelloWorldHttp2Handler.java b/example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/HelloWorldHttp2Handler.java index 3c82a03129..7e5748f725 100644 --- a/example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/HelloWorldHttp2Handler.java +++ b/example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/HelloWorldHttp2Handler.java @@ -60,10 +60,15 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { } } + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.flush(); + } + /** * If receive a frame with end-of-stream set, send a pre-canned response. */ - public void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { + private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { if (data.isEndStream()) { sendResponse(ctx, data.content()); } else { @@ -75,7 +80,7 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { /** * If receive a frame with end-of-stream set, send a pre-canned response. */ - public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) + private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception { if (headers.isEndStream()) { ByteBuf content = ctx.alloc().buffer(); @@ -92,6 +97,6 @@ public class HelloWorldHttp2Handler extends ChannelDuplexHandler { // Send a frame for the response status Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText()); ctx.write(new DefaultHttp2HeadersFrame(headers)); - ctx.writeAndFlush(new DefaultHttp2DataFrame(payload, true)); + ctx.write(new DefaultHttp2DataFrame(payload, true)); } } diff --git a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java index 7277fe6ea9..f5cfbf1fa7 100644 --- a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java +++ b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java @@ -60,13 +60,18 @@ public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler