rate limiter 0.2 sec
This commit is contained in:
parent
e0e8fa8f6b
commit
24d53e731a
4 changed files with 127 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import aiohttp
|
|||
import logging
|
||||
from typing import Optional, Dict, Any
|
||||
from config import LICHESS_API_BASE_URL, LICHESS_STATS_API_BASE_URL
|
||||
from rate_limiter import get_rate_limiter
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -9,9 +10,11 @@ class LichessAPI:
|
|||
def __init__(self):
|
||||
self.lichess_base_url = LICHESS_API_BASE_URL
|
||||
self.stats_base_url = LICHESS_STATS_API_BASE_URL
|
||||
self.rate_limiter = get_rate_limiter()
|
||||
|
||||
async def get_user_profile(self, token: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get user profile from Lichess API using token"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
try:
|
||||
|
|
@ -31,6 +34,7 @@ class LichessAPI:
|
|||
|
||||
async def get_today_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get today's statistics from our stats API"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
|
|
@ -47,6 +51,7 @@ class LichessAPI:
|
|||
|
||||
async def get_yesterday_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get yesterday's statistics from our stats API"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
|
|
@ -63,6 +68,7 @@ class LichessAPI:
|
|||
|
||||
async def get_week_stats(self, username: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get week's statistics from our stats API"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
|
|
@ -79,6 +85,7 @@ class LichessAPI:
|
|||
|
||||
async def get_games_period(self, username: str, since: int, until: int, rated_only: Optional[bool] = None) -> Optional[Dict[str, Any]]:
|
||||
"""Get games for a specific period"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
url = f"{self.stats_base_url}/games/{username}/period"
|
||||
params = {"since": since, "until": until}
|
||||
|
|
@ -102,6 +109,7 @@ class LichessAPI:
|
|||
|
||||
async def get_puzzles_period(self, token: str, since: int, until: int, max_puzzles: int = 150) -> Optional[Dict[str, Any]]:
|
||||
"""Get puzzles for a specific period"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
try:
|
||||
|
|
@ -122,6 +130,7 @@ class LichessAPI:
|
|||
|
||||
async def check_user_exists(self, username: str) -> bool:
|
||||
"""Check if user exists on Lichess"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
|
|
@ -141,6 +150,7 @@ class LichessAPI:
|
|||
|
||||
async def get_user_ratings(self, username: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get user ratings from Lichess API"""
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
|
|
|
|||
53
LichessClientTG_bot/rate_limiter.py
Normal file
53
LichessClientTG_bot/rate_limiter.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
"""
|
||||
Rate limiter for Lichess API requests
|
||||
Ensures minimum delay between requests
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RateLimiter:
|
||||
"""
|
||||
Rate limiter that ensures minimum delay between requests.
|
||||
Thread-safe and async-safe.
|
||||
"""
|
||||
|
||||
def __init__(self, min_delay: float = 0.2):
|
||||
"""
|
||||
Initialize rate limiter.
|
||||
|
||||
Args:
|
||||
min_delay: Minimum delay in seconds between requests (default: 0.2)
|
||||
"""
|
||||
self.min_delay = min_delay
|
||||
self.last_request_time: Optional[float] = None
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
async def wait_if_needed(self):
|
||||
"""
|
||||
Wait if necessary to maintain minimum delay between requests.
|
||||
Should be called before each API request.
|
||||
"""
|
||||
async with self.lock:
|
||||
now = time.time()
|
||||
|
||||
if self.last_request_time is not None:
|
||||
elapsed = now - self.last_request_time
|
||||
if elapsed < self.min_delay:
|
||||
wait_time = self.min_delay - elapsed
|
||||
logger.debug(f"Rate limiter: waiting {wait_time:.3f} seconds")
|
||||
await asyncio.sleep(wait_time)
|
||||
now = time.time()
|
||||
|
||||
self.last_request_time = now
|
||||
|
||||
# Global rate limiter instance
|
||||
_rate_limiter = RateLimiter(min_delay=0.2)
|
||||
|
||||
def get_rate_limiter() -> RateLimiter:
|
||||
"""Get the global rate limiter instance"""
|
||||
return _rate_limiter
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue