Github user iilyak commented on a diff in the pull request:
https://github.com/apache/couchdb-couch-replicator/pull/38#discussion_r62203646
--- Diff: src/couch_replicator_rate_limiter.erl ---
@@ -0,0 +1,219 @@
+-module(couch_replicator_rate_limiter).
+-behaviour(gen_server).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("ibrowse/include/ibrowse.hrl").
+-include_lib("couch_replicator_api_wrap.hrl").
+
+
+-export([
+ start_link/0
+]).
+
+-export([
+ init/1,
+ terminate/2,
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+ code_change/3
+]).
+
+-export ([
+ maybe_start_rate_limiter/3,
+ maybe_decrement_rep_count/1,
+ maybe_delay_request/1
+ ]).
+
+-record(rep_limiter, {
+ requestCounter = 0,
+ lastUpdateTimestamp = 0,
+ pid,
+ replications = 0,
+ limit, % max requests
+ period % interval is in seconds
+}).
+
+
+%% For each unique host, an entry is a rep_limiter record
+-define(HOSTS, couch_replicator_limit_hosts).
+
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+update_host(Host, Limit, Period) ->
+ gen_server:call(?MODULE, {update_host, Host, Limit, Period}, infinity).
+
+
+decrement_replications(Host) ->
+ gen_server:call(?MODULE, {decrement_replications, Host}, infinity).
+
+
+% gen_server functions.
+init([]) ->
+ process_flag(trap_exit, true),
+ ets:new(?HOSTS, [set, public, named_table]),
+ {ok, nil}.
+
+
+handle_call({update_host, Host, Limit, Period}, _From, State) ->
+ Reps = case ets:insert_new(?HOSTS, {Host, #rep_limiter{}}) of
+ true ->
+ % Brand new host for replication, need to spawn
+ % a new resetter process and initialize
+ LoopPid = spawn_link(fun() ->
+ reset_requests_loop(Host, Period)
+ end),
+ modify_host(Host, [{4, LoopPid}, {5, 1}, {6, Limit},
+ {7, Period}]),
+ 1;
+ false ->
+ % this allows the rate limit to be changed during replication
+ modify_host(Host, [{6, Limit}, {7, Period}]),
+ update_rep_count(Host, 1)
+ end,
+ {ok, Reps, State};
+
+
+handle_call({decrement_replications, Host}, _From, State) ->
+ Replications = case update_rep_count(Host, -1) of
+ Rep0 when Rep0 =< 0 ->
+ ResetPid = ets:lookup_element(?HOSTS, Host, 4),
+ % no more replications at this moment, stop resetter
+ % process and remove the host from the ets table
+ exit(ResetPid, stop_resetter),
+ remove_host(Host);
+ _ ->
+ ok % this shoulld be some error
+ end,
+ {ok, Replications, State}.
+
+
+handle_cast(_, State) ->
+ {noreply, State}.
+
+
+handle_info({'EXIT', _FromPid, stop_resetter}, State) ->
+ {noreply, State};
+
+handle_info({'EXIT', _FromPid, Reason}, State) ->
+ couch_log:error("couch_replicator_rate_limiter reset process
+ died abnormally due to: ~p", [Reason]),
+ {noreply, State}.
--- End diff --
Do you want to restart rate_limiter rest process here?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---