Rate Limiting
Nexar includes built-in rate limiting to ensure your application complies with Riot's API rate limits and avoids getting rate limited.
Note
If the rate limit is met, and a Retry-After is provided by Riot, Nexar will simply wait the small duration and continue.
Default Rate Limits
By default, Nexar enforces the following rate limits:
- 20 requests per 1 second
- 100 requests per 2 minutes
These are the standard Riot API limits for most personal applications.
Basic Usage
Rate limiting is enabled automatically when you create a NexarClient
:
# Rate limiting is automatically applied to all API calls
account = await client.get_riot_account("bexli", "bex")
Custom Rate Limits
You can configure custom rate limits if needed:
client_custom_limits = NexarClient(
riot_api_key=api_key,
default_region=Region.NA1,
per_second_limit=(1, 1), # Max of 1 request per second
per_minute_limit=(10, 5), # Max of 10 requests per 5 minutes
)
async with client_custom_limits:
# Use client here
pass
How It Works
Nexar automatically enforces Riot's API rate limits by sleeping between calls, using the strictest of the two provided rate limits. Additionally, aiolimiter
library to aid with asyncio.gather
calls1.
Cached responses do not count against rate limits.
Logging
The rate limiter provides detailed logging to help you understand its behavior. You can configure logging levels using configure_logging
.
Example Log Output
[nexar] Rate limiter initialized with 2 limits:
[nexar] Limit 1: 20 requests per 1s
[nexar] Limit 2: 100 requests per 120s
[nexar] Limit 1: 19/20 used, 1 remaining
[nexar] Limit 2: 45/100 used, 55 remaining
[nexar] Rate limit hit! Limit 1 (20 req/1s) - waiting 0.85 seconds
[nexar] Rate limit wait complete - proceeding with request
Rate Limiting vs Caching
Rate limiting and caching work together intelligently:
- Cached responses don't count against rate limits since no actual API request is made
- Fresh requests are subject to rate limiting to ensure compliance with Riot's limits
- Cache hits are instant and don't consume any rate limit quota
- Cache misses trigger rate limiting before making the actual API call
This means you can make the same API call repeatedly without worrying about rate limits if the response is cached. Only unique requests or expired cache entries will consume your rate limit quota.
Example
# First call - fresh request, counts against rate limits
account = await client.get_riot_account("bexli", "bex") # Rate limited if needed
# Subsequent calls - cached, no rate limiting
account = await client.get_riot_account("bexli", "bex") # Instant, no rate limit check
account = await client.get_riot_account("bexli", "bex") # Instant, no rate limit check
# Different account - fresh request, rate limited
other = await client.get_riot_account("Doublelift", "NA1") # Rate limited if needed
-
With limited success in testing ↩