Skip to content

Commit d2753fe

Browse files
committed
ensure we decrement clients_requesting_changes
ensure we decrement clients_requesting_changes even if the mochiweb process mediating the change request is killed by the client disconnect detector.
1 parent 290314a commit d2753fe

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

src/chttpd/src/chttpd_db.erl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ handle_changes_req1(#httpd{} = Req, Db) ->
151151
mochi = Req,
152152
threshold = Max
153153
},
154-
try
155-
fabric:changes(Db, fun changes_callback/2, Acc0, ChangesArgs)
156-
after
157-
couch_stats:decrement_counter([couchdb, httpd, clients_requesting_changes])
158-
end;
154+
couch_changes_mon:decrement_clients_requesting_changes_on_exit(),
155+
fabric:changes(Db, fun changes_callback/2, Acc0, ChangesArgs);
159156
_ ->
160157
Msg = <<"Supported `feed` types: normal, continuous, live, longpoll, eventsource">>,
161158
throw({bad_request, Msg})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
2+
% use this file except in compliance with the License. You may obtain a copy of
3+
% the License at
4+
%
5+
% http://www.apache.org/licenses/LICENSE-2.0
6+
%
7+
% Unless required by applicable law or agreed to in writing, software
8+
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
% License for the specific language governing permissions and limitations under
11+
% the License.
12+
13+
-module(couch_changes_mon).
14+
15+
-behaviour(gen_server).
16+
17+
-export([decrement_clients_requesting_changes_on_exit/0]).
18+
19+
-export([
20+
start_link/0,
21+
init/1,
22+
handle_call/3,
23+
handle_cast/2,
24+
handle_info/2
25+
]).
26+
27+
decrement_clients_requesting_changes_on_exit() ->
28+
gen_server:cast(?MODULE, {monitor_me, self()}).
29+
30+
start_link() ->
31+
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
32+
33+
init(_) ->
34+
{ok, nil}.
35+
36+
handle_call(_Msg, _From, State) ->
37+
{reply, error, State}.
38+
39+
handle_cast({monitor_me, Pid}, State) ->
40+
monitor(process, Pid),
41+
{noreply, State}.
42+
43+
handle_info({'DOWN', _MonitorRef, process, _Pid, _Info}, State) ->
44+
couch_stats:decrement_counter([couchdb, httpd, clients_requesting_changes]),
45+
{noreply, State};
46+
handle_info(_Msg, State) ->
47+
{noreply, State}.

src/couch/src/couch_httpd_db.erl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,8 @@ handle_changes_req1(Req, Db, ChangesArgs, ChangesFun) ->
189189
couch_stats:increment_counter(
190190
[couchdb, httpd, clients_requesting_changes]
191191
),
192-
try
193-
WrapperFun(ChangesFun)
194-
after
195-
couch_stats:decrement_counter(
196-
[couchdb, httpd, clients_requesting_changes]
197-
)
198-
end.
192+
couch_changes_mon:decrement_clients_requesting_changes_on_exit(),
193+
WrapperFun(ChangesFun).
199194

200195
handle_compact_req(#httpd{method = 'POST'} = Req, Db) ->
201196
case Req#httpd.path_parts of

src/couch/src/couch_secondary_sup.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ init([]) ->
2727
{query_servers, {couch_proc_manager, start_link, []}},
2828
{vhosts, {couch_httpd_vhost, start_link, []}},
2929
{uuids, {couch_uuids, start, []}},
30-
{disk_manager, {couch_disk_monitor, start_link, []}}
30+
{disk_manager, {couch_disk_monitor, start_link, []}},
31+
{couch_changes_mon, {couch_changes_mon, start_link, []}}
3132
] ++ couch_index_servers(),
3233

3334
MaybeHttp =

0 commit comments

Comments
 (0)