Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ try_new_h3_connection(Host, Port, Transport, Options, PoolHandler) ->
case hackney_conn:connect(ConnPid, ConnectTimeout) of
ok ->
%% Verify it's HTTP/3
case hackney_conn:get_protocol(ConnPid) of
case catch hackney_conn:get_protocol(ConnPid) of
http3 ->
%% Register for multiplexing
PoolHandler:register_h3(Host, Port, Transport, ConnPid, Options),
{ok, ConnPid};
_ ->
%% Not HTTP/3, close and fail
%% Not HTTP/3 or connection terminated, close and fail
catch hackney_conn:stop(ConnPid),
hackney_altsvc:mark_h3_blocked(Host, Port),
false
Expand Down Expand Up @@ -302,12 +302,18 @@ connect_pool_new(Transport, Host, Port, Options, PoolHandler) ->
end.

%% @private Register HTTP/2 connection for multiplexing if applicable
%% Uses catch to handle race condition where connection terminates before call
maybe_register_h2(ConnPid, Host, Port, Transport, Options, PoolHandler) ->
case hackney_conn:get_protocol(ConnPid) of
case catch hackney_conn:get_protocol(ConnPid) of
http2 ->
%% HTTP/2 negotiated - register for connection sharing
PoolHandler:register_h2(Host, Port, Transport, ConnPid, Options);
http1 ->
ok;
http3 ->
ok;
{'EXIT', _} ->
%% Connection terminated before we could check - ignore
ok
end.

Expand Down
16 changes: 14 additions & 2 deletions src/hackney_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
%% HTTP/2 connection pooling
-export([checkout_h2/4,
register_h2/5,
unregister_h2/2]).
unregister_h2/2,
unregister_h2_all/0]).

%% HTTP/3 connection pooling
-export([checkout_h3/4,
Expand Down Expand Up @@ -173,6 +174,13 @@ unregister_h2(Pid, Options) ->
gen_server:cast(Pool, {unregister_h2, Pid}),
ok.

%% @doc Remove all HTTP/2 connections from the default pool.
%% Used for testing to ensure clean state between tests.
-spec unregister_h2_all() -> ok.
unregister_h2_all() ->
Pool = find_pool(default, []),
gen_server:call(Pool, unregister_h2_all).

%%====================================================================
%% HTTP/3 Connection Pooling
%%====================================================================
Expand Down Expand Up @@ -508,7 +516,11 @@ handle_call({checkout_h3, Key}, _From, #state{h3_connections = H3Conns} = State)
H3Conns2 = maps:remove(Key, H3Conns),
{reply, none, State#state{h3_connections = H3Conns2}}
end
end.
end;

handle_call(unregister_h2_all, _From, State) ->
%% Clear all HTTP/2 connections (for testing)
{reply, ok, State#state{h2_connections = #{}}}.

handle_cast({checkin, _PoolInfo, Pid}, State) ->
State2 = do_checkin(Pid, State),
Expand Down
Loading
Loading