fix(mqtt_bridge): the mqtt bridge hangs with an unreachable IP

This commit is contained in:
Shawn 2021-11-24 17:11:04 +08:00
parent b6064ce2c0
commit a54668e83b
1 changed files with 36 additions and 22 deletions

View File

@ -410,7 +410,24 @@ start_resource(ResId, PoolName, Options) ->
end.
test_resource_status(PoolName) ->
IsConnected = fun(Worker) ->
Parent = self(),
Pids = [spawn(fun() -> Parent ! {self(), get_worker_status(Worker)} end)
|| {_WorkerName, Worker} <- ecpool:workers(PoolName)],
try
Status = [
receive {Pid, R} -> R
after 1000 -> %% get_worker_status/1 should be a quick operation
throw({timeout, Pid})
end || Pid <- Pids],
lists:any(fun(St) -> St =:= true end, Status)
catch
throw:Reason ->
?LOG(error, "Get mqtt bridge status timeout: ~p", [Reason]),
lists:foreach(fun(Pid) -> exit(Pid, kill) end, Pids),
false
end.
get_worker_status(Worker) ->
case ecpool_worker:client(Worker) of
{ok, Bridge} ->
try emqx_bridge_worker:status(Bridge) of
@ -421,10 +438,7 @@ test_resource_status(PoolName) ->
end;
{error, _} ->
false
end
end,
Status = [IsConnected(Worker) || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
lists:any(fun(St) -> St =:= true end, Status).
end.
-spec(on_get_resource_status(ResId::binary(), Params::map()) -> Status::map()).
on_get_resource_status(_ResId, #{<<"pool">> := PoolName}) ->