Beetle.Backend.ETS (beetle v1.0.0) View Source

An ETS backend for Beetle

Note: This backend is suitable for development, testing, and small single-node deployments, but should not be used for production workloads.

The public API of this module is used by Beetle to store information about rate-limit 'buckets'. A bucket is identified by a key, which is a tuple {bucket_number, id}. The essential schema of a bucket is: {key, count, created_at, updated_at}, although backends are free to store and retrieve this data in whichever way they wish.

Use start or start_link to start the server:

{:ok, pid} = Beetle.Backend.ETS.start_link(args)

args is a keyword list:

  • ets_table_name: (atom) table name to use, defaults to :beetle_ets_buckets
  • expiry_ms: (integer) time in ms before a bucket is auto-deleted, should be larger than the expected largest size/duration of a bucket
  • cleanup_interval_ms: (integer) time between cleanup runs,

Example:

Beetle.Backend.ETS.start_link(
  expiry_ms: 1000 * 60 * 60,
  cleanup_interval_ms: 1000 * 60 * 10
)

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Record a hit in the bucket identified by key

Record a hit in the bucket identified by key, with a custom increment

Delete all buckets associated with id.

Retrieve information about the bucket identified by key

Callback implementation for GenServer.init/1.

Link to this section Types

Specs

bucket_info() ::
  {key :: bucket_key(), count :: integer(), created :: integer(),
   updated :: integer()}

Specs

bucket_key() :: {bucket :: integer(), id :: String.t()}

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

count_hit(pid, key, now)

View Source

Specs

count_hit(pid :: pid(), key :: bucket_key(), now :: integer()) ::
  {:ok, count :: integer()} | {:error, reason :: any()}

Record a hit in the bucket identified by key

Link to this function

count_hit(pid, key, now, increment)

View Source

Specs

count_hit(
  pid :: pid(),
  key :: bucket_key(),
  now :: integer(),
  increment :: integer()
) :: {:ok, count :: integer()} | {:error, reason :: any()}

Record a hit in the bucket identified by key, with a custom increment

Specs

delete_buckets(pid :: pid(), id :: String.t()) ::
  {:ok, count_deleted :: integer()} | {:error, reason :: any()}

Delete all buckets associated with id.

Specs

get_bucket(pid :: pid(), key :: bucket_key()) ::
  {:ok, info :: bucket_info()} | {:ok, nil} | {:error, reason :: any()}

Retrieve information about the bucket identified by key

Callback implementation for GenServer.init/1.