From 3d54b88f0a9afdb3b4e31ac9be2352d7d2c77725 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 21 Sep 2022 12:51:07 +0300 Subject: [PATCH] Make Finch pools lighter for self-hosting (#2250) * Make Finch pools lighter * Use standard http1 Finch pools --- config/runtime.exs | 6 ---- lib/plausible/application.ex | 70 +++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 5b01a1c26..0730f7861 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -372,12 +372,6 @@ config :plausible, :hcaptcha, sitekey: hcaptcha_sitekey, secret: hcaptcha_secret -config :plausible, Plausible.Finch, - default_pool_size: get_int_from_path_or_env(config_dir, "DEFAULT_FINCH_POOL_SIZE", 50), - default_pool_count: get_int_from_path_or_env(config_dir, "DEFAULT_FINCH_POOL_COUNT", 1), - sentry_pool_size: get_int_from_path_or_env(config_dir, "SENTRY_FINCH_POOL_SIZE", 50), - sentry_pool_count: get_int_from_path_or_env(config_dir, "SENTRY_FINCH_POOL_COUNT", 1) - config :plausible, Plausible.Sentry.Client, finch_request_opts: [ pool_timeout: get_int_from_path_or_env(config_dir, "SENTRY_FINCH_POOL_TIMEOUT", 5000), diff --git a/lib/plausible/application.ex b/lib/plausible/application.ex index 579740b42..c0a8b9da9 100644 --- a/lib/plausible/application.ex +++ b/lib/plausible/application.ex @@ -41,41 +41,53 @@ defmodule Plausible.Application do end defp finch_pool_config() do - config = Application.fetch_env!(:plausible, Plausible.Finch) - - pool_config = %{ - :default => [size: config[:default_pool_size], count: config[:default_pool_count]], - "https://vendors.paddle.com" => [ - protocol: :http2, - count: 50, - conn_opts: [transport_opts: [timeout: 15_000]] - ], - "https://www.googleapis.com" => [ - protocol: :http2, - count: 200, - conn_opts: [transport_opts: [timeout: 15_000]] - ], - "https://analyticsreporting.googleapis.com" => [ - protocol: :http2, - count: 200, - conn_opts: [transport_opts: [timeout: 15_000]] - ], + base_config = %{ "https://icons.duckduckgo.com" => [ - protocol: :http2, - count: 100, conn_opts: [transport_opts: [timeout: 15_000]] ] } - sentry_dsn = Application.get_env(:sentry, :dsn) + base_config + |> maybe_add_sentry_pool() + |> maybe_add_paddle_pool() + |> maybe_add_google_pools() + end + + defp maybe_add_sentry_pool(pool_config) do + case Application.get_env(:sentry, :dsn) do + dsn when is_binary(dsn) -> + Map.put(pool_config, dsn, size: 50) + + _ -> + pool_config + end + end + + defp maybe_add_paddle_pool(pool_config) do + paddle_conf = Application.get_env(:plausible, :paddle) + + cond do + paddle_conf[:vendor_id] && paddle_conf[:vendor_auth_code] -> + Map.put(pool_config, Plausible.Billing.PaddleApi.vendors_domain(), + conn_opts: [transport_opts: [timeout: 15_000]] + ) + + true -> + pool_config + end + end + + defp maybe_add_google_pools(pool_config) do + google_conf = Application.get_env(:plausible, :google) + + cond do + google_conf[:client_id] && google_conf[:client_secret] -> + pool_config + |> Map.put(google_conf[:api_url], conn_opts: [transport_opts: [timeout: 15_000]]) + |> Map.put(google_conf[:reporting_api_url], conn_opts: [transport_opts: [timeout: 15_000]]) - if is_binary(sentry_dsn) do - Map.put(pool_config, sentry_dsn, - size: config[:sentry_pool_size], - count: config[:sentry_pool_count] - ) - else - pool_config + true -> + pool_config end end