Skip to content

Commit

Permalink
fix(connection_sup): mimic supervisor:which_children/1
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu authored and HJianBo committed Jul 16, 2020
1 parent fc76924 commit 87ac8aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/esockd_connection_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ handle_call({add_rule, RawRule}, _From, State = #state{access_rules = Rules}) ->
{reply, {error, bad_access_rule}, State}
end;

%% mimic the supervisor's which_children reply
handle_call(which_children, _From, State = #state{curr_connections = Conns, mfargs = MFA}) ->
Mod = get_module(MFA),
{reply, [{undefined, Pid, worker, [Mod]}
|| Pid <- maps:keys(Conns), erlang:is_process_alive(Pid)], State};

handle_call(Req, _From, State) ->
?ERROR_MSG("Unexpected call: ~p", [Req]),
{reply, ignore, State}.
Expand Down Expand Up @@ -375,3 +381,6 @@ report_error(Error, Reason, Pid, #state{mfargs = MFA}) ->
{mfargs, MFA}]}],
error_logger:error_report(supervisor_report, ErrorMsg).

get_module({M, _F, _A}) -> M;
get_module({M, _F}) -> M;
get_module(M) -> M.
5 changes: 5 additions & 0 deletions src/esockd_udp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ handle_call({add_rule, RawRule}, _From, State = #state{access_rules = Rules}) ->
{reply, {error, bad_access_rule}, State}
end;

%% mimic the supervisor's which_children reply
handle_call(which_children, _From, State = #state{peers = Peers, mfa = {Mod, _Func, _Args}}) ->
{reply, [{undefined, Pid, worker, [Mod]}
|| Pid <- maps:keys(Peers), is_pid(Pid), erlang:is_process_alive(Pid)], State};

handle_call(Req, _From, State) ->
?ERROR_MSG("Unexpected call: ~p", [Req]),
{reply, ignore, State}.
Expand Down

0 comments on commit 87ac8aa

Please sign in to comment.