Merge pull request #10217 from JimMoen/conn-health-check-with-worker

fix: for connection used ecpool, let worker do health check fun
This commit is contained in:
JimMoen 2023-03-27 09:51:52 +08:00 committed by GitHub
commit 798597c834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 10 deletions

View File

@ -67,13 +67,14 @@ stop_pool(Name) ->
health_check_ecpool_workers(PoolName, CheckFunc) ->
health_check_ecpool_workers(PoolName, CheckFunc, ?HEALTH_CHECK_TIMEOUT).
health_check_ecpool_workers(PoolName, CheckFunc, Timeout) when is_function(CheckFunc) ->
health_check_ecpool_workers(PoolName, CheckFunc, Timeout) ->
Workers = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
DoPerWorker =
fun(Worker) ->
case ecpool_worker:client(Worker) of
{ok, Conn} ->
erlang:is_process_alive(Conn) andalso CheckFunc(Conn);
erlang:is_process_alive(Conn) andalso
ecpool_worker:exec(Worker, CheckFunc, Timeout);
_ ->
false
end

View File

@ -31,8 +31,7 @@
connect/1,
do_get_status/1,
do_async_reply/2,
worker_do_query/4,
worker_do_get_status/1
worker_do_query/4
]).
-import(hoconsc, [mk/2, enum/1, ref/2]).
@ -165,18 +164,14 @@ on_get_status(_InstanceId, #{poolname := Pool}) ->
Health = emqx_plugin_libs_pool:health_check_ecpool_workers(Pool, fun ?MODULE:do_get_status/1),
status_result(Health).
do_get_status(Conn) ->
do_get_status(_Conn) ->
%% because the dynamodb driver connection process is the ecpool worker self
%% so we must call the checker function inside the worker
ListTables = ecpool_worker:exec(Conn, {?MODULE, worker_do_get_status, []}, infinity),
case ListTables of
case erlcloud_ddb2:list_tables() of
{ok, _} -> true;
_ -> false
end.
worker_do_get_status(_) ->
erlcloud_ddb2:list_tables().
status_result(_Status = true) -> connected;
status_result(_Status = false) -> connecting.