Search code examples
javanettyhttp2

HTTP/2 client - associate response to request - can rely on stream id?


I'm trying to use netty to implement HTTP/2 client. In this example (line 93) they manually increment streamId and put it to the map. When response comes they look for HttpConversionUtil.ExtensionHeaderNames.STREAM_ID header parameter and thus associate the response with the request.

I don't like the idea of increasing streamId by myself. Can I somehow get the id netty's going to use to writeAndFlush the request?

Also, does it take much resources to create a new stream? Or is it just an identifier?


Solution

  • I'm pretty sure, at this moment you can't get streamId generated and used by Netty. Also, I don't like the idea of increasing streamId by myself too, but looks like it's okay to do this with current API.

    I've checked Netty sources and found next things:

    1. HttpToHttp2ConnectionHandler is used for writing requests. It has private method getStreamId which is used in write method for getting value of currentStreamId. But we doesn't have any access to this variable.
    2. getStreamId method uses another method called incrementAndGetNextStreamId. So, again, we can only increment and get new streamId value but can't get the current one.

    Both of these classes marked with annotation @UnstableApi, so maybe this behavior will be change in the future.

    Here's some related links:

    1. A little bit updated HTTP 2 client example from Netty repository.
    2. HTTP/2 Java Client Examples.
    3. Why netty http2 server always use odd number for streamId.