fix: 修复 _email_poller 线程静默死亡问题
- IMAP 连接加 30s socket timeout,防止 _send_id_command 等操作永久阻塞 - _email_poller 加顶层 try/except,异常后 sleep 5s 继续循环 - @retry() 加指数退避延迟(1s,2s,4s...),避免重试风暴
This commit is contained in:
@@ -87,12 +87,12 @@ def _select_mailbox(conn, mailbox: str = "INBOX"):
|
||||
raise RuntimeError(f"无法选择邮箱 {mailbox}: {data}")
|
||||
|
||||
|
||||
def _login_and_prepare(account: EmailAccount):
|
||||
logger.info("连接 %s:%d", account.imap_server, account.imap_port)
|
||||
def _login_and_prepare(account: EmailAccount, timeout: int = 30):
|
||||
logger.info("连接 %s:%d (超时%ds)", account.imap_server, account.imap_port, timeout)
|
||||
if account.use_ssl:
|
||||
conn = imaplib.IMAP4_SSL(account.imap_server, account.imap_port)
|
||||
conn = imaplib.IMAP4_SSL(account.imap_server, account.imap_port, timeout=timeout)
|
||||
else:
|
||||
conn = imaplib.IMAP4(account.imap_server, account.imap_port)
|
||||
conn = imaplib.IMAP4(account.imap_server, account.imap_port, timeout=timeout)
|
||||
if account.use_starttls:
|
||||
conn.starttls()
|
||||
conn.login(account.username, account.password)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import logging
|
||||
import time
|
||||
from functools import wraps
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def retry(max_tries=10):
|
||||
def retry(max_tries=10, base_delay=1, max_delay=60):
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
@@ -15,10 +16,12 @@ def retry(max_tries=10):
|
||||
except Exception as e:
|
||||
last_exc = e
|
||||
if attempt < max_tries:
|
||||
delay = min(base_delay * (2 ** (attempt - 1)), max_delay)
|
||||
logger.warning(
|
||||
"%s 失败(第%d次),正在重试: %s",
|
||||
func.__name__, attempt, e,
|
||||
"%s 失败(第%d次),%d秒后重试: %s",
|
||||
func.__name__, attempt, delay, e,
|
||||
)
|
||||
time.sleep(delay)
|
||||
else:
|
||||
logger.error(
|
||||
"%s 失败 %d 次,放弃: %s",
|
||||
|
||||
Reference in New Issue
Block a user