-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3493 from esl/cache_muclight_affs
Cache muclight affs
- Loading branch information
Showing
7 changed files
with
225 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
-module(mod_muc_light_cache). | ||
|
||
-behaviour(gen_mod). | ||
-define(FRONTEND, mod_muc_light). | ||
|
||
%% gen_mod callbacks | ||
-export([start/2, stop/1, supported_features/0]). | ||
|
||
%% Hook handlers | ||
-export([pre_acc_room_affiliations/2, post_acc_room_affiliations/2, | ||
pre_room_exists/3, post_room_exists/3, | ||
forget_room/4, remove_domain/3, room_new_affiliations/4]). | ||
-ignore_xref([pre_acc_room_affiliations/2, post_acc_room_affiliations/2, | ||
pre_room_exists/3, post_room_exists/3, | ||
forget_room/4, remove_domain/3, room_new_affiliations/4]). | ||
|
||
%% For tests | ||
-export([force_clear/1]). | ||
|
||
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. | ||
start(HostType, Opts) -> | ||
start_cache(HostType, Opts), | ||
ejabberd_hooks:add(hooks(HostType)), | ||
ok. | ||
|
||
-spec stop(HostType :: mongooseim:host_type()) -> ok. | ||
stop(HostType) -> | ||
ejabberd_hooks:delete(hooks(HostType)), | ||
stop_cache(HostType), | ||
ok. | ||
|
||
-spec supported_features() -> [atom()]. | ||
supported_features() -> | ||
[dynamic_domains]. | ||
|
||
-spec hooks(mongooseim:host_type()) -> [ejabberd_hooks:hook()]. | ||
hooks(HostType) -> | ||
[ | ||
{acc_room_affiliations, HostType, ?MODULE, pre_acc_room_affiliations, 40}, | ||
{acc_room_affiliations, HostType, ?MODULE, post_acc_room_affiliations, 60}, | ||
{room_exists, HostType, ?MODULE, pre_room_exists, 40}, | ||
{room_exists, HostType, ?MODULE, post_room_exists, 60}, | ||
{forget_room, HostType, ?MODULE, forget_room, 80}, | ||
{remove_domain, HostType, ?MODULE, remove_domain, 20}, | ||
{room_new_affiliations, HostType, ?MODULE, room_new_affiliations, 50} | ||
]. | ||
|
||
-spec pre_acc_room_affiliations(mongoose_acc:t(), jid:jid()) -> | ||
mongoose_acc:t() | {stop, mongoose_acc:t()}. | ||
pre_acc_room_affiliations(Acc, RoomJid) -> | ||
case mongoose_acc:get(?FRONTEND, affiliations, {error, not_exists}, Acc) of | ||
{error, _} -> | ||
HostType = mongoose_acc:host_type(Acc), | ||
case mongoose_user_cache:get_entry(HostType, ?MODULE, RoomJid) of | ||
#{affs := Res} -> | ||
mongoose_acc:set(?FRONTEND, affiliations, Res, Acc); | ||
_ -> | ||
Acc | ||
end; | ||
_Res -> | ||
{stop, Acc} | ||
end. | ||
|
||
-spec post_acc_room_affiliations(mongoose_acc:t(), jid:jid()) -> mongoose_acc:t(). | ||
post_acc_room_affiliations(Acc, RoomJid) -> | ||
case mongoose_acc:get(?FRONTEND, affiliations, {error, not_exists}, Acc) of | ||
{error, _} -> | ||
Acc; | ||
Res -> | ||
HostType = mongoose_acc:host_type(Acc), | ||
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{affs => Res}), | ||
Acc | ||
end. | ||
|
||
-spec pre_room_exists(boolean(), mongooseim:host_type(), jid:jid()) -> | ||
boolean() | {stop, true}. | ||
pre_room_exists(false, HostType, RoomJid) -> | ||
case mongoose_user_cache:is_member(HostType, ?MODULE, RoomJid) of | ||
true -> {stop, true}; | ||
false -> false | ||
end; | ||
pre_room_exists(Status, _, _) -> | ||
Status. | ||
|
||
-spec post_room_exists(boolean(), mongooseim:host_type(), jid:jid()) -> | ||
boolean(). | ||
post_room_exists(true, HostType, RoomJid) -> | ||
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{}), | ||
true; | ||
post_room_exists(Status, _, _) -> | ||
Status. | ||
|
||
-spec forget_room(mongoose_hooks:simple_acc(), mongooseim:host_type(), jid:lserver(), binary()) -> | ||
mongoose_hooks:simple_acc(). | ||
forget_room(Acc, HostType, RoomS, RoomU) -> | ||
mongoose_user_cache:delete_user(HostType, ?MODULE, jid:make_noprep(RoomU, RoomS, <<>>)), | ||
Acc. | ||
|
||
-spec remove_domain(mongoose_hooks:simple_acc(), mongooseim:host_type(), jid:lserver()) -> | ||
mongoose_hooks:simple_acc(). | ||
remove_domain(Acc, HostType, Domain) -> | ||
MUCHost = mod_muc_light:server_host_to_muc_host(HostType, Domain), | ||
mongoose_user_cache:delete_domain(HostType, ?MODULE, MUCHost), | ||
Acc. | ||
|
||
-spec room_new_affiliations(mongoose_acc:t(), jid:jid(), mod_muc_light:aff_users(), binary()) -> | ||
mongoose_acc:t(). | ||
room_new_affiliations(Acc, RoomJid, NewAffs, NewVersion) -> | ||
HostType = mod_muc_light_utils:acc_to_host_type(Acc), | ||
% make sure other nodes forget about stale values | ||
mongoose_user_cache:delete_user(HostType, ?MODULE, RoomJid), | ||
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{affs => {ok, NewAffs, NewVersion}}), | ||
Acc. | ||
|
||
-spec force_clear(mongooseim:host_type()) -> ok. | ||
force_clear(HostType) -> | ||
CacheName = gen_mod:get_module_proc(HostType, ?MODULE), | ||
segmented_cache:delete_pattern(CacheName, {'_', '_'}), | ||
ok. | ||
|
||
%%==================================================================== | ||
%% internal | ||
%%==================================================================== | ||
-spec start_cache(mongooseim:host_type(), gen_mod:module_opts()) -> any(). | ||
start_cache(HostType, Opts) -> | ||
FinalOpts = maps:to_list(maps:merge(defaults(), maps:from_list(Opts))), | ||
mongoose_user_cache:start_new_cache(HostType, ?MODULE, FinalOpts). | ||
|
||
defaults() -> | ||
#{module => internal, | ||
strategy => fifo, | ||
time_to_live => 2, | ||
number_of_segments => 3 | ||
}. | ||
|
||
-spec stop_cache(mongooseim:host_type()) -> any(). | ||
stop_cache(HostType) -> | ||
mongoose_user_cache:stop_cache(HostType, ?MODULE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
-author('[email protected]'). | ||
|
||
%% API | ||
-export([handle_request/5, maybe_forget/3, process_request/5]). | ||
-export([handle_request/5, maybe_forget/4, process_request/5]). | ||
|
||
%% Callbacks | ||
-export([participant_limit_check/2]). | ||
|
@@ -48,12 +48,16 @@ handle_request(From, Room, OrigPacket, Request, Acc1) -> | |
send_response(From, Room, OrigPacket, Response, Acc2). | ||
|
||
-spec maybe_forget(Acc :: mongoose_acc:t(), | ||
RoomUS :: jid:simple_bare_jid(), NewAffUsers :: aff_users()) -> any(). | ||
maybe_forget(Acc, {RoomU, RoomS} = RoomUS, []) -> | ||
RoomUS :: jid:simple_bare_jid(), | ||
NewAffUsers :: aff_users(), | ||
Version :: binary() ) -> any(). | ||
maybe_forget(Acc, {RoomU, RoomS} = RoomUS, [], _Version) -> | ||
HostType = mod_muc_light_utils:acc_to_host_type(Acc), | ||
mongoose_hooks:forget_room(HostType, RoomS, RoomU), | ||
mod_muc_light_db_backend:destroy_room(HostType, RoomUS); | ||
maybe_forget(_Acc, _, _) -> | ||
maybe_forget(Acc, {RoomU, RoomS}, NewAffs, Version) -> | ||
RoomJid = jid:make_noprep(RoomU, RoomS, <<>>), | ||
mongoose_hooks:room_new_affiliations(Acc, RoomJid, NewAffs, Version), | ||
my_room_will_go_on. | ||
|
||
%%==================================================================== | ||
|
@@ -150,7 +154,7 @@ process_request({set, #destroy{} = DestroyReq}, | |
_From, RoomUS, {_, owner}, AffUsers, Acc) -> | ||
HostType = mongoose_acc:host_type(Acc), | ||
ok = mod_muc_light_db_backend:destroy_room(HostType, RoomUS), | ||
maybe_forget(Acc, RoomUS, []), | ||
maybe_forget(Acc, RoomUS, [], <<>>), | ||
{set, DestroyReq, AffUsers}; | ||
process_request({set, #destroy{}}, | ||
_From, _RoomUS, _Auth, _AffUsers, _Acc) -> | ||
|
@@ -229,7 +233,7 @@ process_aff_set(AffReq, RoomUS, {ok, FilteredAffUsers}, Acc) -> | |
case mod_muc_light_db_backend:modify_aff_users(HostType, RoomUS, FilteredAffUsers, | ||
fun ?MODULE:participant_limit_check/2, NewVersion) of | ||
{ok, OldAffUsers, NewAffUsers, AffUsersChanged, OldVersion} -> | ||
maybe_forget(Acc, RoomUS, NewAffUsers), | ||
maybe_forget(Acc, RoomUS, NewAffUsers, NewVersion), | ||
{set, AffReq#affiliations{ | ||
prev_version = OldVersion, | ||
version = NewVersion, | ||
|
Oops, something went wrong.