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

190 lines
5.1 KiB
Erlang
Raw Normal View History

2017-03-28 18:34:04 +02:00
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
%%%
%%% 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.
%%%
%%%-------------------------------------------------------------------
-module(ejabberd_router_redis).
-behaviour(ejabberd_router).
-behaviour(gen_server).
2017-03-28 18:34:04 +02:00
%% API
-export([init/0, register_route/5, unregister_route/3, find_routes/1,
get_all_routes/0]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
terminate/2, code_change/3, start_link/0]).
2017-03-28 18:34:04 +02:00
-include("ejabberd.hrl").
-include("logger.hrl").
-include("ejabberd_router.hrl").
-record(state, {}).
-define(ROUTES_KEY, <<"ejabberd:routes">>).
2017-04-14 19:34:00 +02:00
-define(DOMAINS_KEY, <<"ejabberd:domains">>).
2017-03-28 18:34:04 +02:00
%%%===================================================================
%%% API
%%%===================================================================
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, [], []).
2017-03-28 18:34:04 +02:00
register_route(Domain, ServerHost, LocalHint, _, Pid) ->
DomKey = domain_key(Domain),
PidKey = term_to_binary(Pid),
T = term_to_binary({ServerHost, LocalHint}),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hset(DomKey, PidKey, T),
2017-04-14 19:34:00 +02:00
ejabberd_redis:sadd(?DOMAINS_KEY, [Domain]),
if Domain /= ServerHost ->
ejabberd_redis:sadd(?ROUTES_KEY, [Domain]);
true ->
ok
end
2017-04-02 10:56:09 +02:00
end) of
{ok, _} ->
2017-03-28 18:34:04 +02:00
ok;
2017-04-07 08:10:33 +02:00
{error, _} ->
{error, db_failure}
2017-03-28 18:34:04 +02:00
end.
unregister_route(Domain, _, Pid) ->
DomKey = domain_key(Domain),
PidKey = term_to_binary(Pid),
try
2017-04-02 10:56:09 +02:00
{ok, Num} = ejabberd_redis:hdel(DomKey, [PidKey]),
if Num > 0 ->
{ok, Len} = ejabberd_redis:hlen(DomKey),
if Len == 0 ->
{ok, _} = ejabberd_redis:multi(
fun() ->
ejabberd_redis:del([DomKey]),
2017-04-14 19:34:00 +02:00
ejabberd_redis:srem(?ROUTES_KEY, [Domain]),
ejabberd_redis:srem(?DOMAINS_KEY, [Domain])
2017-04-02 10:56:09 +02:00
end),
ok;
true ->
ok
end;
true ->
2017-03-28 18:34:04 +02:00
ok
end
2017-04-07 08:10:33 +02:00
catch _:{badmatch, {error, _}} ->
{error, db_failure}
2017-03-28 18:34:04 +02:00
end.
find_routes(Domain) ->
DomKey = domain_key(Domain),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(DomKey) of
2017-03-28 18:34:04 +02:00
{ok, Vals} ->
{ok, decode_routes(Domain, Vals)};
2017-04-07 08:10:33 +02:00
_ ->
{error, db_failure}
2017-03-28 18:34:04 +02:00
end.
get_all_routes() ->
2017-04-02 10:56:09 +02:00
case ejabberd_redis:smembers(?ROUTES_KEY) of
2017-03-28 18:34:04 +02:00
{ok, Routes} ->
{ok, Routes};
_ ->
{error, db_failure}
2017-03-28 18:34:04 +02:00
end.
2017-04-14 19:34:00 +02:00
get_all_domains() ->
case ejabberd_redis:smembers(?DOMAINS_KEY) of
{ok, Domains} ->
{ok, Domains};
_ ->
{error, db_failure}
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([]) ->
clean_table(),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{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}.
2017-03-28 18:34:04 +02:00
%%%===================================================================
%%% Internal functions
%%%===================================================================
clean_table() ->
2017-04-15 14:47:00 +02:00
?DEBUG("Cleaning Redis route entries...", []),
2017-03-28 18:34:04 +02:00
lists:foreach(
fun(#route{domain = Domain, pid = Pid}) when node(Pid) == node() ->
unregister_route(Domain, undefined, Pid);
(_) ->
ok
end, find_routes()).
find_routes() ->
2017-04-14 19:34:00 +02:00
case get_all_domains() of
{ok, Domains} ->
lists:flatmap(
fun(Domain) ->
case find_routes(Domain) of
{ok, Routes} -> Routes;
{error, _} -> []
end
end, Domains);
{error, _} ->
[]
end.
2017-03-28 18:34:04 +02:00
domain_key(Domain) ->
<<"ejabberd:route:", Domain/binary>>.
2017-04-02 10:56:09 +02:00
decode_routes(Domain, Vals) ->
lists:map(
fun({Pid, Data}) ->
{ServerHost, LocalHint} = binary_to_term(Data),
#route{domain = Domain,
pid = binary_to_term(Pid),
server_host = ServerHost,
local_hint = LocalHint}
end, Vals).