0

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

Mattx
  • 65
  • 6

1 Answers1

3

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.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79
  • Creating the requests is not the problem, sending them is as I don't want to write the http client myself. Are you aware of any Netty http client (with ssl support) I can use to complete your example? I'm having a look at [1] and [2] at the moment, not sure I'll be able to do this with them. [1] https://github.com/AsyncHttpClient/async-http-client [2] https://github.com/timboudreau/netty-http-client – Mattx Jun 17 '18 at 20:39
  • I don't know of any external clients. If you plan to use Netty directly, you are effectively required to write your own implementation. Note that asking for a library/program recoomendation is off topic for a new StackOverflow question. – Ferrybig Jun 17 '18 at 20:41
  • It looks like it's not simple task to do this using a third party http client, they were designed to avoid this. I've tried setting max-connection-per-host 1 in a few of them and they effectively send the requests in sync mode (ie, waiting for the response of one before sending the other). That makes sense, but the problem is they don't provide a way to change this behavior. – Mattx Jun 17 '18 at 22:05
  • 1
    As far as I know Vert.x supports pipelining at least. – Norman Maurer Jun 20 '18 at 14:42