Skip to content

Commit

Permalink
change bucket logic
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Oct 20, 2023
1 parent c0e5045 commit 776dcd3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/realtime/tenants/connect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 14 additions & 1 deletion test/realtime/tenants/connect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 776dcd3

Please sign in to comment.