You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by bb...@apache.org on 2017/03/08 20:39:30 UTC
[6/6] couch-index commit: updated refs/heads/8409-view-cache to
5a9f05c
[squash] change couch_index_monitor to be gen_server
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/commit/5a9f05c9
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/tree/5a9f05c9
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/diff/5a9f05c9
Branch: refs/heads/8409-view-cache
Commit: 5a9f05c9f6ad8e8240cf4a4c453e89db5abcfcd7
Parents: 0ad8d9d
Author: Benjamin Bastian <be...@gmail.com>
Authored: Wed Mar 8 12:39:18 2017 -0800
Committer: Benjamin Bastian <be...@gmail.com>
Committed: Wed Mar 8 12:39:18 2017 -0800
----------------------------------------------------------------------
src/couch_index_monitor.erl | 82 ++++++++++++++++++++--------------------
src/couch_index_server.erl | 2 +-
2 files changed, 43 insertions(+), 41 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/blob/5a9f05c9/src/couch_index_monitor.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_monitor.erl b/src/couch_index_monitor.erl
index fc2fef1..a881c7b 100644
--- a/src/couch_index_monitor.erl
+++ b/src/couch_index_monitor.erl
@@ -12,9 +12,11 @@
-module(couch_index_monitor).
+-behaviour(gen_server).
+
-export([
- spawn_link/1,
+ start_link/1,
close/1,
set_pid/2,
@@ -24,7 +26,12 @@
]).
-export([
- init/1
+ init/1,
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+ code_change/3,
+ terminate/2
]).
@@ -40,18 +47,16 @@
}).
-spawn_link(Name) ->
- erlang:spawn_link(?MODULE, init, [Name]).
+start_link(Name) ->
+ gen_server:start_link(?MODULE, [Name], []).
close(Monitor) ->
- Monitor ! exit,
- ok.
+ gen_server:cast(Monitor, exit).
set_pid(Monitor, Pid) ->
- Monitor ! {set_pid, Pid},
- ok.
+ gen_server:cast(Monitor, {set_pid, Pid}).
notify(Monitor) ->
@@ -59,8 +64,7 @@ notify(Monitor) ->
notify(Monitor, Client) when is_pid(Client) ->
- Monitor ! {notify, Client},
- ok;
+ gen_server:cast(Monitor, {notify, Client});
notify(Monitor, {Client, _}) when is_pid(Client) ->
notify(Monitor, Client).
@@ -68,7 +72,7 @@ notify(Monitor, {Client, _}) when is_pid(Client) ->
cancel(Name, {Client, Monitor})
when Client == self(), is_pid(Monitor) ->
- Monitor ! {cancel, self()},
+ gen_server:cast(Monitor, {cancel, self()}),
case (catch ets:update_counter(?BY_COUNTERS, Name, -1)) of
0 ->
true = ets:insert(?BY_IDLE, {Name}),
@@ -78,28 +82,32 @@ cancel(Name, {Client, Monitor})
end.
-init(Name) ->
+init([Name]) ->
{ok, CRefs} = khash:new(),
- loop(#st{
+ {ok, #st{
name = Name,
ref = undefined,
client_refs = CRefs,
closing = false
- }).
+ }}.
+
+
+handle_call(Msg, From, St) ->
+ {stop, {unknown_call, Msg, From}, St}.
-handle_info(exit, St) ->
- {stop, normal, St};
+handle_cast(exit, St) ->
+ {stop, shutdown, St};
-handle_info({set_pid, Pid}, #st{ref = undefined} = St) ->
+handle_cast({set_pid, Pid}, #st{ref = undefined} = St) ->
Ref = erlang:monitor(process, Pid),
{noreply, St#st{ref = Ref}};
-handle_info({set_pid, Pid}, #st{ref = Ref} = St) when is_reference(Ref) ->
+handle_cast({set_pid, Pid}, #st{ref = Ref} = St) when is_reference(Ref) ->
erlang:demonitor(Ref, [flush]),
- handle_info({set_pid, Pid}, St#st{ref = undefined});
+ handle_cast({set_pid, Pid}, St#st{ref = undefined});
-handle_info({notify, Client}, St) when is_pid(Client) ->
+handle_cast({notify, Client}, St) when is_pid(Client) ->
case khash:get(St#st.client_refs, Client) of
{Ref, Count} when is_reference(Ref), is_integer(Count), Count > 0 ->
khash:put(St#st.client_refs, Client, {Ref, Count + 1});
@@ -118,7 +126,7 @@ handle_info({notify, Client}, St) when is_pid(Client) ->
end,
{noreply, St};
-handle_info({cancel, Client}, St) when is_pid(Client) ->
+handle_cast({cancel, Client}, St) when is_pid(Client) ->
case khash:get(St#st.client_refs, Client) of
{Ref, 1} when is_reference(Ref) ->
erlang:demonitor(Ref, [flush]),
@@ -129,8 +137,12 @@ handle_info({cancel, Client}, St) when is_pid(Client) ->
end,
{noreply, St};
+handle_cast(Msg, St) ->
+ {stop, {unknown_cast, Msg}, St}.
+
+
handle_info({'DOWN', Ref, process, _, _}, #st{ref = Ref} = St) ->
- {stop, normal, St};
+ {stop, shutdown, St};
handle_info({'DOWN', _Ref, process, Pid, _Reason}, St) ->
#st{name=Name} = St,
@@ -149,6 +161,14 @@ handle_info(Msg, St) ->
{stop, {bad_info, Msg}, St}.
+code_change(_OldVsn, St, _Extra) ->
+ {ok, St}.
+
+
+terminate(_Reason, _St) ->
+ ok.
+
+
maybe_set_idle(St) ->
case khash:size(St#st.client_refs) of
0 ->
@@ -158,21 +178,3 @@ maybe_set_idle(St) ->
% We have other clients
ok
end.
-
-
-loop(St) ->
- receive
- Other ->
- do_handle_info(Other, St)
- end.
-
-
-do_handle_info(Msg, St) ->
- try handle_info(Msg, St) of
- {noreply, NewSt} ->
- loop(NewSt);
- {stop, Reason, _NewSt} ->
- exit(Reason)
- catch T:R ->
- exit({T, R})
- end.
http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/blob/5a9f05c9/src/couch_index_server.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_server.erl b/src/couch_index_server.erl
index f191700..981eb96 100644
--- a/src/couch_index_server.erl
+++ b/src/couch_index_server.erl
@@ -185,7 +185,7 @@ handle_call({get_index, {_Mod, _IdxState, DbName, Sig}=Args}, From, State) ->
[] ->
{ok, NewState} = maybe_close_idle(State),
spawn_link(fun() -> new_index(Args) end),
- Monitor = couch_index_monitor:spawn_link({DbName, Sig}),
+ {ok, Monitor} = couch_index_monitor:start_link({DbName, Sig}),
ets:insert(?BY_SIG, {{DbName, Sig}, {[From], Monitor}}),
{noreply, NewState};
[{_, {Waiters, Monitor}}] when is_list(Waiters) ->