Spaces:
Paused
Paused
| """ | |
| Connection retry utility for Ultroid | |
| """ | |
| import asyncio | |
| import logging | |
| from typing import Optional | |
| LOGS = logging.getLogger(__name__) | |
| class ConnectionRetry: | |
| def __init__(self, max_retries: int = 5, delay: float = 5.0): | |
| self.max_retries = max_retries | |
| self.delay = delay | |
| async def connect_with_retry(self, connect_func, *args, **kwargs): | |
| """Connect with retry logic""" | |
| for attempt in range(self.max_retries): | |
| try: | |
| LOGS.info(f"Connection attempt {attempt + 1}/{self.max_retries}") | |
| result = await connect_func(*args, **kwargs) | |
| LOGS.info("Connection successful!") | |
| return result | |
| except Exception as e: | |
| LOGS.warning(f"Connection attempt {attempt + 1} failed: {e}") | |
| if attempt < self.max_retries - 1: | |
| LOGS.info(f"Retrying in {self.delay} seconds...") | |
| await asyncio.sleep(self.delay) | |
| self.delay *= 1.5 # Exponential backoff | |
| else: | |
| LOGS.error(f"All connection attempts failed after {self.max_retries} tries") | |
| raise e | |
| # Global retry instance | |
| retry_handler = ConnectionRetry(max_retries=5, delay=5.0) | |