Skip to content

Commit

Permalink
Merge pull request #670 from kpribylov/fix/pool-checkout-race
Browse files Browse the repository at this point in the history
Fix race condition within pool checkout procedure
  • Loading branch information
benoitc authored Dec 19, 2020
2 parents 96b6d19 + a4de9f3 commit 254ce97
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/hackney_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ start() ->
ok.

%% @doc fetch a socket from the pool
checkout(Host, Port, Transport, #client{options = Opts} = Client) ->
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
%% Fall back to using connect_timeout if checkout_timeout is not set
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, ConnectTimeout),
checkout(Host, Port, Transport, Client) ->
Requester = self(),
Ref = make_ref(),
Fun =
fun() ->
Result =
try
do_checkout(Requester, Host, Port, Transport, Client, ConnectTimeout, CheckoutTimeout)
do_checkout(Requester, Host, Port, Transport, Client)
catch _:_ ->
{error, checkout_failure}
end,
Expand All @@ -79,12 +76,13 @@ checkout(Host, Port, Transport, #client{options = Opts} = Client) ->
receive
{checkout, Ref, Result} ->
Result
after CheckoutTimeout ->
{error, checkout_timeout}
end.

do_checkout(Requester, Host, _Port, Transport, #client{options=Opts,
mod_metrics=Metrics}=Client, ConnectTimeout, CheckoutTimeout) ->
mod_metrics=Metrics}=Client) ->
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
%% Fall back to using connect_timeout if checkout_timeout is not set
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, ConnectTimeout),
{Connection, ConnectOptions} = hackney_connection:new(Client),
RequestRef = Client#client.request_ref,
PoolName = proplists:get_value(pool, Opts, default),
Expand Down

0 comments on commit 254ce97

Please sign in to comment.