Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(gateway): add community visitor config, state and metrics

Hampus62d32b3efc9e
diff --git a/fluxer_gateway/src/gateway/fluxer_gateway_config.erl b/fluxer_gateway/src/gateway/fluxer_gateway_config.erl
index 726068955..a997c5f31 100644
--- a/fluxer_gateway/src/gateway/fluxer_gateway_config.erl
+++ b/fluxer_gateway/src/gateway/fluxer_gateway_config.erl
@@ -3,8 +3,8 @@
-module(fluxer_gateway_config).
-typing([eqwalizer]).
--export([load/0, build_config/1, optional_string/1, env_value/1]).
--export_type([config/0]).
+-export([load/0, optional_string/1, env_value/1, visitor_limit_default/1]).
+-export_type([config/0, visitor_limit_key/0]).
-type config() :: map().
-type raw_value() ::
@@ -17,6 +17,12 @@
| undefined.
-type log_level() :: debug | info | notice | warning | error | critical | alert | emergency.
-type gateway_role() :: websocket | sessions | presence | guilds | calls | push | all.
+-type visitor_limit_key() ::
+ guild_visitor_sessions_max
+ | guild_visitor_sessions_floor
+ | guild_visitor_sessions_per_member
+ | guild_visitor_sessions_per_user
+ | guild_visitor_sessions_per_ip.
-define(MAX_CLUSTER_STATIC_PEERS, 256).
@@ -67,7 +73,28 @@ env_services_config() ->
-spec env_gateway_config() -> map().
env_gateway_config() ->
- env_gateway_base_config().
+ maps:merge(env_gateway_base_config(), #{
+ <<"guild_visitor_sessions_max">> => env_int(
+ "FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_MAX",
+ visitor_limit_default(guild_visitor_sessions_max)
+ ),
+ <<"guild_visitor_sessions_floor">> => env_int(
+ "FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_FLOOR",
+ visitor_limit_default(guild_visitor_sessions_floor)
+ ),
+ <<"guild_visitor_sessions_per_member">> => env_int(
+ "FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_PER_MEMBER",
+ visitor_limit_default(guild_visitor_sessions_per_member)
+ ),
+ <<"guild_visitor_sessions_per_user">> => env_int(
+ "FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_PER_USER",
+ visitor_limit_default(guild_visitor_sessions_per_user)
+ ),
+ <<"guild_visitor_sessions_per_ip">> => env_int(
+ "FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_PER_IP",
+ visitor_limit_default(guild_visitor_sessions_per_ip)
+ )
+ }).
-spec env_gateway_base_config() -> map().
env_gateway_base_config() ->
@@ -156,6 +183,7 @@ build_config(RawConfig) ->
build_http_config(Service),
build_cluster_config(Service, Public),
build_pinned_node_config(Service),
+ build_visitor_config(Service),
build_misc_config(Service)
]).
@@ -278,6 +306,43 @@ parse_guild_id(Token) ->
error:badarg -> erlang:error({invalid_pinned_guild_id, Token})
end.
+-spec build_visitor_config(map()) -> config().
+build_visitor_config(Service) ->
+ #{
+ guild_visitor_sessions_max => get_int(
+ Service,
+ <<"guild_visitor_sessions_max">>,
+ visitor_limit_default(guild_visitor_sessions_max)
+ ),
+ guild_visitor_sessions_floor => get_int(
+ Service,
+ <<"guild_visitor_sessions_floor">>,
+ visitor_limit_default(guild_visitor_sessions_floor)
+ ),
+ guild_visitor_sessions_per_member => get_int(
+ Service,
+ <<"guild_visitor_sessions_per_member">>,
+ visitor_limit_default(guild_visitor_sessions_per_member)
+ ),
+ guild_visitor_sessions_per_user => get_int(
+ Service,
+ <<"guild_visitor_sessions_per_user">>,
+ visitor_limit_default(guild_visitor_sessions_per_user)
+ ),
+ guild_visitor_sessions_per_ip => get_int(
+ Service,
+ <<"guild_visitor_sessions_per_ip">>,
+ visitor_limit_default(guild_visitor_sessions_per_ip)
+ )
+ }.
+
+-spec visitor_limit_default(visitor_limit_key()) -> non_neg_integer().
+visitor_limit_default(guild_visitor_sessions_max) -> 10000;
+visitor_limit_default(guild_visitor_sessions_floor) -> 500;
+visitor_limit_default(guild_visitor_sessions_per_member) -> 2;
+visitor_limit_default(guild_visitor_sessions_per_user) -> 5;
+visitor_limit_default(guild_visitor_sessions_per_ip) -> 20.
+
-spec build_misc_config(map()) -> config().
build_misc_config(Service) ->
#{
diff --git a/fluxer_gateway/src/gateway/metrics_handler.erl b/fluxer_gateway/src/gateway/metrics_handler.erl
index 3204987f5..9084bd144 100644
--- a/fluxer_gateway/src/gateway/metrics_handler.erl
+++ b/fluxer_gateway/src/gateway/metrics_handler.erl
@@ -27,17 +27,6 @@ init(Req0, State) ->
{ok, Req, State}
end.
--spec is_loopback_request(cowboy_req:req()) -> boolean().
-is_loopback_request(Req) ->
- case cowboy_req:peer(Req) of
- {{127, 0, 0, 1}, _Port} ->
- true;
- {{0, 0, 0, 0, 0, 0, 0, 1}, _Port} ->
- true;
- _ ->
- false
- end.
-
-spec render_metrics() -> iolist().
render_metrics() ->
[
@@ -45,6 +34,7 @@ render_metrics() ->
render_cluster_counters(),
render_process_counts(),
render_channel_threads_stats(),
+ render_guild_visitor_stats(safe_apply_map(fun guild_visitor_metrics:counts/0)),
render_push_outbox_stats(safe_apply_map(fun push_outbox:stats/0)),
render_vm_metrics()
].
@@ -62,61 +52,31 @@ render_gateway_gauges() ->
ConcurrentSessions = safe_apply_int(fun gateway_concurrency:session_start_count/0),
ConcurrentGuilds = safe_apply_int(fun gateway_concurrency:guild_start_count/0),
[
- format_metric(
- <<"fluxer_gateway_sessions_total">>,
- <<"gauge">>,
- <<"Connected WebSocket sessions">>,
- integer_to_binary(Sessions)
- ),
- format_metric(
- <<"fluxer_gateway_session_resumes_total">>,
- <<"counter">>,
- <<"Successful WebSocket session resumes">>,
- integer_to_binary(Resumes)
- ),
- format_metric(
- <<"fluxer_gateway_guilds_total">>,
- <<"gauge">>,
- <<"Locally loaded guilds">>,
- integer_to_binary(Guilds)
- ),
- format_metric(
- <<"fluxer_gateway_websocket_dispatches_total">>,
- <<"counter">>,
- <<"WebSocket dispatch frames handed to the transport, including replays">>,
- integer_to_binary(Dispatches)
- ),
- format_metric(
- <<"fluxer_gateway_websocket_dispatch_drops_total">>,
- <<"counter">>,
- <<"WebSocket dispatches dropped due to encoding or compression errors">>,
- integer_to_binary(DispatchDrops)
- ),
+ format_integer_metrics([
+ {<<"fluxer_gateway_sessions_total">>, <<"gauge">>,
+ <<"Connected WebSocket sessions">>, Sessions},
+ {<<"fluxer_gateway_session_resumes_total">>, <<"counter">>,
+ <<"Successful WebSocket session resumes">>, Resumes},
+ {<<"fluxer_gateway_guilds_total">>, <<"gauge">>, <<"Locally loaded guilds">>,
+ Guilds},
+ {<<"fluxer_gateway_websocket_dispatches_total">>, <<"counter">>,
+ <<"WebSocket dispatch frames handed to the transport, including replays">>,
+ Dispatches},
+ {<<"fluxer_gateway_websocket_dispatch_drops_total">>, <<"counter">>,
+ <<"WebSocket dispatches dropped due to encoding or compression errors">>,
+ DispatchDrops}
+ ]),
render_voice_metrics(VoiceCounts),
- format_metric(
- <<"fluxer_gateway_calls_total">>,
- <<"gauge">>,
- <<"Active voice calls">>,
- integer_to_binary(length(CallIds))
- ),
- format_metric(
- <<"fluxer_gateway_push_workers_active">>,
- <<"gauge">>,
- <<"Active push notification workers">>,
- integer_to_binary(PushActive)
- ),
- format_metric(
- <<"fluxer_gateway_concurrent_session_starts">>,
- <<"gauge">>,
- <<"In-flight session start operations">>,
- integer_to_binary(ConcurrentSessions)
- ),
- format_metric(
- <<"fluxer_gateway_concurrent_guild_starts">>,
- <<"gauge">>,
- <<"In-flight guild start operations">>,
- integer_to_binary(ConcurrentGuilds)
- )
+ format_integer_metrics([
+ {<<"fluxer_gateway_calls_total">>, <<"gauge">>, <<"Active voice calls">>,
+ length(CallIds)},
+ {<<"fluxer_gateway_push_workers_active">>, <<"gauge">>,
+ <<"Active push notification workers">>, PushActive},
+ {<<"fluxer_gateway_concurrent_session_starts">>, <<"gauge">>,
+ <<"In-flight session start operations">>, ConcurrentSessions},
+ {<<"fluxer_gateway_concurrent_guild_starts">>, <<"gauge">>,
+ <<"In-flight guild start operations">>, ConcurrentGuilds}
+ ])
].
-spec safe_apply_int(fun(() -> term())) -> integer().
@@ -274,6 +234,60 @@ count_registry_prefix(Prefix) ->
error:badarg -> 0
end.
+-spec render_guild_visitor_stats(map()) -> iolist().
+render_guild_visitor_stats(Counts) ->
+ [
+ format_metric(
+ <<"fluxer_gateway_guild_visitor_sessions">>,
+ <<"gauge">>,
+ <<"Live guild visitor sessions on this node">>,
+ integer_to_binary(max(0, visitor_count(live, Counts)))
+ ),
+ format_metric(
+ <<"fluxer_gateway_guild_visitor_admitted_total">>,
+ <<"counter">>,
+ <<"Guild visitor sessions admitted">>,
+ integer_to_binary(visitor_count(admitted, Counts))
+ ),
+ format_metric(
+ <<"fluxer_gateway_guild_visitor_converted_total">>,
+ <<"counter">>,
+ <<"Guild visitor sessions converted to member sessions">>,
+ integer_to_binary(visitor_count(converted, Counts))
+ ),
+ format_labeled_series(
+ <<"fluxer_gateway_guild_visitor_rejected_total">>,
+ <<"counter">>,
+ <<"Guild visitor connects rejected by reason">>,
+ [
+ {
+ <<"reason=\"", (atom_to_binary(Reason))/binary, "\"">>,
+ integer_to_binary(visitor_count({rejected, Reason}, Counts))
+ }
+ || Reason <- guild_visitor_metrics:reject_reasons()
+ ]
+ ),
+ format_labeled_series(
+ <<"fluxer_gateway_guild_visitor_evicted_total">>,
+ <<"counter">>,
+ <<"Guild visitor sessions evicted by reason">>,
+ [
+ {
+ <<"reason=\"", (atom_to_binary(Reason))/binary, "\"">>,
+ integer_to_binary(visitor_count({evicted, Reason}, Counts))
+ }
+ || Reason <- guild_visitor_metrics:evict_reasons()
+ ]
+ )
+ ].
+
+-spec visitor_count(term(), map()) -> integer().
+visitor_count(Key, Counts) ->
+ case maps:get(Key, Counts, 0) of
+ N when is_integer(N) -> N;
+ _ -> 0
+ end.
+
-spec render_channel_threads_stats() -> iolist().
render_channel_threads_stats() ->
UpdateCounts = safe_apply_map(fun channel_threads_config:update_counts/0),
@@ -289,27 +303,15 @@ render_channel_threads_stats() ->
<<"fluxer_gateway_channel_threads_config_updates_total">>,
<<"counter">>,
<<"Channel threads config reads by outcome">>,
- [
- {
- <<"result=\"", (atom_to_binary(Result))/binary, "\"">>,
- gate_counter(Result, UpdateCounts)
- }
- || Result <- [updated, unchanged, stale, rejected]
- ]
+ counter_labels(<<"result">>, [updated, unchanged, stale, rejected], UpdateCounts)
),
format_labeled_series(
<<"fluxer_gateway_channel_threads_flips_total">>,
<<"counter">>,
<<"Channel threads gate flips scheduled by kind">>,
- [
- {
- <<"kind=\"", (atom_to_binary(Kind))/binary, "\"">>,
- gate_counter(Kind, FlipCounts)
- }
- || Kind <- [guild, viewers, user, heal]
- ]
+ counter_labels(<<"kind">>, [guild, viewers, user, heal], FlipCounts)
),
- render_identify_counts(safe_apply_map(fun channel_threads_config:identify_counts/0)),
+ render_identify_counts(safe_apply_map(fun channel_threads_identify_counts:counts/0)),
render_thread_stats()
].
@@ -345,25 +347,13 @@ render_thread_stats() ->
<<"fluxer_gateway_thread_list_sync_total">>,
<<"counter">>,
<<"THREAD_LIST_SYNC sends by reason">>,
- [
- {
- <<"reason=\"", (atom_to_binary(Reason))/binary, "\"">>,
- gate_counter(Reason, SyncCounts)
- }
- || Reason <- [full, access]
- ]
+ counter_labels(<<"reason">>, [full, access], SyncCounts)
),
format_labeled_series(
<<"fluxer_gateway_thread_collection_fetch_total">>,
<<"counter">>,
<<"Thread collection fetches by result">>,
- [
- {
- <<"result=\"", (atom_to_binary(Result))/binary, "\"">>,
- gate_counter(Result, FetchCounts)
- }
- || Result <- [ok, error, refetch]
- ]
+ counter_labels(<<"result">>, [ok, error, refetch], FetchCounts)
),
format_metric(
<<"fluxer_gateway_thread_permission_unknown_parent_total">>,
@@ -494,30 +484,14 @@ render_vm_metrics() ->
SchedulerCount = safe_apply_int(fun() -> erlang:system_info(schedulers_online) end),
[
render_memory_metrics(Memory),
- format_metric(
- <<"erlang_vm_process_count">>,
- <<"gauge">>,
- <<"Erlang VM process count">>,
- integer_to_binary(ProcessCount)
- ),
- format_metric(
- <<"erlang_vm_port_count">>,
- <<"gauge">>,
- <<"Erlang VM port count">>,
- integer_to_binary(PortCount)
- ),
- format_metric(
- <<"erlang_vm_atom_count">>,
- <<"gauge">>,
- <<"Erlang VM atom count">>,
- integer_to_binary(AtomCount)
- ),
- format_metric(
- <<"erlang_vm_scheduler_count">>,
- <<"gauge">>,
- <<"Erlang VM online schedulers">>,
- integer_to_binary(SchedulerCount)
- )
+ format_integer_metrics([
+ {<<"erlang_vm_process_count">>, <<"gauge">>, <<"Erlang VM process count">>,
+ ProcessCount},
+ {<<"erlang_vm_port_count">>, <<"gauge">>, <<"Erlang VM port count">>, PortCount},
+ {<<"erlang_vm_atom_count">>, <<"gauge">>, <<"Erlang VM atom count">>, AtomCount},
+ {<<"erlang_vm_scheduler_count">>, <<"gauge">>, <<"Erlang VM online schedulers">>,
+ SchedulerCount}
+ ])
].
-spec render_memory_metrics(list()) -> iolist().
@@ -539,6 +513,23 @@ render_memory_metrics(Memory) ->
Lines
).
+-spec counter_labels(binary(), [atom()], map()) -> [{binary(), binary()}].
+counter_labels(Label, Keys, Counts) ->
+ [
+ {
+ <<Label/binary, "=\"", (atom_to_binary(Key))/binary, "\"">>,
+ gate_counter(Key, Counts)
+ }
+ || Key <- Keys
+ ].
+
+-spec format_integer_metrics([{binary(), binary(), binary(), integer()}]) -> iolist().
+format_integer_metrics(Metrics) ->
+ [
+ format_metric(Name, Type, Help, integer_to_binary(Value))
+ || {Name, Type, Help, Value} <- Metrics
+ ].
+
-spec format_metric(binary(), binary(), binary(), binary()) -> iolist().
format_metric(Name, Type, Help, Value) ->
[
diff --git a/fluxer_gateway/src/guild/guild_visitor_metrics.erl b/fluxer_gateway/src/guild/guild_visitor_metrics.erl
new file mode 100644
index 000000000..c66575316
--- /dev/null
+++ b/fluxer_gateway/src/guild/guild_visitor_metrics.erl
@@ -0,0 +1,100 @@
+%% SPDX-License-Identifier: AGPL-3.0-or-later
+
+-module(guild_visitor_metrics).
+-typing([eqwalizer]).
+
+-export([
+ init/0,
+ admitted/0,
+ rejected/1,
+ evicted/2,
+ converted/0,
+ session_added/0,
+ session_removed/1,
+ counts/0,
+ reject_reasons/0,
+ evict_reasons/0
+]).
+
+-define(KEY, {?MODULE, counters}).
+-define(REJECT_REASONS, [disabled, age_restricted, unavailable, busy]).
+-define(EVICT_REASONS, [busy, banned, disabled, age_restricted, removed, error]).
+
+-spec init() -> ok.
+init() ->
+ case persistent_term:get(?KEY, undefined) of
+ undefined ->
+ persistent_term:put(?KEY, counters:new(length(keys()), [write_concurrency]));
+ _ ->
+ ok
+ end.
+
+-spec admitted() -> ok.
+admitted() ->
+ add(admitted, 1).
+
+-spec rejected(atom()) -> ok.
+rejected(Reason) ->
+ add({rejected, Reason}, 1).
+
+-spec evicted(atom(), non_neg_integer()) -> ok.
+evicted(_Reason, 0) ->
+ ok;
+evicted(Reason, Count) ->
+ add({evicted, Reason}, Count).
+
+-spec converted() -> ok.
+converted() ->
+ add(converted, 1).
+
+-spec session_added() -> ok.
+session_added() ->
+ add(live, 1).
+
+-spec session_removed(non_neg_integer()) -> ok.
+session_removed(0) ->
+ ok;
+session_removed(Count) ->
+ add(live, -Count).
+
+-spec reject_reasons() -> [atom()].
+reject_reasons() ->
+ ?REJECT_REASONS.
+
+-spec evict_reasons() -> [atom()].
+evict_reasons() ->
+ ?EVICT_REASONS.
+
+-spec counts() -> #{term() => integer()}.
+counts() ->
+ Keys = keys(),
+ case persistent_term:get(?KEY, undefined) of
+ undefined ->
+ maps:from_list([{Key, 0} || Key <- Keys]);
+ Counters ->
+ maps:from_list([
+ {Key, counters:get(Counters, Index)}
+ || {Key, Index} <- lists:zip(Keys, lists:seq(1, length(Keys)))
+ ])
+ end.
+
+-spec keys() -> [term()].
+keys() ->
+ [admitted, converted, live] ++
+ [{rejected, Reason} || Reason <- ?REJECT_REASONS] ++
+ [{evicted, Reason} || Reason <- ?EVICT_REASONS].
+
+-spec add(term(), integer()) -> ok.
+add(Key, N) ->
+ ok = init(),
+ case index(Key, keys(), 1) of
+ undefined ->
+ ok;
+ Index ->
+ counters:add(persistent_term:get(?KEY), Index, N)
+ end.
+
+-spec index(term(), [term()], pos_integer()) -> pos_integer() | undefined.
+index(Key, [Key | _], Index) -> Index;
+index(Key, [_ | Rest], Index) -> index(Key, Rest, Index + 1);
+index(_Key, [], _Index) -> undefined.
diff --git a/fluxer_gateway/src/guild/guild_visitor_state.erl b/fluxer_gateway/src/guild/guild_visitor_state.erl
new file mode 100644
index 000000000..74ff23105
--- /dev/null
+++ b/fluxer_gateway/src/guild/guild_visitor_state.erl
@@ -0,0 +1,37 @@
+%% SPDX-License-Identifier: AGPL-3.0-or-later
+
+-module(guild_visitor_state).
+-typing([eqwalizer]).
+
+-export([new/0, get/1, put/2]).
+-export_type([state/0]).
+
+-type state() :: #{
+ sessions := #{session:session_id() => guild_visitors:visitor_session()},
+ refs := #{reference() => session:session_id()},
+ count := non_neg_integer(),
+ by_user := #{session:user_id() => pos_integer()},
+ by_ip := #{binary() => pos_integer()},
+ focus := #{integer() => #{session:session_id() => true}},
+ views := #{{boolean(), boolean()} => #{integer() => true}}
+}.
+
+-spec new() -> state().
+new() ->
+ #{
+ sessions => #{},
+ refs => #{},
+ count => 0,
+ by_user => #{},
+ by_ip => #{},
+ focus => #{},
+ views => #{}
+ }.
+
+-spec get(guild_data:guild_state()) -> state().
+get(#{visitors := Visitors}) when is_map(Visitors) ->
+ Visitors.
+
+-spec put(state(), guild_data:guild_state()) -> guild_data:guild_state().
+put(Visitors, State) ->
+ State#{visitors => Visitors}.
diff --git a/fluxer_gateway/src/guild/visitor_request.erl b/fluxer_gateway/src/guild/visitor_request.erl
new file mode 100644
index 000000000..48f3f7992
--- /dev/null
+++ b/fluxer_gateway/src/guild/visitor_request.erl
@@ -0,0 +1,15 @@
+%% SPDX-License-Identifier: AGPL-3.0-or-later
+
+-module(visitor_request).
+-typing([eqwalizer]).
+
+-export([tier/1, ip_key/1]).
+
+-spec tier(term()) -> binary().
+tier(<<"verified">>) -> <<"verified">>;
+tier(<<"unverified">>) -> <<"unverified">>;
+tier(_) -> <<"unclaimed">>.
+
+-spec ip_key(term()) -> binary() | undefined.
+ip_key(Key) when is_binary(Key), byte_size(Key) > 0 -> Key;
+ip_key(_) -> undefined.