fix: start connector-mqtt failed when username/password not provided

This commit is contained in:
Shawn 2022-08-31 09:18:10 +08:00
parent 73e19d84ee
commit ca52b8eb29
3 changed files with 40 additions and 29 deletions

View File

@ -224,20 +224,20 @@ make_forward_confs(undefined) ->
make_forward_confs(FrowardConf) -> make_forward_confs(FrowardConf) ->
FrowardConf. FrowardConf.
basic_config(#{ basic_config(
server := Server,
reconnect_interval := ReconnIntv,
proto_ver := ProtoVer,
bridge_mode := BridgeMode,
username := User,
password := Password,
clean_start := CleanStart,
keepalive := KeepAlive,
retry_interval := RetryIntv,
max_inflight := MaxInflight,
ssl := #{enable := EnableSsl} = Ssl
}) ->
#{ #{
server := Server,
reconnect_interval := ReconnIntv,
proto_ver := ProtoVer,
bridge_mode := BridgeMode,
clean_start := CleanStart,
keepalive := KeepAlive,
retry_interval := RetryIntv,
max_inflight := MaxInflight,
ssl := #{enable := EnableSsl} = Ssl
} = Conf
) ->
BaiscConf = #{
%% connection opts %% connection opts
server => Server, server => Server,
%% 30s %% 30s
@ -251,8 +251,6 @@ basic_config(#{
%% non-standard mqtt connection packets will be filtered out by LB. %% non-standard mqtt connection packets will be filtered out by LB.
%% So let's disable bridge_mode. %% So let's disable bridge_mode.
bridge_mode => BridgeMode, bridge_mode => BridgeMode,
username => User,
password => Password,
clean_start => CleanStart, clean_start => CleanStart,
keepalive => ms_to_s(KeepAlive), keepalive => ms_to_s(KeepAlive),
retry_interval => RetryIntv, retry_interval => RetryIntv,
@ -260,7 +258,20 @@ basic_config(#{
ssl => EnableSsl, ssl => EnableSsl,
ssl_opts => maps:to_list(maps:remove(enable, Ssl)), ssl_opts => maps:to_list(maps:remove(enable, Ssl)),
if_record_metrics => true if_record_metrics => true
}. },
maybe_put_fields([username, password], Conf, BaiscConf).
maybe_put_fields(Fields, Conf, Acc0) ->
lists:foldl(
fun(Key, Acc) ->
case maps:find(Key, Conf) of
error -> Acc;
{ok, Val} -> Acc#{Key => Val}
end
end,
Acc0,
Fields
).
ms_to_s(Ms) -> ms_to_s(Ms) ->
erlang:ceil(Ms / 1000). erlang:ceil(Ms / 1000).

View File

@ -88,18 +88,18 @@
%% Logical operators %% Logical operators
-define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')). -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
-define(RAISE(_EXP_, _ERROR_), -define(RAISE(EXP, ERROR),
?RAISE(_EXP_, _ = do_nothing, _ERROR_) ?RAISE(EXP, _ = do_nothing, ERROR)
). ).
-define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_), -define(RAISE(EXP, EXP_ON_FAIL, ERROR),
fun() -> fun() ->
try try
(_EXP_) (EXP)
catch catch
_EXCLASS_:_EXCPTION_:_ST_ -> EXCLASS:EXCPTION:ST ->
_EXP_ON_FAIL_, EXP_ON_FAIL,
throw(_ERROR_) throw(ERROR)
end end
end() end()
). ).

View File

@ -130,13 +130,13 @@ do_apply_rule(
) -> ) ->
{Selected, Collection} = ?RAISE( {Selected, Collection} = ?RAISE(
select_and_collect(Fields, Columns), select_and_collect(Fields, Columns),
{select_and_collect_error, {_EXCLASS_, _EXCPTION_, _ST_}} {select_and_collect_error, {EXCLASS, EXCPTION, ST}}
), ),
ColumnsAndSelected = maps:merge(Columns, Selected), ColumnsAndSelected = maps:merge(Columns, Selected),
case case
?RAISE( ?RAISE(
match_conditions(Conditions, ColumnsAndSelected), match_conditions(Conditions, ColumnsAndSelected),
{match_conditions_error, {_EXCLASS_, _EXCPTION_, _ST_}} {match_conditions_error, {EXCLASS, EXCPTION, ST}}
) )
of of
true -> true ->
@ -166,12 +166,12 @@ do_apply_rule(
) -> ) ->
Selected = ?RAISE( Selected = ?RAISE(
select_and_transform(Fields, Columns), select_and_transform(Fields, Columns),
{select_and_transform_error, {_EXCLASS_, _EXCPTION_, _ST_}} {select_and_transform_error, {EXCLASS, EXCPTION, ST}}
), ),
case case
?RAISE( ?RAISE(
match_conditions(Conditions, maps:merge(Columns, Selected)), match_conditions(Conditions, maps:merge(Columns, Selected)),
{match_conditions_error, {_EXCLASS_, _EXCPTION_, _ST_}} {match_conditions_error, {EXCLASS, EXCPTION, ST}}
) )
of of
true -> true ->
@ -245,7 +245,7 @@ filter_collection(Columns, InCase, DoEach, {CollKey, CollVal}) ->
case case
?RAISE( ?RAISE(
match_conditions(InCase, ColumnsAndItem), match_conditions(InCase, ColumnsAndItem),
{match_incase_error, {_EXCLASS_, _EXCPTION_, _ST_}} {match_incase_error, {EXCLASS, EXCPTION, ST}}
) )
of of
true when DoEach == [] -> {true, ColumnsAndItem}; true when DoEach == [] -> {true, ColumnsAndItem};
@ -253,7 +253,7 @@ filter_collection(Columns, InCase, DoEach, {CollKey, CollVal}) ->
{true, {true,
?RAISE( ?RAISE(
select_and_transform(DoEach, ColumnsAndItem), select_and_transform(DoEach, ColumnsAndItem),
{doeach_error, {_EXCLASS_, _EXCPTION_, _ST_}} {doeach_error, {EXCLASS, EXCPTION, ST}}
)}; )};
false -> false ->
false false