Implement a rate limiter for concurrent Libro.fm API requests to prevent overwhelming the service, especially during parallel downloads. This caps concurrent lookup calls for M4B, MP3, and PDF, while allowing CDN downloads and initial authentication/library fetching to proceed unimpeded.
2.8 KiB
2.8 KiB
API Rate Limiter — Issue #15
Date: 2026-06-03 Type: issue Reference: Issue #15
Goal
Implement a lightweight API rate limiter using threading.Semaphore(3) that caps concurrent Libro.fm API calls regardless of worker count. CDN file downloads must NOT be limited.
What Was Done
- Added
threading.Semaphore(3)toLibroFmClient.__init__via_api_semaphoreattribute - Added
API_SEMAPHORE_CAPACITY = 3configurable constant (not user-facing) - Wrapped 3 API methods with
with self._api_semaphore:context manager:fetch_m4b_url()— M4B URL lookupfetch_download_manifest()— MP3 manifest fetchfetch_pdf_extra_url()— PDF extra URL lookup
- NOT applied to:
authenticate(),fetch_library(), CDN downloads (download_m4b,download_zip_part) - Added 4 new tests in
TestApiRateLimiterclass:test_semaphore_blocks_at_capacity— 4 threads, max 3 in-flighttest_semaphore_releases_on_exception— semaphore released onM4BUnavailableErrortest_cdn_downloads_bypass_semaphore— CDN proceeds while API slots occupiedtest_authenticate_and_fetch_library_not_limited— auth/library work while slots full
- 198 total tests pass (194 existing + 4 new)
Decisions & Rationale
| Decision | Rationale |
|---|---|
Semaphore on LibroFmClient (not external wrapper) |
Encapsulates rate-limiting in the client; callers don't need to know about it. Cleanest fit since all 3 methods are on the same class |
with self._api_semaphore: (context manager) |
Guarantees release on both success and exception — no try/finally needed |
| Capacity = 3 (configurable constant) | Conservative default for Libro.fm API; not user-facing per issue spec |
Not applied to authenticate()/fetch_library() |
These happen before the parallel download phase (sequential), so no contention risk |
Gotchas & Fixes
| Problem | Root Cause | Fix |
|---|---|---|
threading.Value AttributeError in tests |
Value is from multiprocessing, not threading. Used it for shared thread state initially |
Switched to dict + threading.Lock for shared mutable state, or plain list for sequential tests |
Files Changed
| File | Change Summary |
|---|---|
librofm_downloader/client.py |
Added import threading, API_SEMAPHORE_CAPACITY constant, _api_semaphore to __init__, wrapped 3 methods with with self._api_semaphore: |
tests/test_client.py |
Added TestApiRateLimiter class with 4 tests covering capacity blocking, exception release, CDN bypass, and non-limited method isolation |
Open Items & Next Steps
- None — issue complete, all acceptance criteria met
Log written by write-log skill