From f68da70f3ed77700558d39050c2353f46937630a Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Mon, 1 Jul 2019 20:43:57 +0300 Subject: [PATCH] Avoid last handled stanzas cache to grow indefinitely --- rebar.config | 2 +- src/mod_stream_mgmt.erl | 10 +++++++--- src/mod_stream_mgmt_opt.erl | 7 +++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/rebar.config b/rebar.config index b4ba34e46..acf3ebb02 100644 --- a/rebar.config +++ b/rebar.config @@ -20,7 +20,7 @@ {deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager", "3.6.10"}}, {p1_utils, ".*", {git, "https://github.com/processone/p1_utils", "2887223"}}, - {cache_tab, ".*", {git, "https://github.com/processone/cache_tab", "f3d8376"}}, + {cache_tab, ".*", {git, "https://github.com/processone/cache_tab", "01e0f33"}}, {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.1.1"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.16"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", "7fd02f3a2f"}}, diff --git a/src/mod_stream_mgmt.erl b/src/mod_stream_mgmt.erl index c151807ff..1ebff130c 100644 --- a/src/mod_stream_mgmt.erl +++ b/src/mod_stream_mgmt.erl @@ -752,7 +752,8 @@ init_cache(Opts) -> cache_opts(Opts) -> [{max_size, mod_stream_mgmt_opt:cache_size(Opts)}, - {life_time, infinity}]. + {life_time, mod_stream_mgmt_opt:cache_life_time(Opts)}, + {type, ordered_set}]. -spec store_stanzas_in(ljid(), erlang:timestamp(), non_neg_integer()) -> boolean(). store_stanzas_in(LJID, Time, Num) -> @@ -763,8 +764,8 @@ store_stanzas_in(LJID, Time, Num) -> pop_stanzas_in(LJID, Time) -> case ets_cache:lookup(?STREAM_MGMT_CACHE, {LJID, Time}) of {ok, Val} -> - ets_cache:delete(?STREAM_MGMT_CACHE, {LJID, Time}, - ejabberd_cluster:get_nodes()), + ets_cache:match_delete(?STREAM_MGMT_CACHE, {LJID, '_'}, + ejabberd_cluster:get_nodes()), {ok, Val}; error -> error @@ -809,6 +810,8 @@ mod_opt_type(resend_on_timeout) -> econf:bool()); mod_opt_type(cache_size) -> econf:pos_int(infinity); +mod_opt_type(cache_life_time) -> + econf:timeout(second, infinity); mod_opt_type(queue_type) -> econf:queue_type(). @@ -818,5 +821,6 @@ mod_options(Host) -> {max_resume_timeout, undefined}, {ack_timeout, timer:seconds(60)}, {cache_size, ejabberd_option:cache_size(Host)}, + {cache_life_time, timer:hours(48)}, {resend_on_timeout, false}, {queue_type, ejabberd_option:queue_type(Host)}]. diff --git a/src/mod_stream_mgmt_opt.erl b/src/mod_stream_mgmt_opt.erl index 102906fdc..58d4fe1e7 100644 --- a/src/mod_stream_mgmt_opt.erl +++ b/src/mod_stream_mgmt_opt.erl @@ -4,6 +4,7 @@ -module(mod_stream_mgmt_opt). -export([ack_timeout/1]). +-export([cache_life_time/1]). -export([cache_size/1]). -export([max_ack_queue/1]). -export([max_resume_timeout/1]). @@ -17,6 +18,12 @@ ack_timeout(Opts) when is_map(Opts) -> ack_timeout(Host) -> gen_mod:get_module_opt(Host, mod_stream_mgmt, ack_timeout). +-spec cache_life_time(gen_mod:opts() | global | binary()) -> 'infinity' | pos_integer(). +cache_life_time(Opts) when is_map(Opts) -> + gen_mod:get_opt(cache_life_time, Opts); +cache_life_time(Host) -> + gen_mod:get_module_opt(Host, mod_stream_mgmt, cache_life_time). + -spec cache_size(gen_mod:opts() | global | binary()) -> 'infinity' | pos_integer(). cache_size(Opts) when is_map(Opts) -> gen_mod:get_opt(cache_size, Opts); -- 2.40.0