Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache found behaviour implementations #2323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rel/files/mongooseim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@
{services,
[
{service_admin_extra, [{submods, [node, accounts, sessions, vcard, gdpr,
roster, last, private, stanza, stats]}]}
roster, last, private, stanza, stats]}]},
{service_cache, []}
]
}.

Expand Down
4 changes: 3 additions & 1 deletion src/admin_extra/service_admin_extra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

-behaviour(mongoose_service).

-export([start/1, stop/0]).
-export([start/1, stop/0, deps/0]).

-define(SUBMODS, [node, accounts, sessions, vcard, roster, last,
private, stanza, stats, gdpr
Expand All @@ -40,6 +40,8 @@
%%% gen_mod
%%%

deps() -> [service_cache].

start(Opts) ->
Submods = gen_mod:get_opt(submods, Opts, ?SUBMODS),
lists:foreach(fun(Submod) ->
Expand Down
5 changes: 5 additions & 0 deletions src/mongoose_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
%% WARNING! For simplicity, this function searches only MongooseIM code dir
-spec find_behaviour_implementations(Behaviour :: module()) -> [module()].
find_behaviour_implementations(Behaviour) ->
LookupFN = fun() -> {ok, find_implementations(Behaviour)} end,
{ok, Modules} = service_cache:lookup({behaviour, Behaviour}, LookupFN),
Modules.

find_implementations(Behaviour) ->
{ok, EbinFiles} = file:list_dir(code:lib_dir(mongooseim, ebin)),
Mods = [ list_to_atom(filename:rootname(File))
|| File <- EbinFiles, filename:extension(File) == ".beam" ],
Expand Down
33 changes: 33 additions & 0 deletions src/service_cache.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%%%-------------------------------------------------------------------
%%% @author [email protected]
%%% @copyright (C) 2019, Erlang Solutions
%%% @doc
%%%
%%% @end
%%% Created : 28. May 2019 17:23
%%%-------------------------------------------------------------------
-module(service_cache).

-behaviour(mongoose_service).

%% API
-export([start/1, stop/0]).
-export([lookup/2]).

-type lookup_result() :: {ok, Value :: term()} | error.
-type lookup_fn() :: fun(()-> lookup_result()).

-export_type([lookup_fn/0, lookup_result/0]).

start(Opts) -> cache_tab:new(?MODULE, Opts).
stop() -> cache_tab:delete(?MODULE).


-spec lookup(Key :: term(), lookup_fn()) -> lookup_result().
lookup(Key, Fun) ->
case mongoose_service:is_loaded(?MODULE) of
true ->
cache_tab:lookup(?MODULE, Key, Fun);
false ->
Fun()
end.
2 changes: 2 additions & 0 deletions test/mam_jid_mini_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ init_per_suite(Config) ->
{skip,nif_not_loaded}
end.

end_per_suite(Config) -> Config.

test_encode_decode_functionality(_Config) ->
PossibleDomainNames = [<<"a">>, <<"b">>, <<"c">>, <<"d">>, <<"e">>, <<"f">>],
PossibleUserNames = [<<"">> | PossibleDomainNames],
Expand Down