Commits

Anonymous committed 4e2d390

only calculate eventer list on ring changes, not on each event

Comments (0)

Files changed (1)

src/riak_eventer.erl

 
 -export([notify/1,notify/3,eventer_config/1,do_eventer/1]).
 
--record(state, {ring}).
+-record(state, {ring, eventers}).
 
 %% @private
 start_link() -> gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).
 
 %% @private
-init([]) -> {ok, #state{ring=undefined}}.
+init([]) -> {ok, #state{ring=undefined, eventers=[]}}.
 
 notify(Event) -> gen_server2:cast(?MODULE, {event, Event}).
 
     notify({Module, EventName, node(), EventDetail}).
 
 %% @private
-handle_cast({event, Event}, State) ->
+handle_cast({event, _Event}, State=#state{eventers=[],ring=undefined}) ->
     NewState = ensure_ring(State),
-    Eventers = case riak_ring:get_meta(eventers, NewState#state.ring) of
-        undefined -> [];
-        {ok, X} -> sets:to_list(X)
-    end,
+    {noreply, NewState};
+handle_cast({event, _Event}, State=#state{eventers=[]}) ->
+    {noreply, State};
+handle_cast({event, Event}, State=#state{}) ->
+    NewState = ensure_ring(State),
+    Eventers = NewState#state.eventers,
     [gen_event:notify({riak_event,Node},Event) || Node <- Eventers],
     {noreply, NewState}.
     
-    
 eventer_config([Cluster, CookieStr]) ->
     RipConf = [{no_config, true}, {cluster_name, Cluster},
        {riak_cookie, list_to_atom(CookieStr)}, {ring_state_dir, "<nostore>"},
 ensure_ring(State=#state{ring=undefined}) ->
     riak_ring_manager:subscribe(self()),
     {ok, Ring} = riak_ring_manager:get_my_ring(),
-    State#state{ring=Ring};
+    State#state{ring=Ring, eventers=get_eventers(Ring)};
 ensure_ring(State) -> State.
 
 
+get_eventers(Ring) ->
+    case riak_ring:get_meta(eventers, Ring) of
+        undefined -> [];
+        {ok, X} -> sets:to_list(X)
+    end.        
+
 %% @private
-handle_info({set_ring, Ring}, State) -> {noreply, State#state{ring=Ring}};
+handle_info({set_ring, Ring}, State) -> 
+    {noreply, State#state{ring=Ring, eventers=get_eventers(Ring)}};
 handle_info(_Info, State) -> {noreply, State}.
 
 %% @private