ExAgent.RateLimiter.Postgres (ExAgent v3.0.0)

View Source

Shared provider admission on the host's PostgreSQL Ecto Repo.

Stability — Stable.

handle = ExAgent.RateLimiter.Postgres.new(MyApp.Repo, scope: "provider-account-1")

{ExAgent.RateLimiter,
 name: MyApp.Quota,
 limits: %{"openai" => [rpm: 60, tpm: 100_000, concurrency: 4]},
 shared: [adapter: {ExAgent.RateLimiter.Postgres, handle}]}

Apply ExAgent.Migration in a transactional host migration on the same search path. A confirmed missing quota schema returns {:error, {:rate_limiter_migration_required, 1}}. Use one stable scope per account with identical key granularity and limits on every replica. Start the Repo before accepting runs. The adapter uses the Repo's registered pool and READ COMMITTED transactions.

PostgreSQL serializes each bucket and sets expiry using its own clock. An unsettled permit occupies concurrency until expiry and charges RPM and estimated TPM through one minute after expiry. A terminal releases concurrency and shortens the rolling charge to at most one minute from settlement. An explicitly unused permit releases all capacity. Retries of a settlement preserve the first report and its cooldown. Schedule cleanup_expired/2 to remove expired evidence in bounded batches.

Summary

Functions

Removes up to limit expired attempts and limit expired cooldowns across the Repo.

Statements retained for host migration version: 1 and repair. For new installations use ExAgent.Migration. An unsupported version or direction raises ArgumentError. Reapply the up statements in a locked host migration to restore missing columns and indexes and the settled column's NOT NULL DEFAULT false. Surviving charges are retained, and missing data can extend quota waits conservatively. Down deletes all quota evidence.

Builds a handle scoped to a nonempty provider account identifier.

Types

t()

@opaque t()

Functions

cleanup_expired(repo, limit \\ 100)

@spec cleanup_expired(module(), 1..1000) ::
  {:ok, non_neg_integer()} | {:error, :rate_limiter_unavailable}

Removes up to limit expired attempts and limit expired cooldowns across the Repo.

migration_sql(direction, opts)

This function is deprecated. Use ExAgent.Migration for new migrations. Removal in 4.0..
@spec migration_sql(:up | :down, keyword()) :: [String.t()]

Statements retained for host migration version: 1 and repair. For new installations use ExAgent.Migration. An unsupported version or direction raises ArgumentError. Reapply the up statements in a locked host migration to restore missing columns and indexes and the settled column's NOT NULL DEFAULT false. Surviving charges are retained, and missing data can extend quota waits conservatively. Down deletes all quota evidence.

new(repo, opts)

@spec new(module(), keyword()) :: t()

Builds a handle scoped to a nonempty provider account identifier.