From: Holger Weiss Date: Sun, 2 Oct 2016 20:01:03 +0000 (+0200) Subject: ejabberd_sm: Clean up old offline session entries X-Git-Tag: 16.12~25^2~29 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9cee3760db4f304d8354c33b641b84f5423e1a80;p=ejabberd ejabberd_sm: Clean up old offline session entries If the number of offline sessions exceeds the 'max_user_sessions' limit, remove the oldest entry from the table. --- diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 16e0f9114..3369b7ca0 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -713,10 +713,18 @@ get_resource_sessions(User, Server, Resource) -> check_max_sessions(LUser, LServer) -> Mod = get_sm_backend(LServer), - SIDs = [S#session.sid || S <- online(Mod:get_sessions(LUser, LServer))], + Ss = Mod:get_sessions(LUser, LServer), + {OnlineSs, OfflineSs} = lists:partition(fun is_online/1, Ss), MaxSessions = get_max_user_sessions(LUser, LServer), - if length(SIDs) =< MaxSessions -> ok; - true -> {_, Pid} = lists:min(SIDs), Pid ! replaced + if length(OnlineSs) =< MaxSessions -> ok; + true -> + #session{sid = {_, Pid}} = lists:min(OnlineSs), + Pid ! replaced + end, + if length(OfflineSs) =< MaxSessions -> ok; + true -> + #session{sid = SID, usr = {_, _, R}} = lists:min(OfflineSs), + Mod:delete_session(LUser, LServer, R, SID) end. %% Get the user_max_session setting