Updated Branches:
  refs/heads/import [created] 40777d97c

Use a dict for couch_stats_collector state

This gen_server was falling over in this simple collector. The only real
work it's doing is the keytake function. Most likely a huge list ends up
causing a positive feed back loop as messages pile up. Using a dict
should cut down on the worst case behavior while not being overly harsh
to the opposite work load of very few processes being tracked.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/ced3a698
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/ced3a698
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/ced3a698

Branch: refs/heads/import
Commit: ced3a6980568fa4a53d796da5c791b44d39f1c9c
Parents: 78ca542
Author: Paul J. Davis <[email protected]>
Authored: Wed May 2 16:41:33 2012 -0500
Committer: Paul J. Davis <[email protected]>
Committed: Fri Jan 17 16:44:29 2014 -0800

----------------------------------------------------------------------
 src/couch_stats_collector.erl | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/ced3a698/src/couch_stats_collector.erl
----------------------------------------------------------------------
diff --git a/src/couch_stats_collector.erl b/src/couch_stats_collector.erl
index 0ebca99..99814de 100644
--- a/src/couch_stats_collector.erl
+++ b/src/couch_stats_collector.erl
@@ -92,7 +92,7 @@ track_process_count(Pid, Stat) ->
 init(_) ->
     ets:new(?HIT_TABLE, [named_table, set, public]),
     ets:new(?ABS_TABLE, [named_table, duplicate_bag, public]),
-    {ok, []}.
+    {ok, dict:new()}.
 
 terminate(_Reason, _State) ->
     ok.
@@ -102,13 +102,15 @@ handle_call(stop, _, State) ->
 
 handle_cast({track_process_count, Pid, Stat}, State) ->
     Ref = erlang:monitor(process, Pid),
-    {noreply, [{Ref, Stat} | State]}.
+    {noreply, dict:store(Ref, Stat, State)}.
 
 handle_info({'DOWN', Ref, _, _, _}, State) ->
-    {value, {Ref, Stat}, NewState} = lists:keytake(Ref, 1, State),
+    Stat = dict:fetch(Ref, State),
     ok = couch_stats_collector:decrement(Stat),
-    {noreply, NewState}.
+    {noreply, dict:erase(Ref, State)}.
 
+code_change(_OldVersion, State, _Extra) when is_list(State) ->
+    {ok, dict:from_list(State)};
 code_change(_OldVersion, State, _Extra) ->
     {ok, State}.
 

Reply via email to