Permalink
Browse files

graceful reload of the configuration

  • Loading branch information...
benoitc committed May 3, 2013
1 parent a75ae95 commit df9089581c76e509e2b587dfe9925ceb5784c0db
Showing with 28 additions and 22 deletions.
  1. +16 −10 apps/couch_httpd/src/couch_httpd_config.erl
  2. +12 −12 apps/couch_httpd/src/couch_httpd_protocol.erl
@@ -6,7 +6,7 @@
-export([set_protocol_options/0, get_protocol_options/1,
set_protocol_options/2,
- restart_httpd/0, restart_listener/1,
+ reload_httpd_config/0, reload_listener/1,
start_listener/1, stop_listener/1]).
-export([start_link/0, config_change/2]).
@@ -20,15 +20,20 @@ set_protocol_options() ->
set_protocol_options(Binding, Options)
end, couch_httpd:get_bindings()).
-restart_httpd() ->
+reload_httpd_config() ->
lists:foreach(fun(Binding) ->
- restart_listener(Binding)
+ reload_listener(Binding)
end, couch_httpd:get_bindings()).
-restart_listener(Ref) ->
- stop_listener(Ref),
- supervisor:start_child(ranch_sup, couch_httpd:child_spec(Ref)).
-
+reload_listener(Ref) ->
+ ranch:set_max_connections(Ref, 0),
+ Options = ranch:get_protocol_options(Ref),
+ NbAcceptors = list_to_integer(
+ couch_config:get("httpd", "nb_acceptors", "100")
+ ),
+ {ok, Options} = couch_httpd:get_protocol_options(),
+ ranch:set_protocol_options(Ref, Options),
+ ranch:set_max_connections(Ref, NbAcceptors).
start_listener(Ref) ->
supervisor:start_child(ranch_sup, couch_httpd:child_spec(Ref)).
@@ -56,6 +61,7 @@ start_link() ->
init(_) ->
%% register to config changes
+ process_flag(trap_exit, true),
ok = couch_config:register(fun ?MODULE:config_change/2),
{ok, nil}.
@@ -75,9 +81,9 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
config_change("httpd", "bind_address") ->
- restart_httpd();
+ reload_httpd_config();
config_change("httpd", "port") ->
- restart_listener(http);
+ reload_listener(http);
config_change("httpd", "default_handler") ->
set_protocol_options();
config_change("httpd", "server_options") ->
@@ -91,4 +97,4 @@ config_change("httpd_global_handlers", _) ->
config_change("httpd_db_handlers", _) ->
set_protocol_options();
config_change("ssl", _) ->
- restart_listener(https).
+ reload_listener(https).
@@ -27,8 +27,7 @@
-ifndef(gen_tcp_fix).
-define(R15B_GEN_TCP_FIX, {tcp_error,_,emsgsize} ->
% R15B02 returns this then closes the socket, so close and exit
- Transport:close(Socket),
- exit(normal);
+ terminate(State);
).
-else.
-define(R15B_GEN_TCP_FIX, ).
@@ -46,6 +45,7 @@ start_link(ListenerPid, Socket, Transport, Opts) ->
-spec init(pid(), inet:socket(), module(), any()) -> ok | none().
init(ListenerPid, Socket, Transport, Opts) ->
{loop, HttpLoop} = proplists:lookup(loop, Opts),
+ lager:info("listener pid: ~p~n", [ListenerPid]),
ok = ranch:accept_ack(ListenerPid),
loop(#hstate{socket = Socket,
transport = Transport,
@@ -69,17 +69,14 @@ request(#hstate{transport=Transport, socket=Socket}=State) ->
when Protocol == http orelse Protocol == ssl ->
request(State);
{tcp_closed, _} ->
- Transport:close(Socket),
- exit(normal);
+ terminate(State);
{ssl_closed, _} ->
- Transport:close(Socket),
- exit(normal);
+ terminate(State);
?R15B_GEN_TCP_FIX
_Other ->
handle_invalid_request(State)
after ?REQUEST_RECV_TIMEOUT ->
- Transport:close(Socket),
- exit(normal)
+ terminate(State)
end.
headers(#hstate{transport=Transport, socket=Socket}=State, Request,
@@ -101,14 +98,12 @@ headers(#hstate{transport=Transport, socket=Socket, loop=Loop}=State, Request,
headers(State, Request, [{Name, Value} | Headers],
1 + HeaderCount);
{tcp_closed, _} ->
- Transport:close(Socket),
- exit(normal);
+ terminate(State);
?R15B_GEN_TCP_FIX
_Other ->
handle_invalid_request(State, Request, Headers)
after ?HEADERS_RECV_TIMEOUT ->
- Transport:close(Socket),
- exit(normal)
+ terminate(State)
end.
call_body({M, F, A}, Req) ->
@@ -171,3 +166,8 @@ mochiweb_socket(#hstate{transport=Transport, socket=Socket}) ->
_ ->
Socket
end.
+
+
+terminate(#hstate{transport=Transport, socket=Socket}) ->
+ Transport:close(Socket),
+ exit(normal).

0 comments on commit df90895

Please sign in to comment.