Skip to content

Commit

Permalink
fix: fetch correct pool value from settings by application name
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Dec 2, 2024
1 parent 4eaa930 commit 7a9cb99
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
8 changes: 1 addition & 7 deletions lib/extensions/postgres_cdc_rls/replication_poller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %{
Expand Down
50 changes: 44 additions & 6 deletions lib/realtime/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ defmodule Realtime.Database do
:pass,
:pool,
:queue_target,
:application_name,
ssl_enforced: true,
application_name: "realtime_supabase",
backoff: :rand_exp
]

Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7a9cb99

Please sign in to comment.