From a3a21ecd375ce146d28d19f083e560bbbe739d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D1=80=D0=BE=D0=B2?= Date: Thu, 7 Mar 2019 02:30:02 +0100 Subject: [PATCH] Make it possible to configure upstream healthchecks --- src/mtp_handler.erl | 23 +++++++++++++---------- src/mtproto_proxy.app.src | 7 +++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/mtp_handler.erl b/src/mtp_handler.erl index c660352..71a48ba 100644 --- a/src/mtp_handler.erl +++ b/src/mtp_handler.erl @@ -345,28 +345,31 @@ maybe_check_health(#state{last_queue_check = LastCheck} = S) -> end end. -%% 1. If proc queue > 300 - stop -%% 2. If proc total memory > 400kb - do GC and go to 3 -%% 3. If proc total memory > 4mb - stop +%% 1. If proc queue > qlen - stop +%% 2. If proc total memory > gc - do GC and go to 3 +%% 3. If proc total memory > total_mem - stop check_health() -> - do_check_health([qlen, gc, total_mem], calc_health()). + %% see .app.src + Defaults = [{qlen, 300}, + {gc, 409600}, + {total_mem, 3145728}], + Checks = application:get_env(?APP, upstream_healthchecks, Defaults), + do_check_health(Checks, calc_health()). -do_check_health([qlen | _], #{message_queue_len := QLen} = Health) when - QLen > ?HEALTH_CHECK_MAX_QLEN -> +do_check_health([{qlen, Limit} | _], #{message_queue_len := QLen} = Health) when QLen > Limit -> mtp_metric:count_inc([?APP, healthcheck, total], 1, #{labels => [message_queue_len]}), lager:warning("Upstream too large queue_len=~w, health=~p", [QLen, Health]), overflow; -do_check_health([gc | Other], #{total_mem := TotalMem}) when - TotalMem > ?HEALTH_CHECK_GC -> +do_check_health([{gc, Limit} | Other], #{total_mem := TotalMem}) when TotalMem > Limit -> %% Maybe it doesn't makes sense to do GC if queue len is more than, eg, 50? %% In this case allmost all memory will be in msg queue mtp_metric:count_inc([?APP, healthcheck, total], 1, #{labels => [force_gc]}), erlang:garbage_collect(self()), do_check_health(Other, calc_health()); -do_check_health([total_mem | _Other], #{total_mem := TotalMem} = Health) when - TotalMem > ?HEALTH_CHECK_MAX_MEM -> +do_check_health([{total_mem, Limit} | _Other], #{total_mem := TotalMem} = Health) when + TotalMem > Limit -> mtp_metric:count_inc([?APP, healthcheck, total], 1, #{labels => [total_memory]}), lager:warning("Process too large total_mem=~p, health=~p", diff --git a/src/mtproto_proxy.app.src b/src/mtproto_proxy.app.src index f7ee4af..22e0321 100644 --- a/src/mtproto_proxy.app.src +++ b/src/mtproto_proxy.app.src @@ -74,6 +74,13 @@ %% Mostly used to testing %% {proxy_secret_url, "https://core.telegram.org/getProxySecret"}, %% {proxy_config_url, "https://core.telegram.org/getProxyConfig"}, + + %% Upstream self-healthchecks tuning + %% {upstream_healthchecks, + %% [{qlen, 300}, % if queue length >X - close connection + %% {gc, 409600}, % if connection memory >X - do garbage collection + %% {total_mem, 3145728} % if connection memory >X - close connection + %% ]}, ]}, {modules, []},