25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_sm_redis.erl

213 lines
6.2 KiB
Erlang
Raw Normal View History

2015-03-11 12:46:57 +01:00
%%%-------------------------------------------------------------------
2016-12-27 10:44:07 +01:00
%%% File : ejabberd_sm_redis.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
2015-03-11 12:46:57 +01:00
%%% Created : 11 Mar 2015 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2016-12-27 10:44:07 +01:00
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
2016-12-27 10:44:07 +01:00
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
2015-03-11 12:46:57 +01:00
-module(ejabberd_sm_redis).
-ifndef(GEN_SERVER).
-define(GEN_SERVER, p1_server).
-endif.
-behaviour(?GEN_SERVER).
2015-06-01 14:38:27 +02:00
2015-03-11 12:46:57 +01:00
-behaviour(ejabberd_sm).
-export([init/0, set_session/1, delete_session/1,
2015-06-01 14:38:27 +02:00
get_sessions/0, get_sessions/1, get_sessions/2,
cache_nodes/1]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
terminate/2, code_change/3, start_link/0]).
2015-03-11 12:46:57 +01:00
-include("ejabberd.hrl").
-include("ejabberd_sm.hrl").
-include("logger.hrl").
-define(SM_KEY, <<"ejabberd:sm">>).
-record(state, {}).
2015-03-11 12:46:57 +01:00
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
?GEN_SERVER:start_link({local, ?MODULE}, ?MODULE, [], []).
2015-03-11 12:46:57 +01:00
-spec cache_nodes(binary()) -> [node()].
cache_nodes(_LServer) ->
[node()].
-spec set_session(#session{}) -> ok | {error, ejabberd_redis:error_reason()}.
2015-03-11 12:46:57 +01:00
set_session(Session) ->
T = term_to_binary(Session),
USKey = us_to_key(Session#session.us),
SIDKey = sid_to_key(Session#session.sid),
ServKey = server_to_key(element(2, Session#session.us)),
USSIDKey = us_sid_to_key(Session#session.us, Session#session.sid),
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hset(USKey, SIDKey, T),
ejabberd_redis:hset(ServKey, USSIDKey, T),
ejabberd_redis:publish(
?SM_KEY, term_to_binary({delete, Session#session.us}))
end) of
{ok, _} ->
ok;
Err ->
Err
end.
2015-03-11 12:46:57 +01:00
-spec delete_session(#session{}) -> ok | {error, ejabberd_redis:error_reason()}.
delete_session(#session{sid = SID} = Session) ->
USKey = us_to_key(Session#session.us),
SIDKey = sid_to_key(SID),
ServKey = server_to_key(element(2, Session#session.us)),
USSIDKey = us_sid_to_key(Session#session.us, SID),
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hdel(USKey, [SIDKey]),
ejabberd_redis:hdel(ServKey, [USSIDKey]),
ejabberd_redis:publish(
?SM_KEY,
term_to_binary({delete, Session#session.us}))
end) of
{ok, _} ->
ok;
Err ->
Err
2015-03-11 12:46:57 +01:00
end.
-spec get_sessions() -> [#session{}].
get_sessions() ->
lists:flatmap(
fun(LServer) ->
get_sessions(LServer)
end, ejabberd_sm:get_vh_by_backend(?MODULE)).
2015-03-11 12:46:57 +01:00
-spec get_sessions(binary()) -> [#session{}].
get_sessions(LServer) ->
ServKey = server_to_key(LServer),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(ServKey) of
2015-03-11 12:46:57 +01:00
{ok, Vals} ->
decode_session_list(Vals);
2017-04-07 08:10:33 +02:00
{error, _} ->
2015-03-11 12:46:57 +01:00
[]
end.
-spec get_sessions(binary(), binary()) -> {ok, [#session{}]} |
{error, ejabberd_redis:error_reason()}.
2015-03-11 12:46:57 +01:00
get_sessions(LUser, LServer) ->
USKey = us_to_key({LUser, LServer}),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(USKey) of
{ok, Vals} ->
{ok, decode_session_list(Vals)};
Err ->
Err
2015-03-11 12:46:57 +01:00
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([]) ->
ejabberd_redis:subscribe([?SM_KEY]),
clean_table(),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({redis_message, ?SM_KEY, Data}, State) ->
case binary_to_term(Data) of
{delete, Key} ->
ets_cache:delete(?SM_CACHE, Key);
Msg ->
?WARNING_MSG("unexpected redis message: ~p", [Msg])
end,
{noreply, State};
handle_info(Info, State) ->
?ERROR_MSG("unexpected info: ~p", [Info]),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
2015-03-11 12:46:57 +01:00
%%%===================================================================
%%% Internal functions
%%%===================================================================
us_to_key({LUser, LServer}) ->
<<"ejabberd:sm:", LUser/binary, "@", LServer/binary>>.
server_to_key(LServer) ->
<<"ejabberd:sm:", LServer/binary>>.
us_sid_to_key(US, SID) ->
term_to_binary({US, SID}).
sid_to_key(SID) ->
term_to_binary(SID).
2017-04-02 10:56:09 +02:00
decode_session_list(Vals) ->
[binary_to_term(Val) || {_, Val} <- Vals].
2015-03-11 12:46:57 +01:00
clean_table() ->
2017-04-15 14:47:00 +02:00
?DEBUG("Cleaning Redis SM table...", []),
2017-04-02 10:56:09 +02:00
try
lists:foreach(
fun(LServer) ->
ServKey = server_to_key(LServer),
{ok, Vals} = ejabberd_redis:hkeys(ServKey),
{ok, _} =
ejabberd_redis:multi(
fun() ->
lists:foreach(
fun(USSIDKey) ->
{US, SID} = binary_to_term(USSIDKey),
if node(element(2, SID)) == node() ->
USKey = us_to_key(US),
SIDKey = sid_to_key(SID),
ejabberd_redis:hdel(ServKey, [USSIDKey]),
ejabberd_redis:hdel(USKey, [SIDKey]);
true ->
ok
end
end, Vals)
end)
end, ejabberd_sm:get_vh_by_backend(?MODULE))
2017-04-07 08:10:33 +02:00
catch _:{badmatch, {error, _}} ->
?ERROR_MSG("failed to clean redis c2s sessions", [])
2017-04-02 10:56:09 +02:00
end.