Skip to content

Commit

Permalink
fix: fill up the global client tab when relup
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Jun 22, 2024
1 parent b4d38dc commit 1477424
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/ecpool.appup.src
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
%% NOTE: MUST start ecpool_monitor before any load_module instructions
{add_module, ecpool_monitor},
{apply, {ecpool_monitor, ensure_monitor_started, []}},
{apply, {ecpool_monitor, update_clients_global, []}},
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool_sup, brutal_purge, soft_purge, []},
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
Expand All @@ -13,6 +14,7 @@
{<<"0\\.5\\.[0-2]">>, [
{add_module, ecpool_monitor},
{apply, {ecpool_monitor, ensure_monitor_started, []}},
{apply, {ecpool_monitor, update_clients_global, []}},
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool_sup, brutal_purge, soft_purge, []},
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
Expand All @@ -22,6 +24,7 @@
{"0.4.2", [
{add_module, ecpool_monitor},
{apply, {ecpool_monitor, ensure_monitor_started, []}},
{apply, {ecpool_monitor, update_clients_global, []}},
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool, brutal_purge, soft_purge, []},
Expand All @@ -34,21 +37,24 @@
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool_sup, brutal_purge, soft_purge, []},
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
{load_module, ecpool, brutal_purge, soft_purge, []}
{load_module, ecpool, brutal_purge, soft_purge, []},
{apply, {ecpool_monitor, ensure_monitor_stopped, []}}
]},
{<<"0\\.5\\.[0-2]">>, [
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool_sup, brutal_purge, soft_purge, []},
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
{load_module, ecpool, brutal_purge, soft_purge, []},
{load_module, ecpool_worker_sup, brutal_purge, soft_purge, []}
{load_module, ecpool_worker_sup, brutal_purge, soft_purge, []},
{apply, {ecpool_monitor, ensure_monitor_stopped, []}}
]},
{"0.4.2", [
{load_module, ecpool_worker, brutal_purge, soft_purge, []},
{load_module, ecpool_pool, brutal_purge, soft_purge, []},
{load_module, ecpool, brutal_purge, soft_purge, []},
{load_module, ecpool_worker_sup, brutal_purge, soft_purge, []},
{load_module, ecpool_sup, brutal_purge, soft_purge, []}
{load_module, ecpool_sup, brutal_purge, soft_purge, []},
{apply, {ecpool_monitor, ensure_monitor_stopped, []}}
]}
]
}.
9 changes: 7 additions & 2 deletions src/ecpool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,13 @@ with_worker(false, Action, _Mode) when ?IS_ACTION(Action) ->
{error, ecpool_empty};
with_worker(Worker, Action, no_handover) when ?IS_ACTION(Action) ->
case ecpool_monitor:get_client_global(Worker) of
{ok, Client} -> exec(Action, Client);
{error, Reason} -> {error, Reason}
{ok, Client} ->
exec(Action, Client);
{error, _} ->
case ecpool_worker:client(Worker) of
{ok, Client} -> exec(Action, Client);
{error, _} = Err -> Err
end
end;
with_worker(Worker, Action, handover) when ?IS_ACTION(Action) ->
ecpool_worker:exec(Worker, Action, infinity);
Expand Down
43 changes: 39 additions & 4 deletions src/ecpool_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@
-behaviour(gen_server).

%% API
-export([start_link/0, ensure_monitor_started/0, monitor_spec/0]).
-export([reg_worker/0, put_client_global/1, get_client_global/1]).
-export([ start_link/0
, ensure_monitor_started/0
, ensure_monitor_stopped/0
, monitor_spec/0
]).

-export([ update_clients_global/0
, reg_worker/0
, get_all_global_clients/0
, put_client_global/1
, put_client_global/2
, get_client_global/1
]).

%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
code_change/3
]).

-define(DISCOVERY_TAB, ecpool_global_client_discovery).

Expand All @@ -42,6 +54,13 @@ ensure_monitor_started() ->
{error, _} = Error -> Error
end.

ensure_monitor_stopped() ->
case supervisor:terminate_child(ecpool_sup, ecpool_monitor) of
ok -> supervisor:delete_child(ecpool_sup, ecpool_monitor);
{error, not_found} -> ok;
Error -> Error
end.

monitor_spec() ->
#{
id => ecpool_monitor,
Expand All @@ -52,8 +71,24 @@ monitor_spec() ->
modules => [ecpool_monitor]
}.

update_clients_global() ->
lists:foreach(fun({PoolName, _}) ->
lists:foreach(fun({_, WrokerPid}) ->
case ecpool_worker:client(WrokerPid) of
{ok, Client} -> put_client_global(WrokerPid, Client);
_ -> ok
end
end, ecpool:workers(PoolName))
end, ecpool_sup:pools()).

get_all_global_clients() ->
ets:tab2list(?DISCOVERY_TAB).

put_client_global(Client) ->
ets:insert(?DISCOVERY_TAB, {self(), Client}),
put_client_global(self(), Client).

put_client_global(WrokerPid, Client) ->
ets:insert(?DISCOVERY_TAB, {WrokerPid, Client}),
ok.

get_client_global(WorkerPid) ->
Expand Down

0 comments on commit 1477424

Please sign in to comment.