From 2a20f110b9c9f128d1d2aef0f6e23c591ae0cb04 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 29 Dec 2021 19:04:49 +0800 Subject: [PATCH 1/4] fix(gw): use emqx_http_lib to parse uri --- apps/emqx_gateway/src/emqx_gateway_utils.erl | 14 -------------- .../src/exproto/emqx_exproto_impl.erl | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_utils.erl b/apps/emqx_gateway/src/emqx_gateway_utils.erl index 95720ff13..cc771ed95 100644 --- a/apps/emqx_gateway/src/emqx_gateway_utils.erl +++ b/apps/emqx_gateway/src/emqx_gateway_utils.erl @@ -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() diff --git a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl index 48dca4324..75057534f 100644 --- a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl +++ b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl @@ -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), From 990514a048933c1ba31507e01e8f685075d1dcec Mon Sep 17 00:00:00 2001 From: JianBo He Date: Thu, 30 Dec 2021 18:29:19 +0800 Subject: [PATCH 2/4] fix(gw): stop xml_object_db process synchronously --- apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl index 47ed722b1..f4d73879d 100644 --- a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl +++ b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl @@ -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). From 2411c22b422c05f8c050c7c8ba972d7424673549 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Thu, 30 Dec 2021 19:00:28 +0800 Subject: [PATCH 3/4] fix(gw): fix bad listners field on http response --- apps/emqx_gateway/src/emqx_gateway_conf.erl | 2 +- apps/emqx_gateway/test/emqx_exproto_SUITE.erl | 2 +- apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_conf.erl b/apps/emqx_gateway/src/emqx_gateway_conf.erl index cd1f64871..d2f6921e3 100644 --- a/apps/emqx_gateway/src/emqx_gateway_conf.erl +++ b/apps/emqx_gateway/src/emqx_gateway_conf.erl @@ -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}}) -> diff --git a/apps/emqx_gateway/test/emqx_exproto_SUITE.erl b/apps/emqx_gateway/test/emqx_exproto_SUITE.erl index 993ed4975..69e9afbaf 100644 --- a/apps/emqx_gateway/test/emqx_exproto_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_exproto_SUITE.erl @@ -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) -> diff --git a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl index f91347a6e..9b303c100 100644 --- a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl @@ -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">>} ] From 231aeb6068f391da32f6255810e1e8841f11cfb1 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 31 Dec 2021 16:41:35 +0800 Subject: [PATCH 4/4] fix(statsd): fix dialyzer warnings --- apps/emqx_statsd/src/emqx_statsd_api.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_statsd/src/emqx_statsd_api.erl b/apps/emqx_statsd/src/emqx_statsd_api.erl index 2278fa492..b267a9412 100644 --- a/apps/emqx_statsd/src/emqx_statsd_api.erl +++ b/apps/emqx_statsd/src/emqx_statsd_api.erl @@ -59,7 +59,7 @@ statsd(put, #{body := Body}) -> Body, #{rawconf_with_defaults => true, override_to => cluster}) of {ok, #{raw_config := NewConfig, config := Config}} -> - case maps:get(<<"enable">>, Body) of + _ = case maps:get(<<"enable">>, Body) of true -> _ = emqx_statsd_sup:stop_child(?APP), emqx_statsd_sup:start_child(?APP, maps:get(config, Config));