Skip to content

Commit

Permalink
chore: update to use gun 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
keynslug committed Jan 15, 2025
1 parent 0bf1f88 commit cea8d78
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
[ {gpb, "~> 4.11"}
, {gproc, "0.8.0"}
, {cowboy, {git, "https://github.com/emqx/cowboy", {tag, "2.9.0"}}}
, {gun, {git, "https://github.com/emqx/gun", {tag, "1.3.7"}}}
, {gun, "2.1.0"}
]}.

{xref_checks,
Expand Down
47 changes: 13 additions & 34 deletions src/client/grpc_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
%% XXX: Bad impl.
encoding :: grpc_frame:encoding(),
%% Streams
streams :: #{reference() := stream()},
streams :: #{gun:stream_ref() => stream()},
%% Client options
client_opts :: client_opts(),
%% Flush timer reference
Expand Down Expand Up @@ -387,7 +387,7 @@ handle_info({timeout, TRef, flush_streams_sendbuff},
handle_info({gun_up, GunPid, http2}, State = #state{gun_pid = GunPid}) ->
{noreply, State};

handle_info({gun_down, GunPid, http2, Reason, KilledStreamRefs, _},
handle_info({gun_down, GunPid, http2, Reason, KilledStreamRefs},
State = #state{gun_pid = GunPid, streams = Streams}) ->
Nowts = erlang:system_time(millisecond),
%% Reply killed streams error
Expand Down Expand Up @@ -633,45 +633,24 @@ do_connect(State = #state{server = {_, Host, Port}, client_opts = ClientOpts}) -
GunOpts = maps:get(gun_opts, ClientOpts, #{}),
case gun:open(Host, Port, GunOpts) of
{ok, Pid} ->
case gun_await_up_helper(Pid) of
%% NOTE
%% By default, `gun` retries failed connection attempts 5 times, with
%% 5 seconds delay in-between. Give it a bit more spare time, just in
%% case.
MRef = monitor(process, Pid),
case gun:await_up(Pid, 60_000, MRef) of
{ok, _Protocol} ->
MRef = monitor(process, Pid),
State#state{mref = MRef, gun_pid = Pid};
{error, Reason} ->
gun:close(Pid),
{error, Reason}
{error, {down, Reason}} ->
{error, Reason};
{error, timeout} ->
_ = gun:close(Pid),
{error, timeout}
end;
{error, Reason} ->
{error, Reason}
end.

gun_await_up_helper(Pid) ->
gun_await_up_helper(Pid, 50, undefined).
gun_await_up_helper(_Pid, 0, LastRet) ->
LastRet ;
gun_await_up_helper(Pid, Retry, LastRet) ->
case gun:await_up(Pid, 100) of
{ok, _} = Ret ->
Ret;
{error, timeout} ->
case gun_last_reason(Pid) of
undefined ->
gun_await_up_helper(Pid, Retry-1, LastRet);
Reason ->
{error, Reason}
end;
{error, _} = Ret ->
Ret
end.

gun_last_reason(Pid) ->
%% XXX: Hard-coded to get detailed reason, because gun
%% does not expose it with a function
lists:last(
tuple_to_list(
element(2, sys:get_state(Pid)))
).

%%--------------------------------------------------------------------
%% Helpers

Expand Down

0 comments on commit cea8d78

Please sign in to comment.