Skip to content

latent.rate_limiter.backends.sqlite_ipc

SQLite-backed rate-limiter for single-host multi-process coordination.

Default backend. Uses a single SQLite database file with WAL journaling so multiple Python processes on one host share concurrency caps and throttle observation. Cross-host coordination (multi-machine Prefect workers, K8s) needs the Redis backend (separate PR).

Why SQLite over POSIX semaphores or a JSON file:

  • AIMD-ready (PR 3): capacity is a column, atomic UPDATE on throttle. POSIX semaphores don't support resizing.
  • No new pip dependency; stdlib only.
  • Operations-introspectable: sqlite3 /tmp/latent_rate_limiter.db "SELECT * FROM rate_limits" shows live state and throttle counts.
  • Robust to crashes: BEGIN IMMEDIATE transactions roll back cleanly; flock-style auto-recovery isn't needed.

The acquire path polls (50ms default) when contended. LLM calls take seconds, so the polling overhead is invisible. Latency budget for acquire under no contention: one BEGIN IMMEDIATE round-trip, ~100µs typical.

Classes

SqliteIpcBackend

SqliteIpcBackend(db_path: Path | None = None)

Single-host multi-process rate-limiter backed by SQLite + WAL.

Methods

SqliteIpcBackend.acquire

acquire(key: str, max_concurrent: int) -> None

SqliteIpcBackend.current_capacity

current_capacity(key: str) -> int

SqliteIpcBackend.list_keys

list_keys() -> list[str]

SqliteIpcBackend.release

release(key: str) -> None

SqliteIpcBackend.report_throttle

report_throttle(key: str) -> None

SqliteIpcBackend.throttle_count

throttle_count(key: str) -> int

Attributes

DEFAULT_DB_PATH