View Source throttle (util v1.1.5)

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

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 throttle would allow.

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

See also: used/2.

Return the number of used samples given a_now current time.

Link to this section Types

-type throttle() :: #throttle{}.
-type time() :: non_neg_integer().

Link to this section Functions

Add one sample to the throttle
Add Samples to the throttle
Link to this function

add(Throttle, Samples, Now)

View Source
-spec add(throttle(), integer(), time()) -> {integer(), 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.

Link to this function

available(Throttle, Now)

View Source
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()), non_neg_integer()) -> {#throttle{}, any()}.
Call the lambda F, ensuring that it's not called more throttle would allow.

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)).
Link to this function

call(Throttle, M, F, A, Now)

View Source
-spec call(#throttle{}, atom(), atom(), [any()], non_neg_integer()) -> {#throttle{}, any()}.
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.

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

See also: reset/2.

Reset the throttle request counter

See also: used/2.

Return the number of used samples given a_now current time.