mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-26 17:38:45 +01:00
Rely on use_cache/1 callback in mod_caps
This commit is contained in:
parent
672c2f75d3
commit
6c1a1bd000
@ -66,6 +66,9 @@
|
|||||||
{ok, non_neg_integer() | [binary()]} | error.
|
{ok, non_neg_integer() | [binary()]} | error.
|
||||||
-callback caps_write(binary(), {binary(), binary()},
|
-callback caps_write(binary(), {binary(), binary()},
|
||||||
non_neg_integer() | [binary()]) -> any().
|
non_neg_integer() | [binary()]) -> any().
|
||||||
|
-callback use_cache(binary()) -> boolean().
|
||||||
|
|
||||||
|
-optional_callbacks([use_cache/1]).
|
||||||
|
|
||||||
start(Host, Opts) ->
|
start(Host, Opts) ->
|
||||||
gen_mod:start_child(?MODULE, Host, Opts).
|
gen_mod:start_child(?MODULE, Host, Opts).
|
||||||
@ -78,17 +81,23 @@ get_features(_Host, nothing) -> [];
|
|||||||
get_features(Host, #caps{node = Node, version = Version,
|
get_features(Host, #caps{node = Node, version = Version,
|
||||||
exts = Exts}) ->
|
exts = Exts}) ->
|
||||||
SubNodes = [Version | Exts],
|
SubNodes = [Version | Exts],
|
||||||
lists:foldl(fun (SubNode, Acc) ->
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
||||||
|
lists:foldl(
|
||||||
|
fun(SubNode, Acc) ->
|
||||||
NodePair = {Node, SubNode},
|
NodePair = {Node, SubNode},
|
||||||
case ets_cache:lookup(caps_features_cache, NodePair,
|
Res = case use_cache(Mod, Host) of
|
||||||
caps_read_fun(Host, NodePair))
|
true ->
|
||||||
of
|
ets_cache:lookup(caps_features_cache, NodePair,
|
||||||
|
caps_read_fun(Host, NodePair));
|
||||||
|
false ->
|
||||||
|
Mod:caps_read(Host, NodePair)
|
||||||
|
end,
|
||||||
|
case Res of
|
||||||
{ok, Features} when is_list(Features) ->
|
{ok, Features} when is_list(Features) ->
|
||||||
Features ++ Acc;
|
Features ++ Acc;
|
||||||
_ -> Acc
|
_ -> Acc
|
||||||
end
|
end
|
||||||
end,
|
end, [], SubNodes).
|
||||||
[], SubNodes).
|
|
||||||
|
|
||||||
-spec list_features(ejabberd_c2s:state()) -> [{ljid(), caps()}].
|
-spec list_features(ejabberd_c2s:state()) -> [{ljid(), caps()}].
|
||||||
list_features(C2SState) ->
|
list_features(C2SState) ->
|
||||||
@ -250,28 +259,12 @@ reload(Host, NewOpts, OldOpts) ->
|
|||||||
true ->
|
true ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
case gen_mod:is_equal_opt(cache_size, NewOpts, OldOpts) of
|
init_cache(NewMod, Host, NewOpts).
|
||||||
{false, MaxSize, _} ->
|
|
||||||
ets_cache:setopts(caps_features_cache, [{max_size, MaxSize}]),
|
|
||||||
ets_cache:setopts(caps_requests_cache, [{max_size, MaxSize}]);
|
|
||||||
true ->
|
|
||||||
ok
|
|
||||||
end,
|
|
||||||
case gen_mod:is_equal_opt(cache_life_time, NewOpts, OldOpts) of
|
|
||||||
{false, Time, _} ->
|
|
||||||
LifeTime = case Time of
|
|
||||||
infinity -> infinity;
|
|
||||||
_ -> timer:seconds(Time)
|
|
||||||
end,
|
|
||||||
ets_cache:setopts(caps_features_cache, [{life_time, LifeTime}]);
|
|
||||||
true ->
|
|
||||||
ok
|
|
||||||
end.
|
|
||||||
|
|
||||||
init([Host, Opts]) ->
|
init([Host, Opts]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
||||||
init_cache(Opts),
|
init_cache(Mod, Host, Opts),
|
||||||
Mod:init(Host, Opts),
|
Mod:init(Host, Opts),
|
||||||
ejabberd_hooks:add(c2s_presence_in, Host, ?MODULE,
|
ejabberd_hooks:add(c2s_presence_in, Host, ?MODULE,
|
||||||
c2s_presence_in, 75),
|
c2s_presence_in, 75),
|
||||||
@ -332,8 +325,15 @@ feature_request(Host, From, To, Caps,
|
|||||||
[SubNode | Tail] = SubNodes) ->
|
[SubNode | Tail] = SubNodes) ->
|
||||||
Node = Caps#caps.node,
|
Node = Caps#caps.node,
|
||||||
NodePair = {Node, SubNode},
|
NodePair = {Node, SubNode},
|
||||||
case ets_cache:lookup(caps_features_cache, NodePair,
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
||||||
caps_read_fun(Host, NodePair)) of
|
Res = case use_cache(Mod, Host) of
|
||||||
|
true ->
|
||||||
|
ets_cache:lookup(caps_features_cache, NodePair,
|
||||||
|
caps_read_fun(Host, NodePair));
|
||||||
|
false ->
|
||||||
|
Mod:caps_read(Host, NodePair)
|
||||||
|
end,
|
||||||
|
case Res of
|
||||||
{ok, Fs} when is_list(Fs) ->
|
{ok, Fs} when is_list(Fs) ->
|
||||||
feature_request(Host, From, To, Caps, Tail);
|
feature_request(Host, From, To, Caps, Tail);
|
||||||
_ ->
|
_ ->
|
||||||
@ -368,7 +368,12 @@ feature_response(#iq{type = result, sub_els = [El]},
|
|||||||
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
||||||
case Mod:caps_write(LServer, NodePair, Features) of
|
case Mod:caps_write(LServer, NodePair, Features) of
|
||||||
ok ->
|
ok ->
|
||||||
|
case use_cache(Mod, LServer) of
|
||||||
|
true ->
|
||||||
ets_cache:delete(caps_features_cache, NodePair);
|
ets_cache:delete(caps_features_cache, NodePair);
|
||||||
|
false ->
|
||||||
|
ok
|
||||||
|
end;
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
ok
|
ok
|
||||||
end;
|
end;
|
||||||
@ -476,21 +481,24 @@ is_valid_node(Node) ->
|
|||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
init_cache(Opts) ->
|
init_cache(Mod, Host, Opts) ->
|
||||||
CacheOpts = cache_opts(Opts),
|
CacheOpts = cache_opts(Opts),
|
||||||
case use_cache(Opts) of
|
case use_cache(Mod, Host) of
|
||||||
true ->
|
true ->
|
||||||
ets_cache:new(caps_features_cache, CacheOpts);
|
ets_cache:new(caps_features_cache, CacheOpts);
|
||||||
false ->
|
false ->
|
||||||
ok
|
ets_cache:delete(caps_features_cache)
|
||||||
end,
|
end,
|
||||||
CacheSize = proplists:get_value(max_size, CacheOpts),
|
CacheSize = proplists:get_value(max_size, CacheOpts),
|
||||||
ets_cache:new(caps_requests_cache,
|
ets_cache:new(caps_requests_cache,
|
||||||
[{max_size, CacheSize},
|
[{max_size, CacheSize},
|
||||||
{life_time, timer:seconds(?BAD_HASH_LIFETIME)}]).
|
{life_time, timer:seconds(?BAD_HASH_LIFETIME)}]).
|
||||||
|
|
||||||
use_cache(Opts) ->
|
use_cache(Mod, Host) ->
|
||||||
gen_mod:get_opt(use_cache, Opts).
|
case erlang:function_exported(Mod, use_cache, 1) of
|
||||||
|
true -> Mod:use_cache(Host);
|
||||||
|
false -> gen_mod:get_module_opt(Host, ?MODULE, use_cache)
|
||||||
|
end.
|
||||||
|
|
||||||
cache_opts(Opts) ->
|
cache_opts(Opts) ->
|
||||||
MaxSize = gen_mod:get_opt(cache_size, Opts),
|
MaxSize = gen_mod:get_opt(cache_size, Opts),
|
||||||
|
Loading…
Reference in New Issue
Block a user