(*args, **kwargs)
| 13 | def decorator(func): |
| 14 | @wraps(func) |
| 15 | def wrapper(*args, **kwargs): |
| 16 | for attempt in range(max_retries): |
| 17 | try: |
| 18 | return func(*args, **kwargs) |
| 19 | except Exception as e: |
| 20 | error_str = str(e) |
| 21 | if "429" in error_str or "rate" in error_str.lower(): |
| 22 | if attempt < max_retries - 1: |
| 23 | wait_time = (base_delay ** attempt) + random.uniform(0, 1) |
| 24 | print(f"[重试] 遇到限流,{wait_time:.1f}秒后重试 (尝试 {attempt + 2}/{max_retries})") |
| 25 | time.sleep(wait_time) |
| 26 | continue |
| 27 | raise |
| 28 | raise Exception( |
| 29 | f"Text API 重试 {max_retries} 次后仍失败。\n" |
| 30 | "可能原因:\n" |
| 31 | "1. API持续限流或配额不足\n" |
| 32 | "2. 网络连接持续不稳定\n" |
| 33 | "3. API服务暂时不可用\n" |
| 34 | "建议:稍后再试,或联系API服务提供商" |
| 35 | ) |
| 36 | return wrapper |
| 37 | return decorator |
| 38 |
nothing calls this directly
no outgoing calls
no test coverage detected