WeaviateEx.Protocol.HTTP.RateLimit (WeaviateEx v0.7.4)

View Source

Tracks 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 window
  • X-RateLimit-Remaining - Requests remaining in current window
  • X-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

t()

@type t() :: %WeaviateEx.Protocol.HTTP.RateLimit{
  limit: non_neg_integer() | nil,
  remaining: non_neg_integer() | nil,
  reset_at: DateTime.t() | nil
}

Functions

from_headers(headers)

@spec from_headers([{String.t(), String.t()}]) :: t()

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[...]}

near_limit?(rate_limit, threshold_percent \\ 10)

@spec near_limit?(t(), number()) :: boolean()

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

remaining_percent(arg1)

@spec remaining_percent(t()) :: float() | nil

Returns the percentage of rate limit remaining.

Examples

rate_limit = %RateLimit{limit: 100, remaining: 25}
RateLimit.remaining_percent(rate_limit)
# => 25.0

seconds_until_reset(rate_limit)

@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

should_wait?(arg1)

@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

stale?(rate_limit)

@spec stale?(t()) :: boolean()

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

summary(rate_limit)

@spec summary(t()) :: String.t()

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"