tessl install tessl/pypi-pyrate-limiter@3.9.0Python Rate-Limiter using Leaky-Bucket Algorithm for controlling request rates in applications with multiple backend storage options.
Agent Success
Agent success rate when using this tile
81%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.45x
Baseline
Agent success rate without this tile
56%
Build a data fetching service that retrieves information from a simulated external API. The service must enforce rate limits to prevent overwhelming the API endpoint. Implement both synchronous and asynchronous data fetchers with appropriate rate limiting.
Synchronous Data Fetcher: Create a function fetch_user_data(user_id: int) -> dict that simulates fetching user data. The function should:
Asynchronous Data Fetcher: Create an async function fetch_orders_async(order_id: int) -> dict that simulates fetching order data. The function should:
Weighted API Call: Create a function fetch_bulk_report(report_size: str) -> dict that simulates fetching reports of different sizes. The function should:
{"user_id": user_id, "name": "User Name"})File: test_rate_limiter.py { .test_file }
Test that the synchronous fetcher enforces the 5 calls per 10 seconds limit:
import time
from solution import fetch_user_data
# Should succeed for first 5 calls
for i in range(5):
result = fetch_user_data(i)
assert result is not None
assert "user_id" in result
# 6th call should block briefly, then succeed
start_time = time.time()
result = fetch_user_data(5)
elapsed = time.time() - start_time
assert result is not None
assert elapsed > 1 # Should have waitedFile: test_rate_limiter.py { .test_file }
Test that the async fetcher enforces the 10 calls per 15 seconds limit:
import asyncio
from solution import fetch_orders_async
async def test_async_limit():
# Should succeed for first 10 calls
for i in range(10):
result = await fetch_orders_async(i)
assert result is not None
assert "order_id" in result
# 11th call should block briefly, then succeed
start_time = asyncio.get_event_loop().time()
result = await fetch_orders_async(10)
elapsed = asyncio.get_event_loop().time() - start_time
assert result is not None
assert elapsed > 1 # Should have waited
asyncio.run(test_async_limit())File: test_rate_limiter.py { .test_file }
Test that weighted reports consume appropriate capacity:
from solution import fetch_bulk_report
# Fetch 4 small reports (4 weight units total)
for i in range(4):
result = fetch_bulk_report("small")
assert result is not None
# Fetch 3 medium reports (9 weight units total)
for i in range(3):
result = fetch_bulk_report("medium")
assert result is not None
# Total: 13 weight units used
# Fetch 1 large report (5 weight units) - should succeed (18 total)
result = fetch_bulk_report("large")
assert result is not None
# Try another medium report (3 weight units) - would exceed 20, should block
import time
start_time = time.time()
result = fetch_bulk_report("medium")
elapsed = time.time() - start_time
assert result is not None
assert elapsed > 1 # Should have waitedProvides rate limiting capabilities using the leaky bucket algorithm.
solution.py: Contains the three rate-limited functionstest_rate_limiter.py: Contains test cases validating the rate limiting behavior