Skip to content

Commit 2c8bbe5

Browse files
authored
Merge pull request #4586 from catap/mnesia-publish-duplicates
Handle duplicate Mnesia push sessions
2 parents 68713bc + 9a70685 commit 2c8bbe5

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/mod_push.erl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,16 @@ delete_session(LUser, LServer, ID) ->
623623
-spec delete_session(binary(), binary(), jid(), binary()) -> ok | {error, err_reason()}.
624624
delete_session(LUser, LServer, PushJID, Node) ->
625625
Mod = gen_mod:db_mod(LServer, ?MODULE),
626-
case Mod:lookup_session(LUser, LServer, PushJID, Node) of
627-
{ok, {ID, _, _, _}} ->
628-
delete_session(LUser, LServer, ID);
629-
error ->
630-
{error, notfound};
631-
{error, _} = Err ->
632-
Err
633-
end.
626+
LookupFun = fun() ->
627+
case Mod:lookup_sessions(LUser, LServer, PushJID) of
628+
{ok, Sessions} ->
629+
{ok, [Session || {_, _, N, _} = Session <- Sessions,
630+
N == Node]};
631+
{error, _} = Err ->
632+
Err
633+
end
634+
end,
635+
delete_sessions(LUser, LServer, LookupFun, Mod).
634636

635637
-spec delete_sessions(binary(), binary(), jid()) -> ok | {error, err_reason()}.
636638
delete_sessions(LUser, LServer, PushJID) ->

src/mod_push_mnesia.erl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) ->
5252
PushLJID = jid:tolower(PushJID),
5353
MaxSessions = ejabberd_sm:get_max_user_sessions(LUser, LServer),
5454
F = fun() ->
55+
Recs = mnesia:wread({push_session, US}),
56+
lists:foreach(
57+
fun(#push_session{service = P, node = N} = Rec)
58+
when P == PushLJID, N == Node ->
59+
mnesia:delete_object(Rec);
60+
(_) ->
61+
ok
62+
end, Recs),
5563
enforce_max_sessions(US, MaxSessions),
5664
mnesia:write(#push_session{us = US,
5765
timestamp = TS,
@@ -78,8 +86,13 @@ lookup_session(LUser, LServer, PushJID, Node) ->
7886
N == Node ->
7987
Rec
8088
end),
81-
case mnesia:dirty_select(push_session, MatchSpec) of
82-
[#push_session{timestamp = TS, xml = El}] ->
89+
Records = mnesia:dirty_select(push_session, MatchSpec),
90+
SortedRecords = lists:sort(fun(#push_session{timestamp = TS1},
91+
#push_session{timestamp = TS2}) ->
92+
TS1 >= TS2
93+
end, Records),
94+
case SortedRecords of
95+
[#push_session{timestamp = TS, xml = El} | _] ->
8396
{ok, {TS, PushLJID, Node, decode_xdata(El)}};
8497
[] ->
8598
?DEBUG("No push session found for ~ts@~ts (~p, ~ts)",

0 commit comments

Comments
 (0)