ComputeRated (ComputeRated v0.1.0)
View SourceA leaky bucket rate limiter optimized for compute time limits.
This module provides a rate limiter that tracks compute time usage and allows checking and waiting for available capacity. It's based on the leaky bucket algorithm where compute time gradually drains from the bucket over time.
Summary
Functions
Add compute time to the bucket.
Check if adding the specified amount of compute time would exceed the bucket's limit.
Delete bucket to reset the compute time counter.
Starts the ComputeRated server.
Stop the rate limit counter server.
Sleep until there's enough capacity in the bucket for the specified amount, or optionally until the bucket is fully depleted.
Functions
@spec add_compute_time( id :: any(), scale :: integer(), limit :: integer(), amount :: integer() ) :: {:ok, current :: integer(), remaining :: integer()}
Add compute time to the bucket.
Arguments:
id
(Erlang term()) name of the bucketscale
(Integer) time in ms over which the bucket drainslimit
(Integer) the max amount of compute time the bucket can holdamount
(Integer) the amount of compute time to add
Examples
# Add 100 units of compute time to a bucket
iex> ComputeRated.add_compute_time("my-bucket", 60_000, 1000, 100)
{:ok, 100, 900}
@spec check_rate( id :: any(), scale :: integer(), limit :: integer(), amount :: integer() ) :: {:ok, current :: integer(), remaining :: integer()} | {:error, current :: integer(), remaining :: integer()}
Check if adding the specified amount of compute time would exceed the bucket's limit.
Arguments:
id
(Erlang term()) name of the bucketscale
(Integer) time in ms over which the bucket drainslimit
(Integer) the max amount of compute time the bucket can holdamount
(Integer) the amount of compute time to check for
Examples
# Check if adding 100 units of compute time would exceed a bucket with limit 1000
iex> ComputeRated.check_rate("my-bucket", 60_000, 1000, 100)
{:ok, 100, 900}
# When it would exceed the limit
iex> ComputeRated.check_rate("my-bucket", 60_000, 1000, 1100)
{:error, 1000, 0}
@spec delete_bucket(id :: String.t()) :: :ok | :error
Delete bucket to reset the compute time counter.
Arguments:
id
(String) name of the bucket
Example - Reset counter for my-bucket
iex> ComputeRated.delete_bucket("my-bucket")
:ok
Starts the ComputeRated server.
Stop the rate limit counter server.
@spec wait_for_capacity( id :: any(), scale :: integer(), limit :: integer(), estimated_cost :: integer() | nil, wait_for_full_depletion :: boolean() ) :: :ok
Sleep until there's enough capacity in the bucket for the specified amount, or optionally until the bucket is fully depleted.
Arguments:
id
(Erlang term()) name of the bucketscale
(Integer) time in ms over which the bucket drainslimit
(Integer) the max amount of compute time the bucket can holdestimated_cost
(Integer, optional) the estimated amount that will be added after waitingwait_for_full_depletion
(Boolean, optional) if true, waits until bucket is empty
Examples
# Wait until there's enough capacity for 100 units
iex> ComputeRated.wait_for_capacity("my-bucket", 60_000, 1000, 100)
:ok
# Wait until the bucket is completely empty
iex> ComputeRated.wait_for_capacity("my-bucket", 60_000, 1000, nil, true)
:ok