diff --git a/lib/realtime/tenants/connect.ex b/lib/realtime/tenants/connect.ex index 163742fe6..0e64fb7b2 100644 --- a/lib/realtime/tenants/connect.ex +++ b/lib/realtime/tenants/connect.ex @@ -18,7 +18,7 @@ defmodule Realtime.Tenants.Connect do db_conn_reference: nil, db_conn_pid: nil, check_connected_user_interval: nil, - connected_users_bucket: [1, 1, 1, 1, 1, 1] + connected_users_bucket: [1] @doc """ Returns the database connection for a tenant. If the tenant is not connected, it will attempt to connect to the tenant's database. @@ -152,8 +152,8 @@ defmodule Realtime.Tenants.Connect do defp update_connected_users_bucket(tenant_id, connected_users_bucket) do connected_users_bucket - |> Enum.drop(1) |> then(&(&1 ++ [UsersCounter.tenant_users(tenant_id)])) + |> Enum.take(-6) end defp send_connected_user_check_message( diff --git a/test/realtime/tenants/connect_test.exs b/test/realtime/tenants/connect_test.exs index 4a44eae76..98a2c195e 100644 --- a/test/realtime/tenants/connect_test.exs +++ b/test/realtime/tenants/connect_test.exs @@ -71,11 +71,24 @@ defmodule Realtime.Tenants.ConnectTest do tenant: %{external_id: tenant_id} } do UsersCounter.add(self(), tenant_id) - Connect.lookup_or_start_connection(tenant_id, check_connected_user_interval: 100) + Connect.lookup_or_start_connection(tenant_id, check_connected_user_interval: 10) assert {pid, %{conn: conn_pid}} = :syn.lookup(Connect, tenant_id) :timer.sleep(300) assert {^pid, %{conn: ^conn_pid}} = :syn.lookup(Connect, tenant_id) end + + test "connection is killed after user leaving", %{ + tenant: %{external_id: tenant_id} + } do + UsersCounter.add(self(), tenant_id) + Connect.lookup_or_start_connection(tenant_id, check_connected_user_interval: 10) + + assert {pid, %{conn: conn_pid}} = :syn.lookup(Connect, tenant_id) + :timer.sleep(300) + :syn.leave(:users, tenant_id, self()) + :timer.sleep(300) + assert :undefined = :syn.lookup(Connect, tenant_id) + end end end