Browse files

make sure that the vhost server is started.

wait for the vhost server start before to call it.
  • Loading branch information...
1 parent 4a77a1b commit 78d488cbd6a93b2b951d51c79d1ec985e821cb5a @benoitc benoitc committed Dec 8, 2012
View
5 apps/couch/rebar.config
@@ -30,7 +30,10 @@
%% logging
{lager, ".*", {git, "git://github.com/basho/lager.git",
- {branch, "master"}}}
+ {branch, "master"}}},
+
+ {gproc, ".*", {git, "git://github.com/esl/gproc.git", "master"}}
+
]}.
View
2 apps/couch/src/couch.app.src
@@ -17,5 +17,5 @@
{mod, {couch_app, []}},
{applications, [kernel, stdlib, crypto, lager, sasl, public_key, ssl,
- inets, os_mon, couch_stats]}
+ inets, os_mon, couch_stats, gproc]}
]}.
View
31 apps/couch/src/couch_util.erl
@@ -31,6 +31,8 @@
-export([start_app_deps/1, ensure_started/1]).
-export([capitalize/1]).
-export([json_decode/1]).
+-export([register/1, unregister/1, lookup/1, await/1, await/2]).
+
-include("couch_db.hrl").
@@ -462,3 +464,32 @@ json_decode(D) ->
throw:Error ->
throw({invalid_json, Error})
end.
+
+%% @doc Register the local process under a local name
+%% @end
+-spec register(tuple()) -> true.
+register(Name) ->
+ gproc:add_local_name(Name).
+
+%% @doc Unregister the local process from a name
+%% @end
+-spec unregister(tuple()) -> true.
+unregister(Name) ->
+ gproc:unreg({n, l, Name}).
+
+%% @doc Resolve a local name to a pid
+%% @end
+-spec lookup(tuple()) -> pid().
+lookup(Name) ->
+ gproc:lookup_pid({n, l, Name}).
+
+%% @doc Wait until a process registers under a local name
+%% @end
+-spec await(tuple()) -> pid().
+await(Name) ->
+ await(Name, 5000).
+
+-spec await(tuple(), non_neg_integer()) -> pid().
+await(Name, Timeout) ->
+ {Pid, undefined} = gproc:await({n, l, Name}, Timeout),
+ Pid.
View
18 apps/couch_httpd/src/couch_httpd_vhost.erl
@@ -83,10 +83,12 @@ start_link() ->
%% @doc reload vhosts rules
reload() ->
- gen_server:call(?MODULE, reload).
+ Pid = couch_util:await(vhost_server_name()),
+ gen_server:call(Pid, reload).
get_state() ->
- gen_server:call(?MODULE, get_state).
+ Pid = couch_util:await(vhost_server_name()),
+ gen_server:call(Pid, get_state).
%% @doc Try to find a rule matching current Host heade. some rule is
%% found it rewrite the Mochiweb Request else it return current Request.
@@ -329,6 +331,7 @@ make_path(Parts) ->
init(_) ->
ok = couch_config:register(fun ?MODULE:config_change/2),
+ true = register_vhost_server(),
%% load configuration
{VHostGlobals, VHosts, Fun} = load_conf(),
@@ -356,6 +359,7 @@ handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
+ unregister_vhost_server(),
ok.
code_change(_OldVsn, State, _Extra) ->
@@ -382,3 +386,13 @@ load_conf() ->
"redirect_vhost_handler", DefaultVHostFun)),
{VHostGlobals, VHosts, Fun}.
+
+
+vhost_server_name() ->
+ {couch, vhosts_server}.
+
+register_vhost_server() ->
+ couch_util:register(vhost_server_name()).
+
+unregister_vhost_server() ->
+ couch_util:unregister(vhost_server_name()).

0 comments on commit 78d488c

Please sign in to comment.