opuntia
is a basic set of tools for traffic shaping for erlang and elixir
It implements the token bucket algorithm.
There are two ways to use it, checking availability a priori or accepting a penalisation.
After creating a bucket
Bucket = opuntia:new(#{bucket_size => 10, rate => 1, time_unit => millisecond, start_full => true}),
you can either consume all tokens queued and see the suggested delay, considering that this might allow you to consume at once much more than the bucket size:
{NewShaper, Delay} = opuntia:update(Shaper, 50),
timer:sleep(Delay), %% Will suggest to sleep 40ms
or you can first how many tokens are available for you to consume before doing so:
Allowed = opuntia:peek(Shaper),
consume_tokens(Allowed),
{NewShaper, 0} = opuntia:update(Shaper), %% Will suggest no delay if you were diligent and consume less that adviced