feat: 新增图片视频并发设置

新增token导入导出为json
chore: 完善token刷新日志输出
fix: 修复自动更新时无法根据AT有效期禁用token问题
This commit is contained in:
TheSmallHanCat
2025-11-18 17:21:05 +08:00
parent 85f5c3620e
commit 42683f97ae
9 changed files with 700 additions and 88 deletions

View File

@@ -5,12 +5,15 @@ from ..core.models import Token
from ..core.config import config
from .token_manager import TokenManager
from .token_lock import TokenLock
from .concurrency_manager import ConcurrencyManager
from ..core.logger import debug_logger
class LoadBalancer:
"""Token load balancer with random selection and image generation lock"""
def __init__(self, token_manager: TokenManager):
def __init__(self, token_manager: TokenManager, concurrency_manager: Optional[ConcurrencyManager] = None):
self.token_manager = token_manager
self.concurrency_manager = concurrency_manager
# Use image timeout from config as lock timeout
self.token_lock = TokenLock(lock_timeout=config.image_timeout)
@@ -27,7 +30,11 @@ class LoadBalancer:
"""
# Try to auto-refresh tokens expiring within 24 hours if enabled
if config.at_auto_refresh_enabled:
debug_logger.log_info(f"[LOAD_BALANCER] 🔄 自动刷新功能已启用开始检查Token过期时间...")
all_tokens = await self.token_manager.get_all_tokens()
debug_logger.log_info(f"[LOAD_BALANCER] 📊 总Token数: {len(all_tokens)}")
refresh_count = 0
for token in all_tokens:
if token.is_active and token.expiry_time:
from datetime import datetime
@@ -35,8 +42,15 @@ class LoadBalancer:
hours_until_expiry = time_until_expiry.total_seconds() / 3600
# Refresh if expiry is within 24 hours
if hours_until_expiry <= 24:
debug_logger.log_info(f"[LOAD_BALANCER] 🔔 Token {token.id} ({token.email}) 需要刷新,剩余时间: {hours_until_expiry:.2f} 小时")
refresh_count += 1
await self.token_manager.auto_refresh_expiring_token(token.id)
if refresh_count == 0:
debug_logger.log_info(f"[LOAD_BALANCER] ✅ 所有Token都无需刷新")
else:
debug_logger.log_info(f"[LOAD_BALANCER] ✅ 刷新检查完成,共检查 {refresh_count} 个Token")
active_tokens = await self.token_manager.get_active_tokens()
if not active_tokens:
@@ -82,6 +96,9 @@ class LoadBalancer:
continue
if not await self.token_lock.is_locked(token.id):
# Check concurrency limit if concurrency manager is available
if self.concurrency_manager and not await self.concurrency_manager.can_use_image(token.id):
continue
available_tokens.append(token)
if not available_tokens:
@@ -90,5 +107,15 @@ class LoadBalancer:
# Random selection from available tokens
return random.choice(available_tokens)
else:
# For video generation, no lock needed
return random.choice(active_tokens)
# For video generation, check concurrency limit
if for_video_generation and self.concurrency_manager:
available_tokens = []
for token in active_tokens:
if await self.concurrency_manager.can_use_video(token.id):
available_tokens.append(token)
if not available_tokens:
return None
return random.choice(available_tokens)
else:
# For video generation without concurrency manager, no additional filtering
return random.choice(active_tokens)