Skip to content

Commit

Permalink
Remove shared counters and use simple properties
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Jul 28, 2013
1 parent b045c64 commit a47b5a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
44 changes: 14 additions & 30 deletions lib/poxa/presence_subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ defmodule Poxa.PresenceSubscription do
else
Lager.info('Registering ~p to channel ~p', [self, channel])
{user_id, user_info} = extract_userid_and_userinfo(decoded_channel_data)
if user_id_already_on_presence_channel(user_id, channel) do
:gproc.update_shared_counter({:c, :l, {:presence, channel, user_id}}, 1)
else
unless user_id_already_on_presence_channel(user_id, channel) do
message = PusherEvent.presence_member_added(channel, user_id, user_info)
:gproc.add_shared_local_counter({:presence, channel, user_id}, 1)
:gproc.send({:p, :l, {:pusher, channel}}, {self, message})
end
:gproc.reg({:p, :l, {:pusher, channel}}, {user_id, user_info})
:gproc.mreg(:p, :l, [{{:pusher, channel}, {user_id, user_info}},
{{:presence, channel, user_id}, user_info}])
end
{:presence, channel, :gproc.lookup_values({:p, :l, {:pusher, channel}})}
end
Expand All @@ -44,6 +42,11 @@ defmodule Poxa.PresenceSubscription do
{user_id, user_info}
end

defp user_id_already_on_presence_channel(user_id, channel) do
match = {{:p, :l, {:presence, channel, user_id}}, :_, :_}
:gproc.select_count([{match, [], [true]}]) != 0
end

@spec unsubscribe!(binary) :: :ok
def unsubscribe!(channel) do
case :gproc.get_value({:p, :l, {:pusher, channel}}) do
Expand Down Expand Up @@ -93,22 +96,22 @@ defmodule Poxa.PresenceSubscription do
member_remove_fun = fn([channel, user_id]) ->
if presence_channel?(channel) do
if only_one_connection_on_user_id?(channel, user_id) do
:gproc.unreg_shared({:c, :l, {:presence, channel, user_id}})
message = PusherEvent.presence_member_removed(channel, user_id)
:gproc.send({:p, :l, {:pusher, channel}}, {self, message})
else
:gproc.update_shared_counter({:c, :l, {:presence, channel, user_id}}, -1)
end
end
end
Enum.each(channel_user_id, member_remove_fun)
:ok
end

@spec presence_channel?(any) :: boolean
def presence_channel?("presence-" <> _presence_channel = _channel) do
true
defp only_one_connection_on_user_id?(channel, user_id) do
match = {{:p, :l, {:presence, channel, user_id}}, :_, :_}
:gproc.select_count([{match, [], [true]}]) == 1
end

@spec presence_channel?(any) :: boolean
def presence_channel?("presence-" <> _ = _channel), do: true
def presence_channel?(_), do: false

defp sanitize_user_id(user_id) do
Expand All @@ -117,23 +120,4 @@ defmodule Poxa.PresenceSubscription do
false -> user_id
end
end

defp user_id_already_on_presence_channel(user_id, channel) do
match = {{:p, :l, {:pusher, channel}}, :_, {user_id, :_}}
case :gproc.select([{match, [], [:'$$']}]) do
[] -> false
_ -> true
end
end

defp only_one_connection_on_user_id?(channel, user_id) do
try do
case :gproc.get_value({:c, :l, {:presence, channel, user_id}}, :shared) do
1 -> true
_ -> false
end
rescue
ArgumentError -> false
end
end
end
15 changes: 6 additions & 9 deletions test/presence_subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ defmodule Poxa.PresenceSubscriptionTest do

test "subscribe to a presence channel" do
expect(Subscription, :subscribed?, 1, false)
expect(:gproc, :select, 1, []) # false for user_id_already_on_presence_channel/2
expect(:gproc, :add_shared_local_counter, 2, :ok)
expect(:gproc, :select_count, 1, 0)
expect(:gproc, :send, 2, :sent)
expect(:gproc, :reg, 2, :registered)
expect(:gproc, :mreg, 3, :registered)
expect(PusherEvent, :presence_member_added, 3, :event_message)
expect(:gproc, :lookup_values, 1, :values)
assert subscribe!("presence-channel", "{ \"user_id\" : \"id123\", \"user_info\" : \"info456\" }") == {:presence, "presence-channel", :values}
Expand All @@ -44,9 +43,9 @@ defmodule Poxa.PresenceSubscriptionTest do

test "subscribe to a presence channel having the same userid" do
expect(Subscription, :subscribed?, 1, false)
expect(:gproc, :select, 1, [:something]) # true for user_id_already_on_presence_channel/2
expect(:gproc, :select_count, 1, 1) # true for user_id_already_on_presence_channel/2
expect(:gproc, :update_shared_counter, 2, :ok)
expect(:gproc, :reg, 2, :registered)
expect(:gproc, :mreg, 3, :registered)
expect(:gproc, :lookup_values, 1, :values)
assert subscribe!("presence-channel", "{ \"user_id\" : \"id123\", \"user_info\" : \"info456\" }") == {:presence, "presence-channel", :values}
assert validate Subscription
Expand All @@ -71,8 +70,7 @@ defmodule Poxa.PresenceSubscriptionTest do

test "check subscribed presence channels and remove having only one connection on userid" do
expect(:gproc, :select, 1, [["presence-channel", :userid]])
expect(:gproc, :get_value, 2, 1)
expect(:gproc, :unreg_shared, 1, :ok)
expect(:gproc, :select_count, 1, 1)
expect(PusherEvent, :presence_member_removed, 2, :msg)
expect(:gproc, :send, 2, :ok)
assert check_and_remove == :ok
Expand All @@ -82,8 +80,7 @@ defmodule Poxa.PresenceSubscriptionTest do

test "check subscribed presence channels and remove having more than one connection on userid" do
expect(:gproc, :select, 1, [["presence-channel", :userid]])
expect(:gproc, :get_value, 2, 5)
expect(:gproc, :update_shared_counter, 2, :ok)
expect(:gproc, :select_count, 1, 5)
assert check_and_remove == :ok
assert validate PusherEvent
assert validate :gproc
Expand Down

0 comments on commit a47b5a5

Please sign in to comment.