aboutsummaryrefslogtreecommitdiff
path: root/src/mod_bosh_redis.erl
blob: 3847befc08030b4fffdf2040b2605424eb37e8d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2017, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_bosh_redis).
-behaviour(mod_bosh).
-behaviour(gen_server).

%% 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]).

-include("ejabberd.hrl").
-include("logger.hrl").
-include("bosh.hrl").

-record(state, {}).

-define(BOSH_KEY, <<"ejabberd:bosh">>).

%%%===================================================================
%%% 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, [], []).

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
	{ok, _} ->
	    ok;
	{error, _} ->
	    {error, db_failure}
    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.

find_session(SID) ->
    case ejabberd_redis:hget(?BOSH_KEY, SID) of
	{ok, undefined} ->
	    {error, notfound};
	{ok, Pid} ->
	    try
		{ok, binary_to_term(Pid)}
	    catch _:badarg ->
		    ?ERROR_MSG("malformed data in redis (key = '~s'): ~p",
			       [SID, Pid]),
		    {error, db_failure}
	    end;
	{error, _} ->
	    {error, db_failure}
    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}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
clean_table() ->
    ?DEBUG("Cleaning Redis BOSH sessions...", []),
    case ejabberd_redis:hgetall(?BOSH_KEY) of
	{ok, Vals} ->
	    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", [])
    end.