iilyak commented on a change in pull request #2662: Use `couch_rate` 
application for `couch_view`
URL: https://github.com/apache/couchdb/pull/2662#discussion_r393050752
 
 

 ##########
 File path: src/couch_rate/src/couch_rate_limiter.erl
 ##########
 @@ -0,0 +1,305 @@
+% 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(couch_rate_limiter).
+
+%% This module implements an algorithm to control the consumption rate
+%% parameters such as:
+%%  - batch size
+%%  - delay between batches
+%% The components of the algorithm use
+%%  - [ascending minima 
algorithm](http://web.archive.org/web/20120805114719/http://home.tiac.net/~cri/2001/slidingmin.html)
+%%  - "Welford's method" of calculating average
+
+-export([
+    new/2,
+    from_map/2,
+    budget/2,
+    delay/2,
+    wait/2,
+    in/3,
+    success/3,
+    failure/2,
+    is_congestion/2,
+    min_latency/2,
+    format/2,
+    to_map/2
+]).
+
+-type msec() :: non_neg_integer().
+
+-define(STATE, ?MODULE).
+
+%% This is the number below which the math would not work due to round errors
+%% In particular the default values for thresholds would be equal
+-define(MIN_TARGET, 36).
+
+-define(record_to_keyval(Name, Record),
+    lists:zip(record_info(fields, Name),
+        tl(tuple_to_list(Record)))).
+
+-define(map_to_record(RecordName, Map),
+    element(1, lists:foldl(fun(Field, {Record, Idx}) ->
+        {setelement(Idx, Record, maps:get(Field, Map, element(Idx, Record))), 
Idx + 1}
+    end, {#RecordName{}, 2}, record_info(fields, RecordName)))).
+
+
+-define(record_to_map(RecordName, Record),
+    element(1, lists:foldl(fun(Field, {Map, Idx}) ->
+        {
+            maps:put(Field, element(Idx, Record), Map),
+            Idx + 1
+        }
+    end, {#{}, 2}, record_info(fields, RecordName)))).
+
+-record(?STATE, {
+    window_size = 0 :: pos_integer(),
+    timer = fun now_msec/0,
+    size = 0 :: pos_integer(),
+    epoch = 0 :: pos_integer(),
+    minimums :: queue:queue(),
+    start_ts = undefined,
+    mean_reads = 0.0,
+    mean_writes = 0.0,
+    reads = 0,
+    writes = 0,
+    target = 4500,
+    underload_threshold = 4275, %% target * 0.95
+    overload_threshold = 4725,  %% target * 1.05
+    delay_threshold = 4950, %% target * 1.10
+    multiplicative_factor = 0.7,
+    regular_delay = 100,
+    congested_delay = 5000,
+    initial_budget = 100,
+    latency = 0
+}).
+
+new(_Id, #{sensitivity := S}) when S =< 0 ->
+    error("expected SensitivityTimeWindow > 0");
 
 Review comment:
   I wish we could add emilio into CI. But until I figure out how to setup 
windows system it is not going to happen (see 
https://github.com/apache/couchdb/pull/1944). Thank you for a style nit I'll 
fix it. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to