forked from dgiot/dgiot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emqx_rpc.erl
74 lines (59 loc) · 2.26 KB
/
emqx_rpc.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
%%--------------------------------------------------------------------
%% Copyright (c) 2017-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
%% @doc wrap gen_rpc?
-module(emqx_rpc).
-export([ call/4
, call/5
, cast/4
, cast/5
, multicall/4
, multicall/5
]).
-compile({inline,
[ rpc_node/1
, rpc_nodes/1
]}).
-define(RPC, gen_rpc).
-define(DefaultClientNum, 1).
call(Node, Mod, Fun, Args) ->
filter_result(?RPC:call(rpc_node(Node), Mod, Fun, Args)).
call(Key, Node, Mod, Fun, Args) ->
filter_result(?RPC:call(rpc_node({Key, Node}), Mod, Fun, Args)).
multicall(Nodes, Mod, Fun, Args) ->
?RPC:multicall(rpc_nodes(Nodes), Mod, Fun, Args).
multicall(Key, Nodes, Mod, Fun, Args) ->
?RPC:multicall(rpc_nodes([{Key, Node} || Node <- Nodes]), Mod, Fun, Args).
cast(Node, Mod, Fun, Args) ->
filter_result(?RPC:cast(rpc_node(Node), Mod, Fun, Args)).
cast(Key, Node, Mod, Fun, Args) ->
filter_result(?RPC:cast(rpc_node({Key, Node}), Mod, Fun, Args)).
rpc_node(Node) when is_atom(Node) ->
ClientNum = application:get_env(gen_rpc, tcp_client_num, ?DefaultClientNum),
{Node, rand:uniform(ClientNum)};
rpc_node({Key, Node}) when is_atom(Node) ->
ClientNum = application:get_env(gen_rpc, tcp_client_num, ?DefaultClientNum),
{Node, erlang:phash2(Key, ClientNum) + 1}.
rpc_nodes(Nodes) ->
rpc_nodes(Nodes, []).
rpc_nodes([], Acc) ->
Acc;
rpc_nodes([Node | Nodes], Acc) ->
rpc_nodes(Nodes, [rpc_node(Node) | Acc]).
filter_result({Error, Reason})
when Error =:= badrpc; Error =:= badtcp ->
{badrpc, Reason};
filter_result(Delivery) ->
Delivery.