Skip to content

Commit

Permalink
add grpcbox_channel stop/2 alternative
Browse files Browse the repository at this point in the history
Adds stop/2 for providing more graceful termination of a channel.
stop/1 will kill h2 streams which causes crash reports. For a similar
termination without crash reports grpcbox_channel:stop(Name, shutdown)
can now be called. Calling grpcbox_channel:stop(Name, normal) will
shutdown the h2_connection with stop instead of calling exit for it.
  • Loading branch information
psalin committed Sep 29, 2022
1 parent 5ea123f commit d2d7813
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/grpcbox_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
-export([start_link/3,
is_ready/1,
pick/2,
stop/1]).
stop/1,
stop/2]).
-export([init/1,
callback_mode/0,
terminate/3,
Expand Down Expand Up @@ -78,7 +79,9 @@ interceptor(Name, CallType) ->
end.

stop(Name) ->
gen_statem:stop(?CHANNEL(Name)).
stop(Name, {shutdown, force_delete}).
stop(Name, Reason) ->
gen_statem:stop(?CHANNEL(Name), Reason, infinity).

init([Name, Endpoints, Options]) ->
process_flag(trap_exit, true),
Expand Down Expand Up @@ -128,8 +131,11 @@ idle(EventType, EventContent, Data) ->
handle_event(_, _, Data) ->
{keep_state, Data}.

terminate(_Reason, _State, #data{pool=Name}) ->
gproc_pool:force_delete(Name),
terminate({shutdown, force_delete}, _State, #data{pool=Name}) ->
gproc_pool:force_delete(Name);
terminate(Reason, _State, #data{pool=Name}) ->
[grpcbox_subchannel:stop(Pid, Reason) || {_Channel, Pid} <- gproc_pool:active_workers(Name)],
gproc_pool:delete(Name),
ok.

insert_interceptors(Name, Interceptors) ->
Expand Down
15 changes: 13 additions & 2 deletions src/grpcbox_subchannel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

-export([start_link/5,
conn/1,
conn/2]).
conn/2,
stop/2]).
-export([init/1,
callback_mode/0,
terminate/3,
Expand Down Expand Up @@ -35,6 +36,9 @@ conn(Pid, Timeout) ->
exit:{timeout, _} -> {error, timeout}
end.

stop(Pid, Reason) ->
gen_statem:stop(Pid, Reason, infinity).

init([Name, Channel, Endpoint, Encoding, StatsHandler]) ->
process_flag(trap_exit, true),
gproc_pool:connect_worker(Channel, Name),
Expand Down Expand Up @@ -90,12 +94,19 @@ terminate(_Reason, _State, #data{conn=undefined,
gproc_pool:disconnect_worker(Channel, Endpoint),
gproc_pool:remove_worker(Channel, Endpoint),
ok;
terminate(_Reason, _State, #data{conn=Pid,
terminate(normal, _State, #data{conn=Pid,
endpoint=Endpoint,
channel=Channel}) ->
h2_connection:stop(Pid),
gproc_pool:disconnect_worker(Channel, Endpoint),
gproc_pool:remove_worker(Channel, Endpoint),
ok;
terminate(Reason, _State, #data{conn=Pid,
endpoint=Endpoint,
channel=Channel}) ->
exit(Pid, Reason),
gproc_pool:disconnect_worker(Channel, Endpoint),
gproc_pool:remove_worker(Channel, Endpoint),
ok.

connect(Data=#data{conn=undefined,
Expand Down

0 comments on commit d2d7813

Please sign in to comment.