mod_push_mnesia: Apply cosmetic changes

Improve the readability of the 'max_user_sessions' check.
This commit is contained in:
Holger Weiss 2018-05-23 21:40:54 +02:00
parent 508f3ef88d
commit c6a9c30f1c
1 changed files with 15 additions and 17 deletions

View File

@ -52,11 +52,7 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) ->
PushLJID = jid:tolower(PushJID), PushLJID = jid:tolower(PushJID),
MaxSessions = ejabberd_sm:get_max_user_sessions(LUser, LServer), MaxSessions = ejabberd_sm:get_max_user_sessions(LUser, LServer),
F = fun() -> F = fun() ->
if is_integer(MaxSessions) -> enforce_max_sessions(US, MaxSessions),
enforce_max_sessions(US, MaxSessions - 1);
MaxSessions == infinity ->
ok
end,
mnesia:write(#push_session{us = US, mnesia:write(#push_session{us = US,
timestamp = TS, timestamp = TS,
service = PushLJID, service = PushLJID,
@ -185,19 +181,21 @@ transform({push_session, US, TS, Service, Node, XData}) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal functions. %% Internal functions.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec enforce_max_sessions({binary(), binary()}, non_neg_integer()) -> ok. -spec enforce_max_sessions({binary(), binary()}, non_neg_integer() | infinity)
enforce_max_sessions({U, S} = US, Max) -> -> ok.
Recs = mnesia:wread({push_session, US}), enforce_max_sessions(_US, infinity) ->
NumRecs = length(Recs), ok;
if NumRecs > Max -> enforce_max_sessions({U, S} = US, MaxSessions) ->
NumOldRecs = NumRecs - Max, case mnesia:wread({push_session, US}) of
Recs1 = lists:keysort(#push_session.timestamp, Recs), Recs when length(Recs) >= MaxSessions ->
Recs2 = lists:reverse(Recs1), Recs1 = lists:sort(fun(#push_session{timestamp = TS1},
OldRecs = lists:sublist(Recs2, Max + 1, NumOldRecs), #push_session{timestamp = TS2}) ->
?INFO_MSG("Disabling ~B old push session(s) of ~s@~s", TS1 >= TS2
[NumOldRecs, U, S]), end, Recs),
OldRecs = lists:nthtail(MaxSessions - 1, Recs1),
?INFO_MSG("Disabling old push session(s) of ~s@~s", [U, S]),
lists:foreach(fun(Rec) -> mnesia:delete_object(Rec) end, OldRecs); lists:foreach(fun(Rec) -> mnesia:delete_object(Rec) end, OldRecs);
true -> _ ->
ok ok
end. end.