View Source throttle (util v1.3.4)
Throttle 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
Link to this section 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.
See also: available/2.
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.
See also: curr_rps/2.
Return currently used rate per second.
Create a new throttle given the Rate
per second.
See also: new/3.
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.
See also: reset/2.
Reset the throttle request counter
Return the number of used samples given a_now
current time.
Link to this section Types
-type throttle() :: #throttle{}.
-type throttle_opts() :: #{retries => integer(), retry_delay => integer(), blocking => boolean()}.
retries
- number of retries. retry_delay
- delay in milliseconds between successive retries. blocking
- instructs to block the call if throttled.
-type throttle_result() :: {ok, any()} | {error, throttled | {Reason :: any(), StackTrace :: list()}}.
-type time() :: non_neg_integer().
Link to this section 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.
See also: available/2.
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{}, fun(() -> any()), throttle_opts()) -> {#throttle{}, 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.
See also: curr_rps/2.
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().
See also: new/3.
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.
See also: reset/2.
Reset the throttle request counter
See also: used/2.
Return the number of used samples given a_now
current time.