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

150 lines
4.1 KiB
Erlang
Raw Normal View History

2017-11-10 17:51:22 +01:00
%%%----------------------------------------------------------------------
%%% File : mod_bosh_redis.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Purpose :
2017-03-28 20:12:26 +02:00
%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2017-11-10 17:51:22 +01:00
%%%
%%%
2018-01-05 21:18:58 +01:00
%%% ejabberd, Copyright (C) 2017-2018 ProcessOne
2017-11-10 17:51:22 +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.
%%%
%%%----------------------------------------------------------------------
2017-03-28 20:12:26 +02:00
-module(mod_bosh_redis).
-behaviour(mod_bosh).
-behaviour(gen_server).
2017-03-28 20:12:26 +02:00
%% API
-export([init/0, open_session/2, close_session/1, find_session/1,
cache_nodes/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 20:12:26 +02:00
-include("logger.hrl").
-include("bosh.hrl").
2017-03-28 20:12:26 +02:00
-record(state, {}).
-define(BOSH_KEY, <<"ejabberd:bosh">>).
2017-03-28 20:12:26 +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 20:12:26 +02:00
open_session(SID, Pid) ->
PidBin = term_to_binary(Pid),
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hset(?BOSH_KEY, SID, PidBin),
ejabberd_redis:publish(?BOSH_KEY, SID)
end) of
2017-03-28 20:12:26 +02:00
{ok, _} ->
ok;
2017-04-07 08:10:33 +02:00
{error, _} ->
{error, db_failure}
2017-03-28 20:12:26 +02:00
end.
close_session(SID) ->
case ejabberd_redis:multi(
fun() ->
ejabberd_redis:hdel(?BOSH_KEY, [SID]),
ejabberd_redis:publish(?BOSH_KEY, SID)
end) of
{ok, _} ->
ok;
{error, _} ->
{error, db_failure}
end.
2017-03-28 20:12:26 +02:00
find_session(SID) ->
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hget(?BOSH_KEY, SID) of
{ok, undefined} ->
{error, notfound};
{ok, Pid} ->
2017-04-02 10:56:09 +02:00
try
{ok, binary_to_term(Pid)}
catch _:badarg ->
?ERROR_MSG("malformed data in redis (key = '~s'): ~p",
[SID, Pid]),
{error, db_failure}
2017-04-02 10:56:09 +02:00
end;
{error, _} ->
{error, db_failure}
2017-03-28 20:12:26 +02:00
end.
cache_nodes() ->
[node()].
%%%===================================================================
%%% 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({redis_message, ?BOSH_KEY, SID}, State) ->
ets_cache:delete(?BOSH_CACHE, SID),
{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 20:12:26 +02:00
%%%===================================================================
%%% Internal functions
%%%===================================================================
clean_table() ->
2017-04-15 14:47:00 +02:00
?DEBUG("Cleaning Redis BOSH sessions...", []),
2017-04-02 10:56:09 +02:00
case ejabberd_redis:hgetall(?BOSH_KEY) of
2017-03-28 20:12:26 +02:00
{ok, Vals} ->
2017-04-07 08:10:33 +02:00
ejabberd_redis:multi(
fun() ->
lists:foreach(
fun({SID, Pid}) when node(Pid) == node() ->
ejabberd_redis:hdel(?BOSH_KEY, [SID]);
(_) ->
ok
end, Vals)
end),
ok;
{error, _} ->
?ERROR_MSG("failed to clean bosh sessions in redis", [])
2017-03-28 20:12:26 +02:00
end.