summaryrefslogtreecommitdiff
path: root/src/dmon.erl
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2013-06-25 17:43:57 +0100
committerSimon MacMullen <simon@rabbitmq.com>2013-06-25 17:43:57 +0100
commit9f586c509a82b9999034634a30d94f1315967776 (patch)
tree2e8bf09689272d14e6fa5deff86e80dd7efc76c3 /src/dmon.erl
parent3a3df05da017d4738c3d644d23c30baead383326 (diff)
downloadrabbitmq-server-git-9f586c509a82b9999034634a30d94f1315967776.tar.gz
A bit more faff, to deal with genuinely dying nodes.
Diffstat (limited to 'src/dmon.erl')
-rw-r--r--src/dmon.erl43
1 files changed, 31 insertions, 12 deletions
diff --git a/src/dmon.erl b/src/dmon.erl
index dfb420c3a4..3f89c83a56 100644
--- a/src/dmon.erl
+++ b/src/dmon.erl
@@ -16,8 +16,8 @@
-module(dmon).
--export([new/0, monitor/2, monitor_all/2, demonitor/2, is_monitored/2, erase/2,
- monitored/1, is_empty/1]).
+-export([new/0, monitor/2, monitor_all/2, demonitor/2, is_monitored/2,
+ monitored/1, monitored/2, is_empty/1]).
-compile({no_auto_import, [monitor/2]}).
@@ -36,8 +36,8 @@
-spec(monitor_all/2 :: ([item()], ?MODULE()) -> ?MODULE()).
-spec(demonitor/2 :: (item(), ?MODULE()) -> ?MODULE()).
-spec(is_monitored/2 :: (item(), ?MODULE()) -> boolean()).
--spec(erase/2 :: (item(), ?MODULE()) -> ?MODULE()).
-spec(monitored/1 :: (?MODULE()) -> [item()]).
+-spec(monitored/2 :: (node(), ?MODULE()) -> [item()]).
-spec(is_empty/1 :: (?MODULE()) -> boolean()).
-endif.
@@ -45,9 +45,14 @@
new() -> dict:new().
monitor(Item, M) ->
- case dict:is_key(Item, M) of
+ N = case dict:find(node(Item), M) of
+ {ok, N0} -> N0;
+ error -> dict:new()
+ end,
+ case dict:is_key(Item, N) of
true -> M;
- false -> dict:store(Item, delegate:monitor(Item), M)
+ false -> N2 = dict:store(Item, delegate:monitor(Item), N),
+ dict:store(node(Item), N2, M)
end.
monitor_all([], M) -> M; %% optimisation
@@ -55,16 +60,30 @@ monitor_all([Item], M) -> monitor(Item, M); %% optimisation
monitor_all(Items, M) -> lists:foldl(fun monitor/2, M, Items).
demonitor(Item, M) ->
- case dict:find(Item, M) of
- {ok, MRef} -> delegate:demonitor(Item, MRef),
- dict:erase(Item, M);
- error -> M
+ Node = node(Item),
+ case dict:find(Node, M) of
+ {ok, N} -> case dict:find(Item, N) of
+ {ok, MRef} -> delegate:demonitor(Item, MRef),
+ N2 = dict:erase(Item, N),
+ case dict:size(N2) of
+ 0 -> erlang:monitor_node(Node, false),
+ dict:erase(Node, M);
+ _ -> dict:store(Node, N2, M)
+ end;
+ error -> M
+ end;
+ error -> M
end.
-is_monitored(Item, M) -> dict:is_key(Item, M).
+is_monitored(Item, M) -> dict:is_key(node(Item), M) andalso
+ dict:is_key(Item, dict:fetch(node(Item), M)).
-erase(Item, M) -> dict:erase(Item, M).
+monitored(M) -> lists:flatten([dict:fetch_keys(dict:fetch(Node, M)) ||
+ Node <- dict:fetch_keys(M)]).
-monitored(M) -> dict:fetch_keys(M).
+monitored(Node, M) -> case dict:find(Node, M) of
+ {ok, N} -> dict:fetch_keys(N);
+ error -> []
+ end.
is_empty(M) -> dict:size(M) == 0.