Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http request): catch error when gen_server:call timed out and retry when disconnected due to keepalive #263

Merged
merged 2 commits into from
Jun 28, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.