throttle (util v1.3.5)
View SourceThrottle given rate over a number of seconds.
Implementation uses time spacing reservation algorithm where each allocation of
samples reserves a fraction of space in the throttling window. The reservation
gets freed as the time goes by. No more than the Rate number of samples are
allowed to fit in the milliseconds Window.
This is an Erlang implementation of the throttling algorithm from the utxx library found at this URL: [https://github.com/saleyn/utxx/blob/master/include/utxx/rate_throttler.hpp]
Author: Serge Aleynikov <saleyn at gmail dot com>
Summary
Types
retries - number of retries. retry_delay - delay in milliseconds between
successive retries. blocking - instructs to block the call if throttled.
Functions
Add one sample to the throttle
Add Samples to the throttle
Add Samples to the throtlle. Return {FitSamples, State}, where FitSamples
are the number of samples that fit in the throttling window. 0 means that the
throttler is fully congested, and more time needs to elapse before the throttles
gets reset to accept more samples.
Return the number of available samples given Now current time.
Call the lambda F, ensuring that it's not called more frequently than the
throttle would allow.
Call the lambda F, ensuring that it's not called more often then the throttle
would allow. Opts are a map of options. When {retries, R} option is given
and R is greater than 0, the throttler will call the function F() up to R
times if the F() raises an exception. The delay between retries is controlled
by the {retry_delay, D} options, expressed in milliseconds (default: 1)
between successive executions of F(). If F() still raises an exception after
the R's retry, that exception would be reraised and it would be the
responsibility of the caller to handle it.
Call M,F,A, ensuring that it's not called more frequently than the throttle would allow.
Call M,F,A, ensuring that it's not called more frequently than the throttle would allow.
Return currently used rate per second.
Create a new throttle given the Rate per second.
Create a new throttle given the Rate per Window milliseconds. Now is
expressesed in microseconds since epoch using now().
Return the number of milliseconds to wait until the throttling threshold is satisfied to fit another sample.
Reset the throttle request counter
Return the number of used samples given a_now current time.
Types
retries - number of retries. retry_delay - delay in milliseconds between
successive retries. blocking - instructs to block the call if throttled.
-type time() :: non_neg_integer().
Functions
Add one sample to the throttle
Add Samples to the throttle
Add Samples to the throtlle. Return {FitSamples, State}, where FitSamples
are the number of samples that fit in the throttling window. 0 means that the
throttler is fully congested, and more time needs to elapse before the throttles
gets reset to accept more samples.
Return the number of available samples given Now current time.
Call the lambda F, ensuring that it's not called more frequently than the
throttle would allow.
Example:
1> T = throttle:new(10, 1000). 2> lists:foldl(fun(_,{T1,A}) -> {T2,R} =
throttle:call(T1, fun() -> http:get("google.com") end), {T2, [R|A]} end, {T,[]},
lists:seq(1, 100)).
-spec call(#throttle{rate :: term(), window :: integer(), step :: integer(), next_ts :: integer()}, fun(() -> any()), throttle_opts()) -> {#throttle{rate :: term(), window :: integer(), step :: integer(), next_ts :: integer()}, throttle_result()}.
Call the lambda F, ensuring that it's not called more often then the throttle
would allow. Opts are a map of options. When {retries, R} option is given
and R is greater than 0, the throttler will call the function F() up to R
times if the F() raises an exception. The delay between retries is controlled
by the {retry_delay, D} options, expressed in milliseconds (default: 1)
between successive executions of F(). If F() still raises an exception after
the R's retry, that exception would be reraised and it would be the
responsibility of the caller to handle it.
Call M,F,A, ensuring that it's not called more frequently than the throttle would allow.
Example:
1> T = throttle:new(10, 1000). 2> lists:foldl(fun(_,{T1,A}) -> {T2,R} =
throttle:call(T1, http, get, ["google.com"]), {T2, [R|A]} end, {T,[]},
lists:seq(1, 100)).
Call M,F,A, ensuring that it's not called more frequently than the throttle would allow.
Return currently used rate per second.
-spec new(non_neg_integer()) -> throttle().
Create a new throttle given the Rate per second.
-spec new(non_neg_integer(), non_neg_integer()) -> throttle().
-spec new(non_neg_integer(), non_neg_integer(), time()) -> throttle().
Create a new throttle given the Rate per Window milliseconds. Now is
expressesed in microseconds since epoch using now().
Return the number of milliseconds to wait until the throttling threshold is satisfied to fit another sample.
Reset the throttle request counter
Return the number of used samples given a_now current time.