Github user iilyak commented on a diff in the pull request:

    
https://github.com/apache/couchdb-couch-replicator/pull/38#discussion_r62202730
  
    --- 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},
    --- End diff --
    
    Don't use magic numbers here.
    Use `[{#rep_limiter.pid, LoopPid}, {#rep_limiter.replications, 1},....]` 
instead


---
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.
---

Reply via email to