Slice 4: API rate limiter (Semaphore) #15

Closed
opened 2026-06-03 16:47:10 +02:00 by AlexKucera · 0 comments
AlexKucera commented 2026-06-03 16:47:10 +02:00 (Migrated from github.com)

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

## 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
Sign in to join this conversation.