WeaviateEx.Protocol.HTTP.RateLimit (WeaviateEx v0.7.4)
View SourceTracks rate limit headers from Weaviate responses.
Parses rate limit information from HTTP response headers and provides utilities for implementing client-side rate limiting.
Headers Tracked
X-RateLimit-Limit- Maximum requests allowed in the windowX-RateLimit-Remaining- Requests remaining in current windowX-RateLimit-Reset- Unix timestamp when the rate limit resets
Usage
# Extract rate limit info from response headers
rate_limit = RateLimit.from_headers(response.headers)
# Check if we should wait
case RateLimit.should_wait?(rate_limit) do
{:wait, ms} -> Process.sleep(ms)
:ok -> :proceed
end
Summary
Functions
Extracts rate limit information from response headers.
Checks if the rate limit info indicates we're close to being rate limited.
Returns the percentage of rate limit remaining.
Returns seconds until the rate limit resets.
Checks if rate limited and returns wait time in milliseconds.
Checks if the rate limit info is stale (reset time has passed).
Returns a human-readable summary of the rate limit status.
Types
@type t() :: %WeaviateEx.Protocol.HTTP.RateLimit{ limit: non_neg_integer() | nil, remaining: non_neg_integer() | nil, reset_at: DateTime.t() | nil }
Functions
Extracts rate limit information from response headers.
Examples
headers = [
{"x-ratelimit-limit", "100"},
{"x-ratelimit-remaining", "95"},
{"x-ratelimit-reset", "1735500000"}
]
RateLimit.from_headers(headers)
# => %RateLimit{limit: 100, remaining: 95, reset_at: ~U[...]}
Checks if the rate limit info indicates we're close to being rate limited.
Returns true if remaining requests are below the threshold percentage.
Examples
rate_limit = %RateLimit{limit: 100, remaining: 5}
RateLimit.near_limit?(rate_limit, 10)
# => true
rate_limit = %RateLimit{limit: 100, remaining: 50}
RateLimit.near_limit?(rate_limit, 10)
# => false
Returns the percentage of rate limit remaining.
Examples
rate_limit = %RateLimit{limit: 100, remaining: 25}
RateLimit.remaining_percent(rate_limit)
# => 25.0
@spec seconds_until_reset(t()) :: non_neg_integer() | nil
Returns seconds until the rate limit resets.
Examples
rate_limit = %RateLimit{reset_at: future_time}
RateLimit.seconds_until_reset(rate_limit)
# => 60
@spec should_wait?(t()) :: {:wait, non_neg_integer()} | :ok
Checks if rate limited and returns wait time in milliseconds.
Examples
rate_limit = %RateLimit{remaining: 0, reset_at: future_time}
RateLimit.should_wait?(rate_limit)
# => {:wait, 5000}
rate_limit = %RateLimit{remaining: 10, reset_at: future_time}
RateLimit.should_wait?(rate_limit)
# => :ok
Checks if the rate limit info is stale (reset time has passed).
Examples
rate_limit = %RateLimit{reset_at: past_time}
RateLimit.stale?(rate_limit)
# => true
Returns a human-readable summary of the rate limit status.
Examples
rate_limit = %RateLimit{limit: 100, remaining: 42, reset_at: future_time}
RateLimit.summary(rate_limit)
# => "42/100 remaining, resets in 45s"