rate limiter 0.2 sec
This commit is contained in:
parent
e0e8fa8f6b
commit
24d53e731a
4 changed files with 127 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ from typing import List, Dict, Any, Optional
|
|||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
import json
|
||||
from rate_limiter import get_rate_limiter
|
||||
|
||||
# Настройка логирования для модуля
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -42,6 +43,7 @@ class LichessClient:
|
|||
"""
|
||||
self.base_url = "https://lichess.org/api" # Базовый URL Lichess API
|
||||
self.client = httpx.AsyncClient(timeout=30.0) # HTTP клиент с таймаутом
|
||||
self.rate_limiter = get_rate_limiter()
|
||||
|
||||
async def get_user_activity(self, username: str) -> Optional[List[Dict[str, Any]]]:
|
||||
"""
|
||||
|
|
@ -58,6 +60,9 @@ class LichessClient:
|
|||
Exception: При других ошибках
|
||||
"""
|
||||
try:
|
||||
# Rate limiting: ждем если нужно
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
|
||||
# Формируем URL для получения активности пользователя
|
||||
url = f"{self.base_url}/user/{username}/activity"
|
||||
logger.info(f"Запрос активности пользователя {username}")
|
||||
|
|
@ -104,6 +109,9 @@ class LichessClient:
|
|||
Exception: При других ошибках
|
||||
"""
|
||||
try:
|
||||
# Rate limiting: ждем если нужно
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
|
||||
# Формируем URL для получения игр пользователя
|
||||
url = f"{self.base_url}/games/user/{username}"
|
||||
|
||||
|
|
@ -182,6 +190,9 @@ class LichessClient:
|
|||
Exception: При других ошибках
|
||||
"""
|
||||
try:
|
||||
# Rate limiting: ждем если нужно
|
||||
await self.rate_limiter.wait_if_needed()
|
||||
|
||||
# Формируем URL для получения активности по задачам
|
||||
url = f"{self.base_url}/puzzle/activity"
|
||||
|
||||
|
|
|
|||
53
LichessWebServices/rate_limiter.py
Normal file
53
LichessWebServices/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