Module throttle

Authors: Serge Aleynikov (saleyn at gmail dot com).

Description

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

Data Types

throttle()

throttle() = 
    #throttle{rate = any(),
              window = integer(),
              step = integer(),
              next_ts = integer()}

time()

time() = non_neg_integer()

Function Index

add/1Add one sample to the throttle.
add/2Add Samples to the throttle.
add/3Add Samples to the throtlle.
available/1
available/2Return the number of available samples given Now current time.
curr_rps/1
curr_rps/2Return currently used rate per second.
new/1Create a new throttle given the Rate per second.
new/2
new/3Create a new throttle given the Rate per Window milliseconds.
now/0
reset/1
reset/2Reset the throttle request counter.
used/1
used/2Return the number of used samples given a_now current time.

Function Details

add/1

add(T) -> any()

Add one sample to the throttle

add/2

add(T, Samples) -> any()

Add Samples to the throttle

add/3

add(Throttle :: throttle(), Samples :: integer(), Now :: 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.

available/1

available(T) -> any()

See also: available/2.

available/2

available(Throttle, Now) -> any()

Return the number of available samples given Now current time.

curr_rps/1

curr_rps(T) -> any()

See also: curr_rps/2.

curr_rps/2

curr_rps(Throttle, Now) -> any()

Return currently used rate per second.

new/1

new(Rate :: non_neg_integer()) -> throttle()

Create a new throttle given the Rate per second.

new/2

new(Rate :: non_neg_integer(), Window :: non_neg_integer()) ->
       throttle()

See also: new/3.

new/3

new(Rate :: non_neg_integer(),
    Window :: non_neg_integer(),
    Now :: time()) ->
       throttle()

Create a new throttle given the Rate per Window milliseconds. Now is expressesed in microseconds since epoch using now().

now/0

now() -> any()

reset/1

reset(Throttle) -> any()

See also: reset/2.

reset/2

reset(Throttle, Now) -> any()

Reset the throttle request counter

used/1

used(T) -> any()

See also: used/2.

used/2

used(Throttle, Now) -> any()

Return the number of used samples given a_now current time.