Skip to content

Commit

Permalink
fix: handle Presence list call timeout without crashing channel (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
w3b6x9 committed Dec 5, 2022
1 parent 4488d17 commit 4af9109
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule RealtimeWeb.RealtimeChannel do
require Logger

alias DBConnection.Backoff
# alias Extensions.Postgres
alias Phoenix.Tracker.Shard
alias RealtimeWeb.{ChannelsAuthorization, Endpoint, Presence}
alias Realtime.{GenCounter, RateCounter, PostgresCdc}

Expand Down Expand Up @@ -226,7 +226,23 @@ defmodule RealtimeWeb.RealtimeChannel do
@impl true
def handle_info(:sync_presence, %{assigns: %{tenant_topic: topic}} = socket) do
socket = assign_counter(socket)
push(socket, "presence_state", Presence.list(topic))

presence_list =
socket
|> presence_list_shard_bypass()
|> case do
{:ok, [_ | _]} ->
Presence.list(topic)

{:ok, []} ->
%{}

{:error, message} ->
Logger.error("Unable to fetch Presence list for topic #{topic}: #{message}")
Presence.list(topic)
end

push(socket, "presence_state", presence_list)

{:noreply, socket}
end
Expand Down Expand Up @@ -257,7 +273,6 @@ defmodule RealtimeWeb.RealtimeChannel do
pg_change_params: pg_change_params,
postgres_extension: postgres_extension,
channel_name: channel_name,
tenant_topic: tenant_topic,
postgres_cdc_module: module
}
} = socket
Expand Down Expand Up @@ -611,4 +626,17 @@ defmodule RealtimeWeb.RealtimeChannel do
channel: channel_name
})
end

defp presence_list_shard_bypass(%{assigns: %{tenant_topic: topic}} = _socket) do
try do
[{:pool_size, size}] = :ets.lookup(Presence, :pool_size)

{:ok,
Presence
|> Shard.name_for_topic(topic, size)
|> Shard.dirty_list(topic)}
rescue
e -> {:error, Exception.message(e)}
end
end
end

0 comments on commit 4af9109

Please sign in to comment.