How HTTP/2 connection multiplexing works
masterUnlike HTTP/1.1, where each request typically requires its own connection, HTTP/2 allows multiple concurrent requests to share a single TCP connection by multiplexing them as independent "streams".
- Automatic Multiplexing: When using the high-level API,
hackney_poolautomatically reuses existing HTTP/2 connections for the same{Host, Port, Transport}tuple. - Concurrent Requests: You can fire multiple requests in parallel on the same connection. Responses may arrive out of order due to multiplexing.
- Stream Isolation: Each request is assigned a unique
StreamId. Thehackney_connprocess (agen_statem) manages these streams and routes responses back to the correct caller.
%% All three requests share ONE TCP connection
{ok, _, _, _} = hackney:get(<<"https://nghttp2.org/">>).
{ok, _, _, _} = hackney:get(<<"https://nghttp2.org/blog/">>).
{ok, _, _, _} = hackney:get(<<"https://nghttp2.org/documentation/">>).