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

 ##########
 File path: src/couch_rate/src/couch_rate_pd.erl
 ##########
 @@ -0,0 +1,111 @@
+% 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_pd).
+
+-include("couch_rate.hrl").
+
+-export([
+    new/3,
+    from_map/3,
+    budget/1,
+    delay/1,
+    wait/1,
+    in/2,
+    success/2,
+    failure/1,
+    is_congestion/1,
+    min_latency/1,
+    format/1,
+    to_map/1,
+    module/1,
+    state/1
+]).
+
+-spec new(couch_rate:id(), module(), map()) -> ok.
+new(Id, Module, Options) ->
+    put(Id, couch_rate:new(Id, Module, Options)),
+    ok.
+
+-spec from_map(couch_rate:id(), module(), map()) -> ok.
+from_map(Id, Module, Map) ->
+    put(Id, couch_rate:from_map(Id, Module, Map)),
+    ok.
+
+-spec budget(couch_rate:id()) -> integer().
+budget(Id) ->
+    dispatch(Id, fun couch_rate:budget/1).
+
+-spec delay(couch_rate:id()) -> integer().
+delay(Id) ->
+    dispatch(Id, fun couch_rate:delay/1).
+
+-spec wait(couch_rate:id()) -> ok.
+wait(Id) ->
+    dispatch(Id, fun couch_rate:wait/1).
+
+-spec in(couch_rate:id(), integer()) -> ok.
+in(Id, Reads) ->
+    dispatch(Id, fun(Limiter) ->
+        {ok, couch_rate:in(Limiter, Reads)}
+    end).
+
+-spec success(couch_rate:id(), integer()) -> ok.
+success(Id, Writes) ->
+    dispatch(Id, fun(Limiter) ->
+        {ok, couch_rate:success(Limiter, Writes)}
+    end).
+
+-spec failure(couch_rate:id()) -> ok.
+failure(Id) ->
+    dispatch(Id, fun couch_rate:failure/1).
+
+-spec is_congestion(couch_rate:id()) -> boolean().
+is_congestion(Id) ->
+    dispatch(Id, fun couch_rate:is_congestion/1).
+
+-spec min_latency(couch_rate:id()) -> pos_integer().
+min_latency(Id) ->
+    dispatch(Id, fun couch_rate:min_latency/1).
+
+-spec format(couch_rate:id()) -> [{Key :: atom(), Value :: term()}].
+format(Id) ->
+    dispatch(Id, fun couch_rate:format/1).
+
+-spec to_map(couch_rate:id()) -> map().
+to_map(Id) ->
+    dispatch(Id, fun couch_rate:to_map/1).
+
+-spec module(couch_rate:id()) -> module().
+module(Id) ->
+    dispatch(Id, fun couch_rate:module/1).
+
+%%-spec state(couch_rate:id()) -> couch_rate:state().
+state(Id) ->
+    dispatch(Id, fun couch_rate:state/1).
+
+dispatch(Id, Fun) ->
+    case get(Id) of
+        #couch_rate{} = Limiter ->
+            case Fun(Limiter) of
+                {Result, #couch_rate{} = State} ->
+                    put(Id, State),
+                    Result;
+                #couch_rate{} = State ->
+                    put(Id, State),
+                    ok;
+                Result ->
+                    Result
+            end;
+        _ ->
+            error("canot find limiter")
 
 Review comment:
   s/canot/cannot

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