Skip to content

Commit

Permalink
fix: Invalidate Cache on delete (#795)
Browse files Browse the repository at this point in the history
Invalidates Tenant Cache on delete.

This needs to be done because if the client connects within the range of normal cache invalidation they will end up creating replication slot which will leave the database with a replication slot that will not be used and potentially block maintenance operations.
  • Loading branch information
filipecabaco authored Mar 2, 2024
1 parent 7d3c388 commit 6beb927
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/realtime_web/controllers/tenant_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule RealtimeWeb.TenantController do

alias Realtime.Api
alias Realtime.Api.Tenant
alias Realtime.Tenants.Cache
alias Realtime.Helpers
alias Realtime.PostgresCdc
alias Realtime.Tenants
Expand Down Expand Up @@ -193,6 +194,7 @@ defmodule RealtimeWeb.TenantController do
PostgresCdc.stop_all(tenant)
Helpers.replication_slot_teardown(tenant)
Api.delete_tenant_by_external_id(tenant_id)
Cache.invalidate_tenant_cache(tenant_id)
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.25.67",
version: "2.25.68",
elixir: "~> 1.14.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
9 changes: 8 additions & 1 deletion test/realtime_web/controllers/tenant_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule RealtimeWeb.TenantControllerTest do

alias Realtime.Api.Tenant
alias Realtime.Helpers
alias Realtime.Tenants.Cache
alias RealtimeWeb.ChannelsAuthorization
alias RealtimeWeb.JwtVerification

Expand Down Expand Up @@ -138,6 +139,8 @@ defmodule RealtimeWeb.TenantControllerTest do

test "deletes chosen tenant", %{conn: conn, tenant: tenant} do
with_mock JwtVerification, verify: fn _token, _secret -> {:ok, %{}} end do
assert Cache.get_tenant_by_external_id(tenant.external_id)

{:ok, db_conn} = Helpers.check_tenant_connection(tenant, "realtime_test")

assert %{rows: [["supabase_realtime_replication_slot"]]} =
Expand All @@ -159,7 +162,11 @@ defmodule RealtimeWeb.TenantControllerTest do
]}
)

{:ok, _} = Postgrex.query(db_conn, "SELECT slot_name FROM pg_replication_slots", [])
{:ok, %{rows: []}} =
Postgrex.query(db_conn, "SELECT slot_name FROM pg_replication_slots", [])

refute Cache.get_tenant_by_external_id(tenant.external_id)

conn = get(conn, Routes.tenant_path(conn, :show, tenant.external_id))
assert response(conn, 404)
end
Expand Down

0 comments on commit 6beb927

Please # to comment.