Auto subscribe format (#5740)

* fix: auto sub list format with default
This commit is contained in:
DDDHuang 2021-09-15 11:40:05 +08:00 committed by GitHub
parent c4403e886d
commit 237d3c0543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 24 deletions

View File

@ -1,28 +1,28 @@
auto_subscribe {
topics = [
# {
# topic = "/c/${clientid}",
# qos = 0
# rh = 0
# rap = 0
# nl = 0
# }
# {
# topic = "/u/${username}",
# },
# {
# topic = "/h/${host}",
# qos = 2
# },
# {
# topic = "/p/${port}",
# },
# {
# topic = "/topic/abc",
# },
# {
# topic = "/client/${clientid}/username/${username}/host/${host}/port/${port}",
# }
## {
## topic = "/c/${clientid}"
## qos = 0
## rh = 0
## rap = 0
## nl = 0
## },
## {
## topic = "/u/${username}"
## },
## {
## topic = "/h/${host}"
## qos = 2
## },
## {
## topic = "/p/${port}"
## },
## {
## topic = "/topic/abc"
## },
## {
## topic = "/client/${clientid}/username/${username}/host/${host}/port/${port}"
## }
]
}

View File

@ -38,7 +38,7 @@ max_limit() ->
?MAX_AUTO_SUBSCRIBE.
list() ->
emqx:get_config([auto_subscribe, topics], []).
format(emqx:get_config([auto_subscribe, topics], [])).
update(Topics) ->
update_(Topics).
@ -68,6 +68,17 @@ on_client_connected(_, _, _) ->
%%--------------------------------------------------------------------
%% internal
format(Rules) when is_list(Rules) ->
[format(Rule) || Rule <- Rules];
format(Rule = #{topic := Topic}) when is_map(Rule) ->
#{
topic => Topic,
qos => maps:get(qos, Rule, 0),
rh => maps:get(rh, Rule, 0),
rap => maps:get(rap, Rule, 0),
nl => maps:get(nl, Rule, 0)
}.
update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
{ok, _} = emqx:update_config([auto_subscribe, topics], Topics),
update_hook();