refactor(mqtt-worker): avoid unnecessary abstraction

So the code is easier to follow.
This commit is contained in:
Andrew Mayorov 2023-01-31 23:18:46 +03:00
parent 13511d2782
commit f0395be383
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
2 changed files with 45 additions and 38 deletions

View File

@ -198,8 +198,9 @@ on_query_async(_InstId, {send_message, Msg}, Callback, #{name := InstanceId}) ->
?TRACE("QUERY", "async_send_msg_to_remote_node", #{message => Msg, connector => InstanceId}), ?TRACE("QUERY", "async_send_msg_to_remote_node", #{message => Msg, connector => InstanceId}),
case emqx_connector_mqtt_worker:send_to_remote_async(InstanceId, Msg, Callback) of case emqx_connector_mqtt_worker:send_to_remote_async(InstanceId, Msg, Callback) of
ok -> ok ->
% TODO this is racy ok;
{ok, emqx_connector_mqtt_worker:pid(InstanceId)}; {ok, Pid} ->
{ok, Pid};
{error, Reason} -> {error, Reason} ->
classify_error(Reason) classify_error(Reason)
end. end.

View File

@ -67,8 +67,7 @@
%% APIs %% APIs
-export([ -export([
start_link/2, start_link/2,
stop/1, stop/1
pid/1
]). ]).
%% management APIs %% management APIs
@ -175,7 +174,7 @@ mk_client_event_handler(undefined, _Opts) ->
connect(Name) -> connect(Name) ->
#{subscriptions := Subscriptions} = get_config(Name), #{subscriptions := Subscriptions} = get_config(Name),
case emqtt:connect(pid(Name)) of case emqtt:connect(get_pid(Name)) of
{ok, Properties} -> {ok, Properties} ->
case subscribe_remote_topics(Name, Subscriptions) of case subscribe_remote_topics(Name, Subscriptions) of
ok -> ok ->
@ -206,12 +205,8 @@ subscribe_remote_topics(_Ref, undefined) ->
stop(Ref) -> stop(Ref) ->
emqtt:stop(ref(Ref)). emqtt:stop(ref(Ref)).
pid(Name) ->
gproc:lookup_pid(?NAME(Name)).
status(Ref) -> status(Ref) ->
trycall( try
fun() ->
Info = emqtt:info(ref(Ref)), Info = emqtt:info(ref(Ref)),
case proplists:get_value(socket, Info) of case proplists:get_value(socket, Info) of
Socket when Socket /= undefined -> Socket when Socket /= undefined ->
@ -219,24 +214,19 @@ status(Ref) ->
undefined -> undefined ->
connecting connecting
end end
end, catch
#{noproc => disconnected} exit:{noproc, _} ->
). disconnected
end.
ping(Ref) -> ping(Ref) ->
emqtt:ping(ref(Ref)). emqtt:ping(ref(Ref)).
send_to_remote(Name, MsgIn) -> send_to_remote(Name, MsgIn) ->
trycall( trycall(fun() -> do_send(Name, export_msg(Name, MsgIn)) end).
fun() -> do_send(Name, export_msg(Name, MsgIn)) end,
#{
badarg => {error, disconnected},
noproc => {error, disconnected}
}
).
do_send(Name, {true, Msg}) -> do_send(Name, {true, Msg}) ->
case emqtt:publish(pid(Name), Msg) of case emqtt:publish(get_pid(Name), Msg) of
ok -> ok ->
ok; ok;
{ok, #{reason_code := RC}} when {ok, #{reason_code := RC}} when
@ -263,13 +253,16 @@ do_send(_Name, false) ->
ok. ok.
send_to_remote_async(Name, MsgIn, Callback) -> send_to_remote_async(Name, MsgIn, Callback) ->
trycall( trycall(fun() -> do_send_async(Name, export_msg(Name, MsgIn), Callback) end).
fun() -> do_send_async(Name, export_msg(Name, MsgIn), Callback) end,
#{badarg => {error, disconnected}}
).
do_send_async(Name, {true, Msg}, Callback) -> do_send_async(Name, {true, Msg}, Callback) ->
emqtt:publish_async(pid(Name), Msg, _Timeout = infinity, Callback); Pid = get_pid(Name),
case emqtt:publish_async(Pid, Msg, _Timeout = infinity, Callback) of
ok ->
{ok, Pid};
{error, _} = Error ->
Error
end;
do_send_async(_Name, false, _Callback) -> do_send_async(_Name, false, _Callback) ->
ok. ok.
@ -278,14 +271,14 @@ ref(Pid) when is_pid(Pid) ->
ref(Term) -> ref(Term) ->
?REF(Term). ?REF(Term).
trycall(Fun, Else) -> trycall(Fun) ->
try try
Fun() Fun()
catch catch
error:badarg -> throw:noproc ->
maps:get(badarg, Else); {error, disconnected};
exit:{noproc, _} -> exit:{noproc, _} ->
maps:get(noproc, Else) {error, disconnected}
end. end.
format_mountpoint(undefined) -> format_mountpoint(undefined) ->
@ -325,8 +318,21 @@ pre_process_conf(Key, Conf) ->
Conf#{Key => Val} Conf#{Key => Val}
end. end.
get_pid(Name) ->
case gproc:where(?NAME(Name)) of
Pid when is_pid(Pid) ->
Pid;
undefined ->
throw(noproc)
end.
get_config(Name) -> get_config(Name) ->
gproc:lookup_value(?NAME(Name)). try
gproc:lookup_value(?NAME(Name))
catch
error:badarg ->
throw(noproc)
end.
export_msg(Name, Msg) -> export_msg(Name, Msg) ->
case get_config(Name) of case get_config(Name) of