ComputeRated (ComputeRated v0.1.1)

View Source

A 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

add_compute_time(id, scale, limit, amount)

@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 bucket
  • scale (Integer) time in ms over which the bucket drains
  • limit (Integer) the max amount of compute time the bucket can hold
  • amount (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}

check_rate(id, scale, limit, amount)

@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 bucket
  • scale (Integer) time in ms over which the bucket drains
  • limit (Integer) the max amount of compute time the bucket can hold
  • amount (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}

delete_bucket(id)

@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

start_link(args \\ [], opts \\ [])

Starts the ComputeRated server.

stop(server)

Stop the rate limit counter server.

wait_for_capacity(id, scale, limit, estimated_cost \\ nil, wait_for_full_depletion \\ false)

@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 bucket
  • scale (Integer) time in ms over which the bucket drains
  • limit (Integer) the max amount of compute time the bucket can hold
  • estimated_cost (Integer, optional) the estimated amount that will be added after waiting
  • wait_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