Search code examples
jettyclienthttp2

Recycling Jetty Http2 Client streams -- best practice?


We have a Jetty Http2 Client constructed roughly as the example here.

Each request processed by the client calls session.newStream(...). It appears that old streams are not GC-ed. However, we can't seem to find a good way, in the API, to either recycle them, or close them.

Should we set a very small idle timeout using streamPromise.get().setIdleTimeout(t)?

Should we keep the Stream object, mark it when an exchange finishes, then reuse it? In this case though, we also need to recycle the listener, which makes is stateful.

Is there a way to "close" a Stream object, or mark it for GC? Simply setting it to null doesn't seem very API-ish.


Solution

  • Streams that are closed are GCed.

    Streams support half closes, so in order for a stream to be closed you need to send a frame with the end_stream flag set, and receive a frame with the end_stream flag set.

    If you use HTTP2Client directly, chances are that you're not ending the stream on your side (i.e. you send frames, but forget to set the end_stream flag on the last frame you send), or the server does not end the stream (which would be a server bug).

    Either case, turning on DEBUG logging for category org.eclipse.jetty.http2 on the client will tell you whether the frames have the end_stream flag set, and report when streams are removed - you just need to parse the possibly large-ish log files.