`src/stream/rate_limit.rs` — the token bucket
The proactive rate limiter: wait before firing into a throttled endpoint, instead of only reacting to 429s.
Key items
TokenBucket::new(capacity)— burst = capacity; continuous refill atcapacity/60tokens per second; lazy refill on access (no background task).take()/take_at(instant)— consume one token →Ok(())orErr(RateLimitError::Wait(duration))(time until a token refills;Duration::MAXfor zero-refill buckets) orErr(Poisoned).available()/available_at()— non-consuming queries.RateLimiter::new(requests_per_minute)—0disables; one bucket per base URL (lazily created, shared).
Behavior notes
- Provider identity is the base URL: OpenAI and Ollama get independent buckets; two clients on one endpoint share.
- The handler’s
gate_on_rate_limitsleeps in slices bounded byrate_limit_max_wait(30s default) and then proceeds anyway — better to risk a 429 than hang. - Reactive throttling (honoring 429 responses) lives in the handler’s retry ladder; this module is the proactive complement.
Deep dive: Stream events.