Skip to content

Commit

Permalink
fix: Add init warning; increase tenant cache to 6 hours (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Nov 12, 2024
1 parent fed9ba8 commit b8172d2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/realtime/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Realtime.Tenants do
## Examples
iex> Realtime.Tenants.get_health_conn(%Realtime.Api.Tenant{external_id: "not_found_tenant"})
{:error, :not_found}
{:error, :tenant_database_connection_initializing}
"""

@spec get_health_conn(Tenant.t()) :: {:error, term()} | {:ok, pid()}
Expand Down
5 changes: 3 additions & 2 deletions lib/realtime/tenants/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ defmodule Realtime.Tenants.Cache do
require Cachex.Spec

alias Realtime.Tenants

@expiration :timer.hours(6)
def child_spec(_) do
%{
id: __MODULE__,
start:
{Cachex, :start_link, [__MODULE__, [expiration: Cachex.Spec.expiration(default: 30_000)]]}
{Cachex, :start_link,
[__MODULE__, [expiration: Cachex.Spec.expiration(default: @expiration)]]}
}
end

Expand Down
21 changes: 10 additions & 11 deletions lib/realtime/tenants/connect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ defmodule Realtime.Tenants.Connect do
{:ok, conn} ->
{:ok, conn}

{:error, :not_found} ->
{:error, :tenant_database_unavailable} ->
call_external_node(tenant_id, opts)

{:error, :initializing} ->
{:error, :initializing}
{:error, :tenant_database_connection_initializing} ->
:timer.sleep(100)
call_external_node(tenant_id, opts)

{:error, :unknown_error} ->
{:error, :initializing} ->
{:error, :tenant_database_unavailable}
end
end
Expand All @@ -73,26 +74,24 @@ defmodule Realtime.Tenants.Connect do
@spec get_status(binary()) ::
{:ok, pid()}
| {:error,
:unknown_error
:tenant_database_unavailable
| :initializing
| :not_found}
| :tenant_database_connection_initializing}
def get_status(tenant_id) do
case :syn.lookup(__MODULE__, tenant_id) do
{_, %{conn: conn}} when not is_nil(conn) ->
# we have the database connection pid in syn
{:ok, conn}

{_, %{conn: nil}} ->
# syn has registred the tenant process but we haven't updated the registry with the database connection pid
{:error, :initializing}

:undefined ->
# syn hasn't registered anything we need to start the database connection supervision tree
{:error, :not_found}
Logger.warning("Connection process starting up")
{:error, :tenant_database_connection_initializing}

error ->
log_error("SynInitializationError", error)
{:error, :unknown_error}
{:error, :tenant_database_unavailable}
end
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.36",
version: "2.33.37",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit b8172d2

Please sign in to comment.