mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-22 17:28:25 +01:00
Use new cache API in mod_shared_roster_ldap
This commit is contained in:
parent
9937cb48fd
commit
d88c08e074
@ -33,7 +33,7 @@
|
|||||||
-behaviour(gen_mod).
|
-behaviour(gen_mod).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start/2, stop/1]).
|
-export([start/2, stop/1, reload/3]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2,
|
-export([init/1, handle_call/3, handle_cast/2,
|
||||||
@ -41,7 +41,8 @@
|
|||||||
|
|
||||||
-export([get_user_roster/2, c2s_session_opened/1,
|
-export([get_user_roster/2, c2s_session_opened/1,
|
||||||
get_jid_info/4, process_item/2, in_subscription/6,
|
get_jid_info/4, process_item/2, in_subscription/6,
|
||||||
out_subscription/4, mod_opt_type/1, opt_type/1, depends/2]).
|
out_subscription/4, mod_opt_type/1, opt_type/1, depends/2,
|
||||||
|
transform_module_options/1]).
|
||||||
|
|
||||||
-include("ejabberd.hrl").
|
-include("ejabberd.hrl").
|
||||||
-include("logger.hrl").
|
-include("logger.hrl").
|
||||||
@ -50,9 +51,8 @@
|
|||||||
-include("eldap.hrl").
|
-include("eldap.hrl").
|
||||||
|
|
||||||
-define(SETS, gb_sets).
|
-define(SETS, gb_sets).
|
||||||
-define(CACHE_SIZE, 1000).
|
-define(USER_CACHE, shared_roster_ldap_user_cache).
|
||||||
-define(USER_CACHE_VALIDITY, 300). %% in seconds
|
-define(GROUP_CACHE, shared_roster_ldap_group_cache).
|
||||||
-define(GROUP_CACHE_VALIDITY, 300).
|
|
||||||
-define(LDAP_SEARCH_TIMEOUT, 5). %% Timeout for LDAP search queries in seconds
|
-define(LDAP_SEARCH_TIMEOUT, 5). %% Timeout for LDAP search queries in seconds
|
||||||
-define(INVALID_SETTING_MSG, "~s is not properly set! ~s will not function.").
|
-define(INVALID_SETTING_MSG, "~s is not properly set! ~s will not function.").
|
||||||
|
|
||||||
@ -79,11 +79,7 @@
|
|||||||
ufilter = <<"">> :: binary(),
|
ufilter = <<"">> :: binary(),
|
||||||
rfilter = <<"">> :: binary(),
|
rfilter = <<"">> :: binary(),
|
||||||
gfilter = <<"">> :: binary(),
|
gfilter = <<"">> :: binary(),
|
||||||
auth_check = true :: boolean(),
|
auth_check = true :: boolean()}).
|
||||||
user_cache_size = ?CACHE_SIZE :: non_neg_integer(),
|
|
||||||
group_cache_size = ?CACHE_SIZE :: non_neg_integer(),
|
|
||||||
user_cache_validity = ?USER_CACHE_VALIDITY :: non_neg_integer(),
|
|
||||||
group_cache_validity = ?GROUP_CACHE_VALIDITY :: non_neg_integer()}).
|
|
||||||
|
|
||||||
-record(group_info, {desc, members}).
|
-record(group_info, {desc, members}).
|
||||||
|
|
||||||
@ -96,6 +92,17 @@ start(Host, Opts) ->
|
|||||||
stop(Host) ->
|
stop(Host) ->
|
||||||
gen_mod:stop_child(?MODULE, Host).
|
gen_mod:stop_child(?MODULE, Host).
|
||||||
|
|
||||||
|
reload(Host, NewOpts, _OldOpts) ->
|
||||||
|
case init_cache(Host, NewOpts) of
|
||||||
|
true ->
|
||||||
|
ets_cache:setopts(?USER_CACHE, cache_opts(Host, NewOpts)),
|
||||||
|
ets_cache:setopts(?GROUP_CACHE, cache_opts(Host, NewOpts));
|
||||||
|
false ->
|
||||||
|
ok
|
||||||
|
end,
|
||||||
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
||||||
|
gen_server:cast(Proc, {set_state, parse_options(Host, NewOpts)}).
|
||||||
|
|
||||||
depends(_Host, _Opts) ->
|
depends(_Host, _Opts) ->
|
||||||
[{mod_roster, hard}].
|
[{mod_roster, hard}].
|
||||||
|
|
||||||
@ -231,12 +238,7 @@ process_subscription(Direction, User, Server, JID,
|
|||||||
init([Host, Opts]) ->
|
init([Host, Opts]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
State = parse_options(Host, Opts),
|
State = parse_options(Host, Opts),
|
||||||
cache_tab:new(shared_roster_ldap_user,
|
init_cache(Host, Opts),
|
||||||
[{max_size, State#state.user_cache_size}, {lru, false},
|
|
||||||
{life_time, State#state.user_cache_validity}]),
|
|
||||||
cache_tab:new(shared_roster_ldap_group,
|
|
||||||
[{max_size, State#state.group_cache_size}, {lru, false},
|
|
||||||
{life_time, State#state.group_cache_validity}]),
|
|
||||||
ejabberd_hooks:add(roster_get, Host, ?MODULE,
|
ejabberd_hooks:add(roster_get, Host, ?MODULE,
|
||||||
get_user_roster, 70),
|
get_user_roster, 70),
|
||||||
ejabberd_hooks:add(roster_in_subscription, Host,
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
||||||
@ -260,6 +262,8 @@ handle_call(get_state, _From, State) ->
|
|||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
{reply, {error, badarg}, State}.
|
{reply, {error, badarg}, State}.
|
||||||
|
|
||||||
|
handle_cast({set_state, NewState}, _State) ->
|
||||||
|
{noreply, NewState};
|
||||||
handle_cast(_Msg, State) -> {noreply, State}.
|
handle_cast(_Msg, State) -> {noreply, State}.
|
||||||
|
|
||||||
handle_info(_Info, State) -> {noreply, State}.
|
handle_info(_Info, State) -> {noreply, State}.
|
||||||
@ -341,7 +345,7 @@ get_user_displayed_groups({User, Host}) ->
|
|||||||
|
|
||||||
get_group_users(Host, Group) ->
|
get_group_users(Host, Group) ->
|
||||||
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
||||||
case cache_tab:dirty_lookup(shared_roster_ldap_group,
|
case ets_cache:lookup(?GROUP_CACHE,
|
||||||
{Group, Host},
|
{Group, Host},
|
||||||
fun () -> search_group_info(State, Group) end) of
|
fun () -> search_group_info(State, Group) end) of
|
||||||
{ok, #group_info{members = Members}}
|
{ok, #group_info{members = Members}}
|
||||||
@ -352,7 +356,7 @@ get_group_users(Host, Group) ->
|
|||||||
|
|
||||||
get_group_name(Host, Group) ->
|
get_group_name(Host, Group) ->
|
||||||
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
||||||
case cache_tab:dirty_lookup(shared_roster_ldap_group,
|
case ets_cache:lookup(?GROUP_CACHE,
|
||||||
{Group, Host},
|
{Group, Host},
|
||||||
fun () -> search_group_info(State, Group) end)
|
fun () -> search_group_info(State, Group) end)
|
||||||
of
|
of
|
||||||
@ -364,7 +368,7 @@ get_group_name(Host, Group) ->
|
|||||||
|
|
||||||
get_user_name(User, Host) ->
|
get_user_name(User, Host) ->
|
||||||
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
||||||
case cache_tab:dirty_lookup(shared_roster_ldap_user,
|
case ets_cache:lookup(?USER_CACHE,
|
||||||
{User, Host},
|
{User, Host},
|
||||||
fun () -> search_user_name(State, User) end)
|
fun () -> search_user_name(State, User) end)
|
||||||
of
|
of
|
||||||
@ -494,22 +498,6 @@ parse_options(Host, Opts) ->
|
|||||||
(false) -> false;
|
(false) -> false;
|
||||||
(true) -> true
|
(true) -> true
|
||||||
end, true),
|
end, true),
|
||||||
UserCacheValidity = gen_mod:get_opt(
|
|
||||||
{ldap_user_cache_validity, Host}, Opts,
|
|
||||||
fun(I) when is_integer(I), I>0 -> I end,
|
|
||||||
?USER_CACHE_VALIDITY),
|
|
||||||
GroupCacheValidity = gen_mod:get_opt(
|
|
||||||
{ldap_group_cache_validity, Host}, Opts,
|
|
||||||
fun(I) when is_integer(I), I>0 -> I end,
|
|
||||||
?GROUP_CACHE_VALIDITY),
|
|
||||||
UserCacheSize = gen_mod:get_opt(
|
|
||||||
{ldap_user_cache_size, Host}, Opts,
|
|
||||||
fun(I) when is_integer(I), I>0 -> I end,
|
|
||||||
?CACHE_SIZE),
|
|
||||||
GroupCacheSize = gen_mod:get_opt(
|
|
||||||
{ldap_group_cache_size, Host}, Opts,
|
|
||||||
fun(I) when is_integer(I), I>0 -> I end,
|
|
||||||
?CACHE_SIZE),
|
|
||||||
ConfigFilter = gen_mod:get_opt({ldap_filter, Host}, Opts,
|
ConfigFilter = gen_mod:get_opt({ldap_filter, Host}, Opts,
|
||||||
fun check_filter/1, <<"">>),
|
fun check_filter/1, <<"">>),
|
||||||
ConfigUserFilter = gen_mod:get_opt({ldap_ufilter, Host}, Opts,
|
ConfigUserFilter = gen_mod:get_opt({ldap_ufilter, Host}, Opts,
|
||||||
@ -562,17 +550,67 @@ parse_options(Host, Opts) ->
|
|||||||
uid_format = UIDAttrFormat,
|
uid_format = UIDAttrFormat,
|
||||||
uid_format_re = UIDAttrFormatRe, filter = Filter,
|
uid_format_re = UIDAttrFormatRe, filter = Filter,
|
||||||
ufilter = UserFilter, rfilter = RosterFilter,
|
ufilter = UserFilter, rfilter = RosterFilter,
|
||||||
gfilter = GroupFilter, auth_check = AuthCheck,
|
gfilter = GroupFilter, auth_check = AuthCheck}.
|
||||||
user_cache_size = UserCacheSize,
|
|
||||||
user_cache_validity = UserCacheValidity,
|
|
||||||
group_cache_size = GroupCacheSize,
|
|
||||||
group_cache_validity = GroupCacheValidity}.
|
|
||||||
|
|
||||||
check_filter(F) ->
|
check_filter(F) ->
|
||||||
NewF = iolist_to_binary(F),
|
NewF = iolist_to_binary(F),
|
||||||
{ok, _} = eldap_filter:parse(NewF),
|
{ok, _} = eldap_filter:parse(NewF),
|
||||||
NewF.
|
NewF.
|
||||||
|
|
||||||
|
init_cache(Host, Opts) ->
|
||||||
|
UseCache = use_cache(Host, Opts),
|
||||||
|
case UseCache of
|
||||||
|
true ->
|
||||||
|
CacheOpts = cache_opts(Host, Opts),
|
||||||
|
ets_cache:new(?USER_CACHE, CacheOpts),
|
||||||
|
ets_cache:new(?GROUP_CACHE, CacheOpts);
|
||||||
|
false ->
|
||||||
|
ets_cache:delete(?USER_CACHE),
|
||||||
|
ets_cache:delete(?GROUP_CACHE)
|
||||||
|
end,
|
||||||
|
UseCache.
|
||||||
|
|
||||||
|
use_cache(Host, Opts) ->
|
||||||
|
gen_mod:get_opt(use_cache, Opts, mod_opt_type(use_cache),
|
||||||
|
ejabberd_config:use_cache(Host)).
|
||||||
|
|
||||||
|
cache_opts(Host, Opts) ->
|
||||||
|
MaxSize = gen_mod:get_opt(cache_size, Opts,
|
||||||
|
mod_opt_type(cache_size),
|
||||||
|
ejabberd_config:cache_size(Host)),
|
||||||
|
CacheMissed = gen_mod:get_opt(cache_missed, Opts,
|
||||||
|
mod_opt_type(cache_missed),
|
||||||
|
ejabberd_config:cache_missed(Host)),
|
||||||
|
LifeTime = case gen_mod:get_opt(cache_life_time, Opts,
|
||||||
|
mod_opt_type(cache_life_time),
|
||||||
|
ejabberd_config:cache_life_time(Host)) of
|
||||||
|
infinity -> infinity;
|
||||||
|
I -> timer:seconds(I)
|
||||||
|
end,
|
||||||
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
||||||
|
|
||||||
|
transform_module_options(Opts) ->
|
||||||
|
lists:map(
|
||||||
|
fun({ldap_group_cache_size, I}) ->
|
||||||
|
?WARNING_MSG("Option 'ldap_group_cache_size' is deprecated, "
|
||||||
|
"use 'cache_size' instead", []),
|
||||||
|
{cache_size, I};
|
||||||
|
({ldap_user_cache_size, I}) ->
|
||||||
|
?WARNING_MSG("Option 'ldap_user_cache_size' is deprecated, "
|
||||||
|
"use 'cache_size' instead", []),
|
||||||
|
{cache_size, I};
|
||||||
|
({ldap_group_cache_validity, Secs}) ->
|
||||||
|
?WARNING_MSG("Option 'ldap_group_cache_validity' is deprecated, "
|
||||||
|
"use 'cache_life_time' instead", []),
|
||||||
|
{cache_life_time, Secs};
|
||||||
|
({ldap_user_cache_validity, Secs}) ->
|
||||||
|
?WARNING_MSG("Option 'ldap_user_cache_validity' is deprecated, "
|
||||||
|
"use 'cache_life_time' instead", []),
|
||||||
|
{cache_life_time, Secs};
|
||||||
|
(Opt) ->
|
||||||
|
Opt
|
||||||
|
end, Opts).
|
||||||
|
|
||||||
mod_opt_type(deref_aliases) ->
|
mod_opt_type(deref_aliases) ->
|
||||||
fun (never) -> never;
|
fun (never) -> never;
|
||||||
(searching) -> searching;
|
(searching) -> searching;
|
||||||
@ -618,10 +656,13 @@ mod_opt_type(ldap_auth_check) ->
|
|||||||
end;
|
end;
|
||||||
mod_opt_type(ldap_filter) -> fun check_filter/1;
|
mod_opt_type(ldap_filter) -> fun check_filter/1;
|
||||||
mod_opt_type(ldap_gfilter) -> fun check_filter/1;
|
mod_opt_type(ldap_gfilter) -> fun check_filter/1;
|
||||||
mod_opt_type(ldap_group_cache_size) ->
|
mod_opt_type(O) when O == cache_size;
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
O == cache_life_time ->
|
||||||
mod_opt_type(ldap_group_cache_validity) ->
|
fun (I) when is_integer(I), I > 0 -> I;
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
(infinity) -> infinity
|
||||||
|
end;
|
||||||
|
mod_opt_type(O) when O == use_cache; O == cache_missed ->
|
||||||
|
fun (B) when is_boolean(B) -> B end;
|
||||||
mod_opt_type(ldap_groupattr) -> fun iolist_to_binary/1;
|
mod_opt_type(ldap_groupattr) -> fun iolist_to_binary/1;
|
||||||
mod_opt_type(ldap_groupdesc) -> fun iolist_to_binary/1;
|
mod_opt_type(ldap_groupdesc) -> fun iolist_to_binary/1;
|
||||||
mod_opt_type(ldap_memberattr) -> fun iolist_to_binary/1;
|
mod_opt_type(ldap_memberattr) -> fun iolist_to_binary/1;
|
||||||
@ -633,38 +674,22 @@ mod_opt_type(ldap_memberattr_format_re) ->
|
|||||||
end;
|
end;
|
||||||
mod_opt_type(ldap_rfilter) -> fun check_filter/1;
|
mod_opt_type(ldap_rfilter) -> fun check_filter/1;
|
||||||
mod_opt_type(ldap_ufilter) -> fun check_filter/1;
|
mod_opt_type(ldap_ufilter) -> fun check_filter/1;
|
||||||
mod_opt_type(ldap_user_cache_size) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
mod_opt_type(ldap_user_cache_validity) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
mod_opt_type(ldap_userdesc) -> fun iolist_to_binary/1;
|
mod_opt_type(ldap_userdesc) -> fun iolist_to_binary/1;
|
||||||
mod_opt_type(ldap_useruid) -> fun iolist_to_binary/1;
|
mod_opt_type(ldap_useruid) -> fun iolist_to_binary/1;
|
||||||
mod_opt_type(_) ->
|
mod_opt_type(_) ->
|
||||||
[ldap_auth_check, ldap_filter, ldap_gfilter,
|
[ldap_auth_check, ldap_filter, ldap_gfilter,
|
||||||
ldap_group_cache_size, ldap_group_cache_validity,
|
|
||||||
ldap_groupattr, ldap_groupdesc, ldap_memberattr,
|
ldap_groupattr, ldap_groupdesc, ldap_memberattr,
|
||||||
ldap_memberattr_format, ldap_memberattr_format_re,
|
ldap_memberattr_format, ldap_memberattr_format_re,
|
||||||
ldap_rfilter, ldap_ufilter, ldap_user_cache_size,
|
ldap_rfilter, ldap_ufilter, ldap_userdesc, ldap_useruid,
|
||||||
ldap_user_cache_validity, ldap_userdesc, ldap_useruid,
|
|
||||||
deref_aliases, ldap_backups, ldap_base,
|
deref_aliases, ldap_backups, ldap_base,
|
||||||
ldap_deref_aliases, ldap_encrypt, ldap_password,
|
ldap_deref_aliases, ldap_encrypt, ldap_password,
|
||||||
ldap_port, ldap_rootdn, ldap_servers,
|
ldap_port, ldap_rootdn, ldap_servers,
|
||||||
ldap_tls_cacertfile, ldap_tls_certfile, ldap_tls_depth,
|
ldap_tls_cacertfile, ldap_tls_certfile, ldap_tls_depth,
|
||||||
ldap_tls_verify].
|
ldap_tls_verify, use_cache, cache_missed, cache_size, cache_life_time].
|
||||||
|
|
||||||
opt_type(ldap_filter) -> fun check_filter/1;
|
opt_type(ldap_filter) -> fun check_filter/1;
|
||||||
opt_type(ldap_gfilter) -> fun check_filter/1;
|
opt_type(ldap_gfilter) -> fun check_filter/1;
|
||||||
opt_type(ldap_group_cache_size) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
opt_type(ldap_group_cache_validity) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
opt_type(ldap_rfilter) -> fun check_filter/1;
|
opt_type(ldap_rfilter) -> fun check_filter/1;
|
||||||
opt_type(ldap_ufilter) -> fun check_filter/1;
|
opt_type(ldap_ufilter) -> fun check_filter/1;
|
||||||
opt_type(ldap_user_cache_size) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
opt_type(ldap_user_cache_validity) ->
|
|
||||||
fun (I) when is_integer(I), I > 0 -> I end;
|
|
||||||
opt_type(_) ->
|
opt_type(_) ->
|
||||||
[ldap_filter, ldap_gfilter, ldap_group_cache_size,
|
[ldap_filter, ldap_gfilter, ldap_rfilter, ldap_ufilter].
|
||||||
ldap_group_cache_validity, ldap_rfilter, ldap_ufilter,
|
|
||||||
ldap_user_cache_size, ldap_user_cache_validity].
|
|
||||||
|
Loading…
Reference in New Issue
Block a user