fix(gw-exproto): fix grpc remote call

This commit is contained in:
JianBo He 2021-09-03 10:40:38 +08:00
parent 6e7d3d05e4
commit ba9ad47cc1
1 changed files with 18 additions and 13 deletions

View File

@ -22,9 +22,10 @@
-include("src/exproto/include/emqx_exproto.hrl"). -include("src/exproto/include/emqx_exproto.hrl").
-include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/logger.hrl").
-define(IS_QOS(X), (X =:= 0 orelse X =:= 1 orelse X =:= 2)). -define(IS_QOS(X), (X =:= 0 orelse X =:= 1 orelse X =:= 2)).
-define(DEFAULT_CALL_TIMEOUT, 5000).
%% gRPC server callbacks %% gRPC server callbacks
-export([ send/2 -export([ send/2
, close/2 , close/2
@ -117,18 +118,22 @@ to_pid(ConnStr) ->
binary_to_term(base64:decode(ConnStr)). binary_to_term(base64:decode(ConnStr)).
call(ConnStr, Req) -> call(ConnStr, Req) ->
case catch to_pid(ConnStr) of try
{'EXIT', {badarg, _}} -> Pid = to_pid(ConnStr),
{error, ?RESP_PARAMS_TYPE_ERROR, emqx_gateway_conn:call(Pid, Req, ?DEFAULT_CALL_TIMEOUT)
<<"The conn type error">>}; catch
Pid when is_pid(Pid) -> exit : badarg ->
case erlang:is_process_alive(Pid) of {error, ?RESP_PARAMS_TYPE_ERROR, <<"The conn type error">>};
true -> exit : noproc ->
emqx_gateway_conn:call(Pid, Req); {error, ?RESP_CONN_PROCESS_NOT_ALIVE,
false -> <<"Connection process is not alive">>};
{error, ?RESP_CONN_PROCESS_NOT_ALIVE, exit : timeout ->
<<"Connection process is not alive">>} {error, ?RESP_UNKNOWN, <<"Connection is not answered">>};
end Class : Reason : Stk->
?LOG(error, "Call ~p crashed: {~0p, ~0p}, "
"stacktrace: ~0p",
[Class, Reason, Stk]),
{error, ?RESP_UNKNOWN, <<"Unkwown crashs">>}
end. end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------