Skip to content

Commit

Permalink
fix: retry request when error reason is normal
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed Apr 20, 2023
1 parent aa4b26c commit fc911b1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
@@ -1,5 +1,9 @@
# ehttpc changes

## 0.4.8

- Fix an issue where a race condition would yield an `{error, normal}` return value. This can be caused when the `gun` process terminates when the remote server closes the connection for whatever reason. In this case, we simply retry without consuming "retry credits".

## 0.4.7

- Fix crash when using body headers and no body. When one sent a message with, for example, the content-type header and no body, the underlying gun library would expect a body to come with a gun:data/3 call and would not finish the request. The next request would then crash. See the following issue for more information: https://github.com/ninenines/gun/issues/141
Expand Down
2 changes: 1 addition & 1 deletion src/ehttpc.app.src
@@ -1,6 +1,6 @@
{application, ehttpc,
[{description, "HTTP Client for Erlang/OTP"},
{vsn, "0.4.7"},
{vsn, "0.4.8"},
{registered, []},
{applications, [kernel,
stdlib,
Expand Down
10 changes: 8 additions & 2 deletions src/ehttpc.appup.src
@@ -1,6 +1,9 @@
%% -*-: erlang -*-
{"0.4.7",
%% -*- mode: erlang -*-
{"0.4.8",
[
{"0.4.7", [
{load_module, ehttpc, brutal_purge, soft_purge, []}
]},
{"0.4.6", [
{load_module, ehttpc, brutal_purge, soft_purge, []}
]},
Expand Down Expand Up @@ -47,6 +50,9 @@
]}
],
[
{"0.4.7", [
{load_module, ehttpc, brutal_purge, soft_purge, []}
]},
{"0.4.6", [
{load_module, ehttpc, brutal_purge, soft_purge, []}
]},
Expand Down
9 changes: 9 additions & 0 deletions src/ehttpc.erl
Expand Up @@ -141,6 +141,15 @@ request(Worker, Method, Request, Timeout, Retry) when is_pid(Worker) ->
try gen_server:call(Worker, ?REQ(Method, Request, ExpireAt), Timeout + 500) of
%% gun will reply {gun_down, _Client, _, normal, _KilledStreams, _} message
%% when connection closed by keepalive

%% If `Reason' = `normal', we should just retry without
%% consuming a retry credit, as it could be a race condition
%% where the gun process is down (e.g.: when the server closes
%% the connection), and then requests would be ignored while
%% the `gun' process is terminating.
{error, normal} ->
?LOG(warning, "retrying at no cost!", []),
request(Worker, Method, Request, Timeout, Retry);
{error, Reason} when Retry < 1 ->
{error, Reason};
{error, _} ->
Expand Down

0 comments on commit fc911b1

Please sign in to comment.