WeaviateEx.Batch.RateLimit (WeaviateEx v0.7.4)
View SourceProvider-specific rate limit detection for batch operations.
Detects rate limiting from various AI providers and calculates appropriate backoff times.
Supported Patterns
- Generic: HTTP 429 status with optional Retry-After header
- OpenAI:
rate_limitorinsufficient_quotaerror types - Cohere: X-RateLimit-* headers
- Weaviate: Rate limit errors in batch result messages
Examples
case RateLimit.detect(response) do
{:rate_limited, retry_after_ms} ->
Process.sleep(retry_after_ms)
retry_request()
:ok ->
handle_response(response)
end
Summary
Functions
Calculates exponential backoff with jitter for retry attempts.
Detects if a response indicates rate limiting.
Extracts the Retry-After value from response headers.
Checks if a response or error indicates rate limiting.
Types
@type detect_result() :: :ok | {:rate_limited, non_neg_integer()}
@type response() :: map()
Functions
@spec calculate_backoff(non_neg_integer()) :: non_neg_integer()
Calculates exponential backoff with jitter for retry attempts.
Uses the formula: min(max_backoff, base * 2^attempt) + jitter
Examples
RateLimit.calculate_backoff(0) # => ~1000ms
RateLimit.calculate_backoff(1) # => ~2000ms
RateLimit.calculate_backoff(2) # => ~4000ms
@spec detect(response()) :: detect_result()
Detects if a response indicates rate limiting.
Returns :ok if not rate limited, or {:rate_limited, retry_after_ms}
with the recommended wait time in milliseconds.
Examples
case RateLimit.detect(response) do
{:rate_limited, 30_000} -> Process.sleep(30_000)
:ok -> process_response()
end
@spec extract_retry_after(map()) :: non_neg_integer()
Extracts the Retry-After value from response headers.
Returns the retry duration in milliseconds.
Examples
RateLimit.extract_retry_after(%{"retry-after" => "30"})
# => 30_000
Checks if a response or error indicates rate limiting.
Examples
RateLimit.rate_limited?(%{status: 429})
# => true
RateLimit.rate_limited?({:error, %{status: 429}})
# => true