diff options
author | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-04-14 13:57:52 +0300 |
---|---|---|
committer | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-04-14 13:57:52 +0300 |
commit | e40baf0bdaecf3206420fe8c16c33f2c166cb717 (patch) | |
tree | 75d9fe880e8257ea9fd20c095c252d7940cea89d /src/mod_bosh_redis.erl | |
parent | Bump xmpp dependency, it's required by previous commit (diff) |
Use cache in front of Redis/SQL RAM backends
Diffstat (limited to 'src/mod_bosh_redis.erl')
-rw-r--r-- | src/mod_bosh_redis.erl | 81 |
1 files changed, 71 insertions, 10 deletions
diff --git a/src/mod_bosh_redis.erl b/src/mod_bosh_redis.erl index 156df368b..194d220a1 100644 --- a/src/mod_bosh_redis.erl +++ b/src/mod_bosh_redis.erl @@ -8,24 +8,45 @@ %%%------------------------------------------------------------------- -module(mod_bosh_redis). -behaviour(mod_bosh). +-behaviour(gen_server). %% API --export([init/0, open_session/2, close_session/1, find_session/1]). +-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"). --define(BOSH_KEY, "ejabberd:bosh"). +-record(state, {}). + +-define(BOSH_KEY, <<"ejabberd:bosh">>). %%%=================================================================== %%% API %%%=================================================================== init() -> - clean_table(). + 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:hset(?BOSH_KEY, SID, PidBin) of + case ejabberd_redis:multi( + fun() -> + ejabberd_redis:hset(?BOSH_KEY, SID, PidBin), + ejabberd_redis:publish(?BOSH_KEY, SID) + end) of {ok, _} -> ok; {error, _} -> @@ -33,23 +54,63 @@ open_session(SID, Pid) -> end. close_session(SID) -> - ejabberd_redis:hdel(?BOSH_KEY, [SID]), - ok. + 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, Pid} when is_binary(Pid) -> + {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 + {error, db_failure} end; - _ -> - error + {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 %%%=================================================================== |