HTTP/2 Server Example Not Using Flow Controller

Motiviation:
The HTTP/2 server example is not using the outbound flow control.  It is instead using a FrameWriter directly.
This can lead to flow control errors and other comm. related errors

Modifications:
-Force server example to use outbound flow controller

Result:
-Server example should use follow flow control rules.
This commit is contained in:
Scott Mitchell 2014-10-10 18:35:12 -04:00 committed by Norman Maurer
parent ad259a456e
commit c8c69ae300
2 changed files with 14 additions and 17 deletions

View File

@ -64,15 +64,10 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
public Http2ConnectionHandler(Http2Connection connection, Http2FrameReader frameReader, public Http2ConnectionHandler(Http2Connection connection, Http2FrameReader frameReader,
Http2FrameWriter frameWriter, Http2InboundFlowController inboundFlow, Http2FrameWriter frameWriter, Http2InboundFlowController inboundFlow,
Http2OutboundFlowController outboundFlow, Http2FrameListener listener) { Http2OutboundFlowController outboundFlow, Http2FrameListener listener) {
this.encoder = this(DefaultHttp2ConnectionDecoder.newBuilder().connection(connection)
.frameReader(frameReader).inboundFlow(inboundFlow).listener(listener),
DefaultHttp2ConnectionEncoder.newBuilder().connection(connection) DefaultHttp2ConnectionEncoder.newBuilder().connection(connection)
.frameWriter(frameWriter).outboundFlow(outboundFlow).lifecycleManager(this) .frameWriter(frameWriter).outboundFlow(outboundFlow));
.build();
this.decoder =
DefaultHttp2ConnectionDecoder.newBuilder().connection(connection)
.frameReader(frameReader).inboundFlow(inboundFlow).encoder(encoder)
.listener(listener).lifecycleManager(this).build();
clientPrefaceString = clientPrefaceString(connection);
} }
/** /**

View File

@ -28,6 +28,7 @@ import io.netty.handler.codec.http2.DefaultHttp2FrameReader;
import io.netty.handler.codec.http2.DefaultHttp2FrameWriter; import io.netty.handler.codec.http2.DefaultHttp2FrameWriter;
import io.netty.handler.codec.http2.DefaultHttp2Headers; import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Connection; import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
import io.netty.handler.codec.http2.Http2ConnectionHandler; import io.netty.handler.codec.http2.Http2ConnectionHandler;
import io.netty.handler.codec.http2.Http2Exception; import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2FrameAdapter; import io.netty.handler.codec.http2.Http2FrameAdapter;
@ -52,12 +53,13 @@ public class HelloWorldHttp2Handler extends Http2ConnectionHandler {
public HelloWorldHttp2Handler() { public HelloWorldHttp2Handler() {
this(new DefaultHttp2Connection(true), new Http2InboundFrameLogger( this(new DefaultHttp2Connection(true), new Http2InboundFrameLogger(
new DefaultHttp2FrameReader(), logger), new Http2OutboundFrameLogger( new DefaultHttp2FrameReader(), logger), new Http2OutboundFrameLogger(
new DefaultHttp2FrameWriter(), logger)); new DefaultHttp2FrameWriter(), logger), new SimpleHttp2FrameListener());
} }
private HelloWorldHttp2Handler(Http2Connection connection, Http2FrameReader frameReader, private HelloWorldHttp2Handler(Http2Connection connection, Http2FrameReader frameReader,
Http2FrameWriter frameWriter) { Http2FrameWriter frameWriter, SimpleHttp2FrameListener listener) {
super(connection, frameReader, frameWriter, new SimpleHttp2FrameListener(frameWriter)); super(connection, frameReader, frameWriter, listener);
listener.encoder(encoder());
} }
/** /**
@ -83,10 +85,10 @@ public class HelloWorldHttp2Handler extends Http2ConnectionHandler {
} }
private static class SimpleHttp2FrameListener extends Http2FrameAdapter { private static class SimpleHttp2FrameListener extends Http2FrameAdapter {
private Http2FrameWriter frameWriter; private Http2ConnectionEncoder encoder;
public SimpleHttp2FrameListener(Http2FrameWriter frameWriter) { public void encoder(Http2ConnectionEncoder encoder) {
this.frameWriter = frameWriter; this.encoder = encoder;
} }
/** /**
@ -118,8 +120,8 @@ public class HelloWorldHttp2Handler extends Http2ConnectionHandler {
private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) { private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) {
// Send a frame for the response status // Send a frame for the response status
Http2Headers headers = new DefaultHttp2Headers().status(new AsciiString("200")); Http2Headers headers = new DefaultHttp2Headers().status(new AsciiString("200"));
frameWriter.writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise()); encoder.writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
frameWriter.writeData(ctx, streamId, payload, 0, true, ctx.newPromise()); encoder.writeData(ctx, streamId, payload, 0, true, ctx.newPromise());
ctx.flush(); ctx.flush();
} }
}; };