Repository: couchdb-chttpd Updated Branches: refs/heads/master 663273b0e -> 9e0ad343c
Update handle_config_terminate API COUCHDB-3102 Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/4f7dcc31 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/4f7dcc31 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/4f7dcc31 Branch: refs/heads/master Commit: 4f7dcc319c8c43a7330f5e19414717db1343fdd9 Parents: 663273b Author: ILYA Khlopotov <iil...@ca.ibm.com> Authored: Wed Aug 17 11:37:54 2016 -0700 Committer: ILYA Khlopotov <iil...@ca.ibm.com> Committed: Tue Aug 23 12:22:50 2016 -0700 ---------------------------------------------------------------------- src/chttpd_config_listener.erl | 59 ------------------------------------- src/chttpd_sup.erl | 47 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 61 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/4f7dcc31/src/chttpd_config_listener.erl ---------------------------------------------------------------------- diff --git a/src/chttpd_config_listener.erl b/src/chttpd_config_listener.erl deleted file mode 100644 index 8506524..0000000 --- a/src/chttpd_config_listener.erl +++ /dev/null @@ -1,59 +0,0 @@ -% Licensed under the Apache License, Version 2.0 (the "License"); you may not -% use this file except in compliance with the License. You may obtain a copy of -% the License at -% -% http://www.apache.org/licenses/LICENSE-2.0 -% -% Unless required by applicable law or agreed to in writing, software -% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -% License for the specific language governing permissions and limitations under -% the License. - --module(chttpd_config_listener). --vsn(2). --behaviour(config_listener). - -% public interface --export([subscribe/0]). - -% config_listener callback --export([handle_config_change/5, handle_config_terminate/3]). - -subscribe() -> - Settings = [ - {bind_address, config:get("chttpd", "bind_address")}, - {port, config:get("chttpd", "port")}, - {backlog, config:get("chttpd", "backlog")}, - {server_options, config:get("chttpd", "server_options")} - ], - ok = config:listen_for_changes(?MODULE, Settings), - ok. - -handle_config_change("chttpd", "bind_address", Value, _, Settings) -> - maybe_replace(bind_address, Value, Settings); -handle_config_change("chttpd", "port", Value, _, Settings) -> - maybe_replace(port, Value, Settings); -handle_config_change("chttpd", "backlog", Value, _, Settings) -> - maybe_replace(backlog, Value, Settings); -handle_config_change("chttpd", "server_options", Value, _, Settings) -> - maybe_replace(server_options, Value, Settings); -handle_config_change(_, _, _, _, Settings) -> - {ok, Settings}. - -handle_config_terminate(_, stop, _) -> ok; -handle_config_terminate(_Server, _Reason, State) -> - spawn(fun() -> - timer:sleep(5000), - config:listen_for_changes(?MODULE, State) - end). - -% private -maybe_replace(Key, Value, Settings) -> - case couch_util:get_value(Key, Settings) of - Value -> - {ok, Settings}; - _ -> - chttpd:stop(), - {ok, lists:keyreplace(Key, 1, Settings, {Key, Value})} - end. http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/4f7dcc31/src/chttpd_sup.erl ---------------------------------------------------------------------- diff --git a/src/chttpd_sup.erl b/src/chttpd_sup.erl index 07dde48..fe84b67 100644 --- a/src/chttpd_sup.erl +++ b/src/chttpd_sup.erl @@ -12,10 +12,16 @@ -module(chttpd_sup). -behaviour(supervisor). +-vsn(1). + +-behaviour(config_listener). + -export([init/1]). -export([start_link/1]). +-export([handle_config_change/5, handle_config_terminate/3]). + %% Helper macro for declaring children of supervisor -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 100, Type, [I]}). @@ -23,9 +29,15 @@ start_link(Args) -> supervisor:start_link({local,?MODULE}, ?MODULE, Args). init([]) -> - chttpd_config_listener:subscribe(), - Children = [ + { + config_listener_mon, + {config_listener_mon, start_link, [?MODULE, settings()]}, + permanent, + 5000, + worker, + [config_listener_mon] + }, ?CHILD(chttpd, worker), ?CHILD(chttpd_auth_cache, worker), {chttpd_auth_cache_lru, @@ -36,6 +48,37 @@ init([]) -> {ok, {{one_for_one, 3, 10}, couch_epi:register_service(chttpd_epi, Children)}}. +handle_config_change("chttpd", "bind_address", Value, _, Settings) -> + maybe_replace(bind_address, Value, Settings); +handle_config_change("chttpd", "port", Value, _, Settings) -> + maybe_replace(port, Value, Settings); +handle_config_change("chttpd", "backlog", Value, _, Settings) -> + maybe_replace(backlog, Value, Settings); +handle_config_change("chttpd", "server_options", Value, _, Settings) -> + maybe_replace(server_options, Value, Settings); +handle_config_change(_, _, _, _, Settings) -> + {ok, Settings}. + +handle_config_terminate(_Server, _Reason, _State) -> + ok. + +settings() -> + [ + {bind_address, config:get("chttpd", "bind_address")}, + {port, config:get("chttpd", "port")}, + {backlog, config:get("chttpd", "backlog")}, + {server_options, config:get("chttpd", "server_options")} + ]. + +maybe_replace(Key, Value, Settings) -> + case couch_util:get_value(Key, Settings) of + Value -> + {ok, Settings}; + _ -> + chttpd:stop(), + {ok, lists:keyreplace(Key, 1, Settings, {Key, Value})} + end. + lru_opts() -> case config:get("chttpd_auth_cache", "max_objects") of MxObjs when is_integer(MxObjs), MxObjs > 0 ->