Merge pull request #6567 from HJianBo/gw-review-r4

This commit is contained in:
JianBo He 2022-01-04 11:24:03 +08:00 committed by GitHub
commit 126924bc54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 27 deletions

View File

@ -275,7 +275,7 @@ ret_gw(GwName, {ok, #{raw_config := GwConf}}) ->
end, maps:to_list(SubConf)),
[NLConfs | Acc]
end, [], maps:to_list(LsConf)),
{ok, maps:merge(GwConf1, #{<<"listeners">> => NLsConf})};
{ok, maps:merge(GwConf1, #{<<"listeners">> => lists:append(NLsConf)})};
ret_gw(_GwName, Err) -> Err.
ret_authn(GwName, {ok, #{raw_config := GwConf}}) ->

View File

@ -46,7 +46,6 @@
]).
-export([ stringfy/1
, parse_address/1
]).
-export([ normalize_config/1
@ -332,19 +331,6 @@ stringfy(T) when is_list(T); is_binary(T) ->
stringfy(T) ->
iolist_to_binary(io_lib:format("~0p", [T])).
-spec parse_address(binary() | list()) -> {list(), integer()}.
parse_address(S) when is_binary(S); is_list(S) ->
S1 = case is_binary(S) of
true -> lists:reverse(binary_to_list(S));
_ -> lists:reverse(S)
end,
case re:split(S1, ":", [{parts, 2}, {return, list}]) of
[Port0, Host0] ->
{lists:reverse(Host0), list_to_integer(lists:reverse(Port0))};
_ ->
error(badarg)
end.
-spec normalize_config(emqx_config:config())
-> list({ Type :: udp | tcp | ssl | dtls
, Name :: atom()

View File

@ -148,13 +148,16 @@ stop_grpc_server(GwName) ->
start_grpc_client_channel(_GwName, undefined) ->
undefined;
start_grpc_client_channel(GwName, Options = #{address := Address}) ->
{Host, Port} = try emqx_gateway_utils:parse_address(Address)
catch error : badarg ->
throw({badconf, #{key => address,
value => Address,
reason => illegal_grpc_address
}})
end,
#{host := Host, port := Port} =
case emqx_http_lib:uri_parse(Address) of
{ok, URIMap0} -> URIMap0;
{error, _Reason} ->
throw({badconf, #{key => address,
value => Address,
reason => illegal_grpc_address
}})
end,
case maps:to_list(maps:get(ssl, Options, #{})) of
[] ->
SvrAddr = compose_http_uri(http, Host, Port),
@ -170,7 +173,7 @@ start_grpc_client_channel(GwName, Options = #{address := Address}) ->
compose_http_uri(Scheme, Host, Port) ->
lists:flatten(
io_lib:format(
"~s://~s:~w", [Scheme, Host, Port])).
"~s://~s:~w", [Scheme, inet:ntoa(Host), Port])).
stop_grpc_client_channel(GwName) ->
_ = grpc_client_sup:stop_channel_pool(GwName),

View File

@ -92,7 +92,7 @@ on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
on_gateway_unload(_Gateway = #{ name := GwName,
config := Config
}, _GwState = #{registry := RegPid}) ->
exit(RegPid, kill),
}, _GwState = #{registry := _RegPid}) ->
_ = try emqx_lwm2m_xml_object_db:stop() catch _ : _ -> ok end,
Listeners = emqx_gateway_utils:normalize_config(Config),
emqx_gateway_utils:stop_listeners(GwName, Listeners).

View File

@ -69,7 +69,7 @@ set_special_cfg(emqx_gateway) ->
emqx_config:put(
[gateway, exproto],
#{server => #{bind => 9100},
handler => #{address => "127.0.0.1:9001"},
handler => #{address => "http://127.0.0.1:9001"},
listeners => listener_confs(LisType)
});
set_special_cfg(_App) ->

View File

@ -172,7 +172,7 @@ t_gateway_exproto(_) ->
%% post
GwConf = #{name => <<"exproto">>,
server => #{bind => <<"9100">>},
handler => #{address => <<"127.0.0.1:9001">>},
handler => #{address => <<"http://127.0.0.1:9001">>},
listeners => [
#{name => <<"def">>, type => <<"tcp">>, bind => <<"7993">>}
]