View Source throttle (util v1.3.1)
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
.
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
Samples
to the throttleSamples
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.
Now
current time.Call the lambda F
, ensuring that it's not called more frequently than the throttle would allow.
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.
See also: curr_rps/2.
Rate
per second.See also: new/3.
Rate
per Window
milliseconds. Now
is expressesed in microseconds since epoch using now/0
.See also: reset/2.
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
Samples
to the throttle
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.
Now
current time.
Call the lambda F
, ensuring that it's not called more frequently than the throttle would allow.
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()}.
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)).
See also: curr_rps/2.
-spec new(non_neg_integer()) -> throttle().
Rate
per second.
-spec new(non_neg_integer(), non_neg_integer()) -> throttle().
See also: new/3.
Rate
per Window
milliseconds. Now
is expressesed in microseconds since epoch using now/0
.
See also: reset/2.
See also: used/2.
a_now
current time.