Skip to content

Commit

Permalink
Merge pull request #263 from emqx/fix/http-request
Browse files Browse the repository at this point in the history
fix(http request): catch error when gen_server:call timed out and retry when disconnected due to keepalive
  • Loading branch information
tigercl committed Jun 28, 2021
2 parents 40da87f + b0c6120 commit d52202c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/emqx_auth_http.appup.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*-: erlang -*-
{VSN,
[
{<<"4.2.([7-9]|(10))">>, [
{<<"4.2.([7-9]|(10)|(11)|(12))">>, [
{load_module, emqx_http_client, brutal_purge, soft_purge, []}
]},
{"4.2.6", [
Expand All @@ -14,7 +14,7 @@
{<<".*">>, []}
],
[
{<<"4.2.([7-9]|(10))">>, [
{<<"4.2.([7-9]|(10)|(11)|(12))">>, [
{load_module, emqx_http_client, brutal_purge, soft_purge, []}
]},
{"4.2.6", [
Expand All @@ -26,4 +26,4 @@
]},
{<<".*">>, []}
]
}.
}.
29 changes: 21 additions & 8 deletions src/emqx_http_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ start_link(Pool, Id, Opts) ->
gen_server:start_link(?MODULE, [Pool, Id, Opts], []).

request(Method, Pool, Req) ->
request(Method, Pool, Req, 5000).
request(Method, Pool, Req, 5000, 3).

request(get, Pool, {Path, Headers}, Timeout) ->
call(pick(Pool), {get, {Path, Headers}, Timeout}, Timeout + 1000);
request(Method, Pool, {Path, Headers, Body}, Timeout) ->
call(pick(Pool), {Method, {Path, Headers, Body}, Timeout}, Timeout + 1000).
request(Method, Pool, Req, Timeout) ->
request(Method, Pool, Req, Timeout, 3).

request(Method, Pool, Req, Timeout, Retry) ->
request_(pick(Pool), Method, Req, Timeout, Retry).

%%--------------------------------------------------------------------
%% gen_server callbacks
Expand Down Expand Up @@ -234,8 +235,20 @@ gun_opts([{transport_opts, TransportOpts} | Opts], Acc) ->
gun_opts([_ | Opts], Acc) ->
gun_opts(Opts, Acc).

call(ChannPid, Msg, Timeout) ->
gen_server:call(ChannPid, Msg, Timeout).
request_(_Worker, _Method, _Req, _Timeout, 0) ->
{error, normal};
request_(Worker, Method, Req, Timeout, Retry) ->
try gen_server:call(Worker, {Method, Req, Timeout}, Timeout + 1000) of
{error, normal} ->
request_(Worker, Method, Req, Timeout, Retry - 1);
{error, Reason} ->
{error, Reason};
Other ->
Other
catch
exit:{timeout, _Details} ->
{error, timeout}
end.

pick(Pool) ->
gproc_pool:pick_worker(Pool).
Expand All @@ -255,4 +268,4 @@ flush_stream(Client, StreamRef) ->
flush_stream(Client, StreamRef)
after 0 ->
ok
end.
end.

0 comments on commit d52202c

Please sign in to comment.