Skip to content

Commit

Permalink
Merge pull request #3598 from esl/module-opts-map-caps
Browse files Browse the repository at this point in the history
mod_caps changing config from list to map
  • Loading branch information
arcusfelis authored Mar 21, 2022
2 parents 0aa6cea + 135066e commit b260438
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion big_tests/tests/disco_and_caps_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ user_can_query_server_info(Config) ->
%% Helpers

required_modules(disco_with_caps) ->
[{mod_caps, []},
[{mod_caps, config_parser_helper:default_mod_config(mod_caps)},
{mod_disco, default_mod_config(mod_disco)}];
required_modules(disco_with_extra_features) ->
[{mod_disco, mod_config(mod_disco, extra_disco_opts())}].
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/gdpr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ pubsub_required_modules(Plugins) ->
host => HostPattern,
nodetree => nodetree_tree,
plugins => Plugins}),
[{mod_caps, []}, {mod_pubsub, PubsubConfig}].
[{mod_caps, config_parser_helper:default_mod_config(mod_caps)}, {mod_pubsub, PubsubConfig}].

is_mim2_started() ->
#{node := Node} = distributed_helper:mim2(),
Expand Down
4 changes: 2 additions & 2 deletions big_tests/tests/pep_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ unsubscribe_after_presence_unsubscription(Config) ->
%%-----------------------------------------------------------------

required_modules() ->
[{mod_caps, []},
[{mod_caps, config_parser_helper:default_mod_config(mod_caps)},
{mod_pubsub, mod_config(mod_pubsub, #{plugins => [<<"dag">>, <<"pep">>],
nodetree => nodetree_dag,
backend => mongoose_helper:mnesia_or_rdbms_backend(),
pep_mapping => #{},
host => subhost_pattern("pubsub.@HOST@")})}].
required_modules(cache_tests) ->
[{mod_caps, []},
[{mod_caps, config_parser_helper:default_mod_config(mod_caps)},
{mod_pubsub, mod_config(mod_pubsub, #{plugins => [<<"dag">>, <<"pep">>],
nodetree => nodetree_dag,
backend => mongoose_helper:mnesia_or_rdbms_backend(),
Expand Down
19 changes: 10 additions & 9 deletions src/mod_caps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ stop(HostType) ->
-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"cache_size">> => #option{type = integer,
validate = positive},
<<"cache_life_time">> => #option{type = integer,
validate = positive}
}
}.
items = #{<<"cache_size">> => #option{type = integer,
validate = positive},
<<"cache_life_time">> => #option{type = integer,
validate = positive}
},
defaults = #{<<"cache_size">> => 1000,
<<"cache_life_time">> => timer:hours(24) div 1000},
format_items = map
}.

supported_features() -> [dynamic_domains].

Expand Down Expand Up @@ -352,10 +355,8 @@ init_db(mnesia) ->
disc_only_copies).

-spec init(list()) -> {ok, state()}.
init([HostType, Opts]) ->
init([HostType, #{cache_size := MaxSize, cache_life_time := LifeTime}]) ->
init_db(db_type(HostType)),
MaxSize = gen_mod:get_opt(cache_size, Opts, 1000),
LifeTime = gen_mod:get_opt(cache_life_time, Opts, timer:hours(24) div 1000),
cache_tab:new(caps_features, [{max_size, MaxSize}, {life_time, LifeTime}]),
ejabberd_hooks:add(hooks(HostType)),
{ok, #state{host_type = HostType}}.
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ start_link(Host, Opts) ->
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).

deps(_Host, _Opts) ->
[{mod_caps, [], optional}].
[{mod_caps, #{cache_size => 1000, cache_life_time => timer:hours(24) div 1000}, optional}].

start(Host, Opts) ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Expand Down
5 changes: 4 additions & 1 deletion test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ all_modules() ->
#{enabled => false}),
host => {fqdn, <<"muc.example.com">>},
no_stanzaid_element => true}),
mod_caps => [{cache_life_time, 86}, {cache_size, 1000}],
mod_caps => default_mod_config(mod_caps),
mod_mam_cache_user => (default_config([modules, mod_mam_meta, cache]))#{muc => true, pm => true},
mod_offline =>
mod_config(mod_offline, #{backend => riak,
Expand Down Expand Up @@ -854,6 +854,9 @@ default_mod_config(mod_bosh) ->
server_acks => false, max_pause => 120};
default_mod_config(mod_cache_users) ->
#{strategy => fifo, time_to_live => 480, number_of_segments => 3};
default_mod_config(mod_caps) ->
#{cache_size => 1000,
cache_life_time => timer:hours(24) div 1000};
default_mod_config(mod_disco) ->
#{extra_domains => [], server_info => [],
users_can_see_hidden_services => true, iqdisc => one_queue};
Expand Down
7 changes: 4 additions & 3 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1629,10 +1629,11 @@ mod_bosh(_Config) ->
?errh(T(<<"maxpause">>, 0)).

mod_caps(_Config) ->
check_module_defaults(mod_caps),
T = fun(K, V) -> #{<<"modules">> => #{<<"mod_caps">> => #{K => V}}} end,
M = fun(K, V) -> modopts(mod_caps, [{K, V}]) end,
?cfgh(M(cache_size, 10), T(<<"cache_size">>, 10)),
?cfgh(M(cache_life_time, 10), T(<<"cache_life_time">>, 10)),
P = [modules, mod_caps],
?cfgh(P ++ [cache_size], 10, T(<<"cache_size">>, 10)),
?cfgh(P ++ [cache_life_time], 10, T(<<"cache_life_time">>, 10)),
?errh(T(<<"cache_size">>, 0)),
?errh(T(<<"cache_size">>, <<"infinity">>)),
?errh(T(<<"cache_life_time">>, 0)),
Expand Down

0 comments on commit b260438

Please sign in to comment.