refactor(self-hosting): forward every setting, drop dead config (#3047)

This commit is contained in:
Hampus
2026-09-30 00:58:43 +02:00
committed by GitHub
parent 39f9beda5a
commit 2b8a743dc5
96 changed files with 1743 additions and 2391 deletions
+1 -3
View File
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
FROM erlang:28-slim AS build
ARG LOGGER_LEVEL=info
ARG RUST_TOOLCHAIN=1.98.1
ENV PATH="/root/.cargo/bin:${PATH}"
@@ -15,7 +14,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
g++ \
libc6-dev \
gettext-base \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
@@ -29,7 +27,7 @@ COPY . .
WORKDIR /usr/src/app/fluxer_gateway
RUN rebar3 compile --deps_only
RUN LOGGER_LEVEL=${LOGGER_LEVEL} envsubst < config/sys.config.template > config/sys.config && \
RUN cp config/sys.config.template config/sys.config && \
rebar3 as prod release
FROM erlang:28-slim
+2 -2
View File
@@ -50,8 +50,8 @@ clamp_int() {
echo "$value"
}
: "${FLUXER_ERLANG_SCHEDULERS_MIN:=2}"
: "${FLUXER_ERLANG_SCHEDULERS_MAX:=16}"
is_positive_int "${FLUXER_ERLANG_SCHEDULERS_MIN:-}" || FLUXER_ERLANG_SCHEDULERS_MIN=2
is_positive_int "${FLUXER_ERLANG_SCHEDULERS_MAX:-}" || FLUXER_ERLANG_SCHEDULERS_MAX=16
: "${FLUXER_ERLANG_NODE_NAME:[email protected]}"
: "${FLUXER_ERLANG_DIST_PORT:=8081}"
@@ -3,7 +3,7 @@
-module(fluxer_gateway_config).
-typing([eqwalizer]).
-export([load/0, build_config/1, optional_string/1]).
-export([load/0, build_config/1, optional_string/1, env_value/1]).
-export_type([config/0]).
-type config() :: map().
@@ -31,8 +31,7 @@ env_config() ->
<<"internal">> => env_internal_config(),
<<"public">> => env_public_config(),
<<"proxy">> => env_proxy_config(),
<<"services">> => env_services_config(),
<<"telemetry">> => env_telemetry_config()
<<"services">> => env_services_config()
}.
-spec env_internal_config() -> map().
@@ -82,7 +81,7 @@ env_gateway_base_config() ->
<<"push_outbox_request_timeout_ms">> => env_int(
"FLUXER_GATEWAY_PUSH_OUTBOX_REQUEST_TIMEOUT_MS", 100000
),
<<"logger_level">> => env_binary("FLUXER_GATEWAY_LOGGER_LEVEL", <<"info">>),
<<"logger_level">> => env_optional_binary("FLUXER_GATEWAY_LOGGER_LEVEL"),
<<"api_rpc_endpoint">> => env_optional_binary("FLUXER_GATEWAY_API_RPC_ENDPOINT"),
<<"cluster_enabled">> => env_bool("FLUXER_GATEWAY_CLUSTER_ENABLED", false),
<<"cluster_discovery_dns_name">> => env_optional_binary(
@@ -109,7 +108,6 @@ env_gateway_base_config() ->
<<"presence_push_buffer_max_bytes">> => env_int(
"FLUXER_GATEWAY_PRESENCE_PUSH_BUFFER_MAX_BYTES", 1048576
),
<<"shutdown_drain_wait_ms">> => env_int("FLUXER_GATEWAY_SHUTDOWN_DRAIN_WAIT_MS", 5000),
<<"gateway_http_rpc_max_concurrency">> => env_int(
"FLUXER_GATEWAY_HTTP_RPC_MAX_CONCURRENCY", 512
),
@@ -131,19 +129,11 @@ env_nats_config() ->
<<"auth_token">> => env_string("FLUXER_NATS_AUTH_TOKEN", "")
}.
-spec env_telemetry_config() -> map().
env_telemetry_config() ->
#{
<<"enabled">> => env_bool("FLUXER_TELEMETRY_ENABLED", false),
<<"environment">> => env_string("FLUXER_ENV", "development")
}.
-spec build_config(map()) -> config().
build_config(RawConfig) ->
Service = get_map(RawConfig, [<<"services">>, <<"gateway">>]),
Internal = get_map(RawConfig, [<<"internal">>]),
Nats = get_map(RawConfig, [<<"services">>, <<"nats">>]),
Telemetry = get_map(RawConfig, [<<"telemetry">>]),
Proxy = get_map(RawConfig, [<<"proxy">>]),
Public = get_map(RawConfig, [<<"public">>]),
lists:foldl(fun maps:merge/2, #{}, [
@@ -152,7 +142,7 @@ build_config(RawConfig) ->
build_sharding_config(Service),
build_http_config(Service),
build_cluster_config(Service, Public),
build_misc_config(Service, Telemetry)
build_misc_config(Service)
]).
-spec build_core_config(map(), map(), map(), map()) -> config().
@@ -178,9 +168,6 @@ build_core_config(Service, Internal, Nats, Proxy) ->
build_push_config(Service, Public) ->
#{
push_enabled => get_bool(Service, <<"push_enabled">>, true),
push_user_guild_settings_cache_mb =>
get_int(Service, <<"push_user_guild_settings_cache_mb">>, 1024),
push_blocked_ids_cache_mb => get_int(Service, <<"push_blocked_ids_cache_mb">>, 1024),
static_cdn_endpoint => public_endpoint(
get_binary(Service, <<"static_cdn_endpoint">>, <<"http://localhost:8088">>), Public
),
@@ -208,8 +195,7 @@ build_sharding_config(Service) ->
guild_counts_cache_shards => get_optional_int(Service, <<"guild_counts_cache_shards">>),
guild_shards => get_optional_int(Service, <<"guild_shards">>),
session_shards => get_optional_int(Service, <<"session_shards">>),
session_connect_max_queue => get_int(Service, <<"session_connect_max_queue">>, 1024),
shutdown_drain_wait_ms => get_int(Service, <<"shutdown_drain_wait_ms">>, 5000)
session_connect_max_queue => get_int(Service, <<"session_connect_max_queue">>, 1024)
}.
-spec build_http_config(map()) -> config().
@@ -247,19 +233,15 @@ build_cluster_config(Service, Public) ->
)
}.
-spec build_misc_config(map(), map()) -> config().
build_misc_config(Service, Telemetry) ->
-spec build_misc_config(map()) -> config().
build_misc_config(Service) ->
#{
handoff_enable_event_pause => get_bool(
Service, <<"handoff_enable_event_pause">>, false
),
voice_state_counts_sync_interval_ms =>
get_int(Service, <<"voice_state_counts_sync_interval_ms">>, 30000),
logger_level => get_log_level(Service, <<"logger_level">>, warning),
telemetry => #{
enabled => get_bool(Telemetry, <<"enabled">>, true),
environment => get_string(Telemetry, <<"environment">>, "development")
}
logger_level => get_log_level(Service, <<"logger_level">>, info)
}.
-spec get_map(map(), [binary()]) -> map().
@@ -271,21 +253,33 @@ get_map(Map, Keys) ->
-spec env_string(string(), string()) -> string().
env_string(Name, Default) ->
case os:getenv(Name) of
false -> Default;
"" -> Default;
case env_value(Name) of
undefined -> Default;
Value -> Value
end.
-spec env_value(string()) -> string() | undefined.
env_value(Name) ->
case os:getenv(Name) of
false -> undefined;
Value -> non_blank(Value)
end.
-spec non_blank(string()) -> string() | undefined.
non_blank(Value) ->
case string:trim(Value) of
"" -> undefined;
_ -> Value
end.
-spec env_binary(string(), binary()) -> binary().
env_binary(Name, Default) ->
characters_to_binary_or_default(env_string(Name, binary_to_list(Default)), Default).
-spec env_optional_binary(string()) -> binary() | undefined.
env_optional_binary(Name) ->
case os:getenv(Name) of
false -> undefined;
"" -> undefined;
case env_value(Name) of
undefined -> undefined;
Value -> characters_to_binary_or_default(Value, undefined)
end.
@@ -300,27 +294,17 @@ characters_to_binary_or_default(Value, Default) ->
-spec env_int(string(), integer()) -> integer().
env_int(Name, Default) ->
parse_env_int(Name, os:getenv(Name), Default).
case env_value(Name) of
undefined -> Default;
Value -> parse_int(Name, Value)
end.
-spec parse_env_int(string(), false | string(), integer()) -> integer().
parse_env_int(_Name, false, Default) ->
Default;
parse_env_int(_Name, "", Default) ->
Default;
parse_env_int(Name, Value, Default) ->
parse_int(Name, Value, Default).
-spec parse_int(string(), string(), integer()) -> integer().
parse_int(Name, Value, Default) ->
case string:trim(Value) of
"" ->
Default;
Trimmed ->
try list_to_integer(Trimmed) of
Parsed -> Parsed
catch
error:badarg -> erlang:error({invalid_integer_env, Name, Value})
end
-spec parse_int(string(), string()) -> integer().
parse_int(Name, Value) ->
try list_to_integer(string:trim(Value)) of
Parsed -> Parsed
catch
error:badarg -> erlang:error({invalid_integer_env, Name, Value})
end.
-spec env_bool(string(), boolean()) -> boolean().
@@ -60,36 +60,16 @@ build_config() ->
-spec apply_system_config(config()) -> ok.
apply_system_config(Config) ->
apply_logger_config(Config),
store_environment(Config).
-spec apply_logger_config(config()) -> ok.
apply_logger_config(Config) ->
LoggerLevel = resolve_logger_level(Config),
_ = logger:set_primary_config(level, LoggerLevel),
_ = logger:set_handler_config(default, level, LoggerLevel),
ok.
-spec store_environment(config()) -> ok.
store_environment(Config) ->
Telemetry = maps:get(telemetry, Config, #{}),
Environment = maps:get(environment, Telemetry, <<"unknown">>),
EnvBin = ensure_binary(Environment),
persistent_term:put({fluxer_config, environment}, EnvBin),
ok.
-spec ensure_binary(term()) -> binary().
ensure_binary(Value) when is_binary(Value) -> Value;
ensure_binary(Value) when is_list(Value) -> characters_to_binary_or_unknown(Value);
ensure_binary(Value) when is_atom(Value) -> atom_to_binary(Value, utf8);
ensure_binary(_) -> <<"unknown">>.
-spec resolve_logger_level(config()) -> logger_level().
resolve_logger_level(Config) ->
Default = normalize_logger_level(maps:get(logger_level, Config, info)),
case os:getenv("LOGGER_LEVEL") of
false -> Default;
"" -> Default;
case fluxer_gateway_config:env_value("LOGGER_LEVEL") of
undefined -> Default;
Value -> parse_logger_level(Value, Default)
end.
@@ -117,7 +97,3 @@ normalize_logger_level(critical) -> critical;
normalize_logger_level(alert) -> alert;
normalize_logger_level(emergency) -> emergency;
normalize_logger_level(_) -> info.
-spec characters_to_binary_or_unknown(term()) -> binary().
characters_to_binary_or_unknown(Value) ->
type_conv:ensure_binary(Value, <<"unknown">>).
@@ -9,13 +9,9 @@
-spec version() -> binary().
version() ->
case os:getenv("BUILD_VERSION") of
false ->
<<"dev">>;
"" ->
<<"dev">>;
Value ->
ensure_binary(Value)
case fluxer_gateway_config:env_value("BUILD_VERSION") of
undefined -> <<"dev">>;
Value -> ensure_binary(Value)
end.
-spec version_headers(#{binary() => binary()}) -> #{binary() => binary()}.
@@ -523,8 +523,8 @@ pod_name() ->
first_runtime_name([], Fallback) ->
Fallback;
first_runtime_name([Name | Rest], Fallback) ->
case os:getenv(Name) of
false -> first_runtime_name(Rest, Fallback);
case fluxer_gateway_config:env_value(Name) of
undefined -> first_runtime_name(Rest, Fallback);
Value -> normalize_key(Value)
end.
@@ -117,14 +117,12 @@ presence_push_buffer_env_defaults_test() ->
env_only_http_runtime_config_test() ->
with_envs(
[
{"FLUXER_GATEWAY_SHUTDOWN_DRAIN_WAIT_MS", "1234"},
{"FLUXER_GATEWAY_HTTP_RPC_MAX_CONCURRENCY", "42"},
{"FLUXER_GATEWAY_HTTP_FAILURE_THRESHOLD", "9"},
{"FLUXER_GATEWAY_HTTP_RECOVERY_TIMEOUT_MS", "6000"}
],
fun() ->
Config = fluxer_gateway_config:load(),
?assertEqual(1234, maps:get(shutdown_drain_wait_ms, Config)),
?assertEqual(42, maps:get(gateway_http_rpc_max_concurrency, Config)),
?assertEqual(9, maps:get(gateway_http_failure_threshold, Config)),
?assertEqual(6000, maps:get(gateway_http_recovery_timeout_ms, Config))
@@ -158,6 +156,32 @@ env_int_falls_back_to_the_default_for_an_empty_value_test() ->
?assertEqual(512, maps:get(gateway_http_rpc_max_concurrency, Config))
end).
blank_env_values_fall_back_to_the_defaults_test() ->
with_envs(
[
{"FLUXER_GATEWAY_HTTP_RPC_MAX_CONCURRENCY", " "},
{"FLUXER_CLIENT_IP_HEADER_NAME", " "},
{"FLUXER_NATS_URL", "\t"},
{"FLUXER_GATEWAY_API_RPC_ENDPOINT", " "},
{"FLUXER_GATEWAY_LOGGER_LEVEL", " "},
{"FLUXER_GATEWAY_PUSH_ENABLED", " "}
],
fun() ->
Config = fluxer_gateway_config:load(),
?assertEqual(512, maps:get(gateway_http_rpc_max_concurrency, Config)),
?assertEqual(<<"x-forwarded-for">>, maps:get(client_ip_header, Config)),
?assertEqual("nats://nats:4222", maps:get(nats_core_url, Config)),
?assertEqual(undefined, maps:get(api_rpc_endpoint, Config)),
?assertEqual(info, maps:get(logger_level, Config)),
?assertEqual(true, maps:get(push_enabled, Config))
end
).
logger_level_env_test() ->
with_env("FLUXER_GATEWAY_LOGGER_LEVEL", "Debug", fun() ->
?assertEqual(debug, maps:get(logger_level, fluxer_gateway_config:load()))
end).
rpc_concurrency_key_defaults_test() ->
Config = fluxer_gateway_config:build_config(#{}),
?assertEqual(512, maps:get(gateway_nats_rpc_max_handlers, Config)),