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_limit_holders"shows who holds each slot (key, pid, start_time, acquired_at);rate_limitsshows capacities and throttle counts.
Each held slot is one row in rate_limit_holders tagged with the
holder's pid and the start time the OS reports for that process. A
process that dies without releasing (SIGKILL, OOM kill, hard container
stop) leaves its rows behind; the next acquire on that key deletes
rows whose pid no longer exists, is a zombie its parent has not
reaped, or now belongs to a process with a different start time, before
counting. A dead holder's slots therefore
return on the next acquire, even when a container restarted in place
and its new process got the same pid (PID 1, typically).
Start times come from /proc/<pid>/stat on Linux and
sysctl(KERN_PROC_PID) on macOS. Constructing the backend on any
other platform raises NotImplementedError.
Limits of the holder check:
- All processes sharing the file must share one PID namespace. A file
shared across containers with separate namespaces (a mounted volume
via
LATENT_RATE_LIMIT_DB) can make other containers' holders look dead, and their slots get handed out again. The same applies on Linux when/procis mounted withhidepidand holders run as other users.
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¶
Single-host multi-process rate-limiter backed by SQLite + WAL.