Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created December 18, 2017 21:04
Show Gist options
  • Save zorbash/0145626da5cf177cbb4409107e4db760 to your computer and use it in GitHub Desktop.
Save zorbash/0145626da5cf177cbb4409107e4db760 to your computer and use it in GitHub Desktop.
Happy Place - 2
defmodule Example
@pubsub ExampleApp.PubSub
@spec websocket_connections([non_neg_integer]) :: non_neg_integer
def websocket_connections([_ | _] = shards) do
shards
|> Enum.map(&Phoenix.PubSub.Local.local_name(@pubsub, &1))
|> Enum.reduce(0, fn name, acc ->
with pid when is_pid(pid) <- Process.whereis(name),
info when is_list(info) <- Process.info(pid, [:monitors])[:monitors],
procs when is_list(procs) <- info |> Enum.map(&(elem(&1, 1))) do
acc + length(procs)
else
_ -> acc
end
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment