A lightweight API rate limiter using threading.Semaphore(3) that caps concurrent Libro.fm API calls regardless of worker count. CDN file downloads are NOT limited (they go to CloudFront/AWS which handles high concurrency natively).
New module or addition:
Create a rate_limit function or context manager that wraps callable execution with a semaphore
Semaphore capacity: 3 (configurable constant, not user-facing)
Applied to 3 client methods: fetch_m4b_url(), fetch_download_manifest(), fetch_pdf_extra_url()
NOT applied to: CDN streaming downloads (download_m4b(), ZIP part downloads), authenticate(), fetch_library() (these happen before the parallel phase)
Design decision during implementation: The semaphore can live either as a client wrapper decorator or in the orchestrator layer. Choose the cleanest fit based on where it minimizes coupling.
Acceptance criteria
Semaphore blocks when 3 API calls are in flight; 4th caller waits
Semaphore releases after each call completes (success or exception)
Multiple threads calling rate-limited API methods are correctly serialized to max 3
CDN download calls are NOT affected by the semaphore
3-4 new tests: semaphore blocks at capacity, releases after call, multiple callers serialized, CDN bypass
Existing client/downloader tests pass unchanged
Blocked by
None - can start immediately
## Parent
#11
## What to build
A lightweight API rate limiter using `threading.Semaphore(3)` that caps concurrent Libro.fm API calls regardless of worker count. CDN file downloads are NOT limited (they go to CloudFront/AWS which handles high concurrency natively).
**New module or addition:**
- Create a `rate_limit` function or context manager that wraps callable execution with a semaphore
- Semaphore capacity: 3 (configurable constant, not user-facing)
- Applied to 3 client methods: `fetch_m4b_url()`, `fetch_download_manifest()`, `fetch_pdf_extra_url()`
- NOT applied to: CDN streaming downloads (`download_m4b()`, ZIP part downloads), `authenticate()`, `fetch_library()` (these happen before the parallel phase)
**Design decision during implementation:** The semaphore can live either as a client wrapper decorator or in the orchestrator layer. Choose the cleanest fit based on where it minimizes coupling.
## Acceptance criteria
- [ ] Semaphore blocks when 3 API calls are in flight; 4th caller waits
- [ ] Semaphore releases after each call completes (success or exception)
- [ ] Multiple threads calling rate-limited API methods are correctly serialized to max 3
- [ ] CDN download calls are NOT affected by the semaphore
- [ ] 3-4 new tests: semaphore blocks at capacity, releases after call, multiple callers serialized, CDN bypass
- [ ] Existing client/downloader tests pass unchanged
## Blocked by
None - can start immediately
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Parent
#11
What to build
A lightweight API rate limiter using
threading.Semaphore(3)that caps concurrent Libro.fm API calls regardless of worker count. CDN file downloads are NOT limited (they go to CloudFront/AWS which handles high concurrency natively).New module or addition:
rate_limitfunction or context manager that wraps callable execution with a semaphorefetch_m4b_url(),fetch_download_manifest(),fetch_pdf_extra_url()download_m4b(), ZIP part downloads),authenticate(),fetch_library()(these happen before the parallel phase)Design decision during implementation: The semaphore can live either as a client wrapper decorator or in the orchestrator layer. Choose the cleanest fit based on where it minimizes coupling.
Acceptance criteria
Blocked by
None - can start immediately