From 7a9cb996d107c5aba146e24fe77d0c09b1f04f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20Caba=C3=A7o?= Date: Mon, 2 Dec 2024 22:03:47 +0000 Subject: [PATCH] fix: fetch correct pool value from settings by application name --- .../postgres_cdc_rls/replication_poller.ex | 8 +-- lib/realtime/database.ex | 50 ++++++++++++++++--- mix.exs | 2 +- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/lib/extensions/postgres_cdc_rls/replication_poller.ex b/lib/extensions/postgres_cdc_rls/replication_poller.ex index aef4d2172..052f1beda 100644 --- a/lib/extensions/postgres_cdc_rls/replication_poller.ex +++ b/lib/extensions/postgres_cdc_rls/replication_poller.ex @@ -28,13 +28,7 @@ defmodule Extensions.PostgresCdcRls.ReplicationPoller do def init(args) do tenant = args["id"] Logger.metadata(external_id: tenant, project: tenant) - - # higher number of pool connections leads to issues - realtime_rls_settings = - args - |> Database.from_settings("realtime_rls") - |> Map.put(:pool, 1) - + realtime_rls_settings = Database.from_settings(args, "realtime_rls") {:ok, conn} = Database.connect_db(realtime_rls_settings) state = %{ diff --git a/lib/realtime/database.ex b/lib/realtime/database.ex index 01d0aeaaf..8532eeb5a 100644 --- a/lib/realtime/database.ex +++ b/lib/realtime/database.ex @@ -17,8 +17,8 @@ defmodule Realtime.Database do :pass, :pool, :queue_target, + :application_name, ssl_enforced: true, - application_name: "realtime_supabase", backoff: :rand_exp ] @@ -39,9 +39,14 @@ defmodule Realtime.Database do @spec from_settings(map(), binary(), :stop | :exp | :rand | :rand_exp, boolean()) :: Realtime.Database.t() - def from_settings(settings, application_name, backoff \\ :rand_exp, decrypt \\ false) do - pool = - settings["subs_pool_size"] || settings["subcriber_pool_size"] || settings["db_pool"] || 1 + def from_settings( + settings, + application_name, + backoff \\ :rand_exp, + decrypt \\ false, + pool \\ nil + ) do + pool = pool_size_by_application_name(application_name, settings, pool) settings = if decrypt do @@ -68,6 +73,40 @@ defmodule Realtime.Database do } end + @doc """ + Returns the pool size for a given application name. Override pool size if provided. + + ## Examples + + iex> Realtime.Database.pool_size_by_application_name("realtime_rls", %{}, 1) + 1 + + iex> Realtime.Database.pool_size_by_application_name("realtime_rls", %{"db_pool" => 10}, 1) + 1 + + iex> Realtime.Database.pool_size_by_application_name("realtime_rls", %{"db_pool" => 10}, nil) + 10 + + """ + @spec pool_size_by_application_name(binary(), map(), non_neg_integer()) :: non_neg_integer() + def pool_size_by_application_name(application_name, settings, override_pool \\ nil) do + pool = + case application_name do + "realtime_subscription_manager" -> settings["subcriber_pool_size"] + "realtime_subscription_manager_pub" -> settings["subs_pool_size"] + "realtime_subscription_checker" -> settings["subs_pool_size"] + "realtime_rls" -> settings["db_pool"] + "realtime_health_check" -> settings["db_pool"] + "realtime_broadcast_changes" -> 1 + "realtime_migrations" -> 2 + "realtime_janitor" -> 1 + "realtime_connect" -> 1 + _ -> 1 + end + + if override_pool, do: override_pool, else: pool + end + @spec from_tenant( Realtime.Api.Tenant.t(), binary(), @@ -147,8 +186,7 @@ defmodule Realtime.Database do def connect(tenant, application_name, pool, backoff \\ :stop) do tenant |> then(&Realtime.PostgresCdc.filter_settings(@cdc, &1.extensions)) - |> then(&Realtime.Database.from_settings(&1, application_name, backoff)) - |> then(&%{&1 | pool: pool}) + |> then(&Realtime.Database.from_settings(&1, application_name, backoff, false, pool)) |> connect_db() end diff --git a/mix.exs b/mix.exs index 9e3ca8086..9788154da 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do def project do [ app: :realtime, - version: "2.33.62", + version: "2.33.63", elixir: "~> 1.17.3", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod,