Netty HTTP 1.1 Pipelining Support

1.1k views Asked by At

I need to send multiple async requests to a rest server through the same connection and get them executed in FIFO order, I think HTTP 1.1 pipelining is just perfect for this.

I found some related issues on Netty but I couldn't find much on their user guide and nothing on their test cases.

Is HTTP 1.1 pipelining supported on Netty? How would that be implemented?

An example would be greatly appreciated.

Related -unanswered- question: HTTP 1.1 pipelining vs HTTP 2 multiplexing

1

There are 1 answers

4
Ferrybig On

Since Netty is closer to the TCP layer, than to the HTTP layer, sending multiple requests is easy, after setting up the pipeline, just write them.

HttpRequest request1 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
request1.headers().set(HttpHeaderNames.HOST, host);
request1.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request1.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);

channel.writeAndFlush(request1);

HttpRequest request2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
request2.headers().set(HttpHeaderNames.HOST, host);
request2.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request2.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);

channel.writeAndFlush(request2);

And then inside you channelRead method, read them in the same order you have send them.

To properly manage the queues for the packets, you could a solution like this, where you basicly keep a queue, so you know the correct callback to call after a request completes.