更新:

This commit is contained in:
Your Name
2026-07-17 10:21:18 +08:00
parent 530e7f839d
commit 2f2150d2f6
6 changed files with 596 additions and 30 deletions
+21 -1
View File
@@ -1,5 +1,7 @@
import asyncio
import logging
import os
import random
import time
from typing import Awaitable, Callable, Optional
@@ -374,6 +376,22 @@ class DouyinImService:
)
await self._ws_client.start()
# 轮询错峰:多账号同时托管时,若所有账号按同一节奏轮询,请求会在同一
# 时刻叠峰。这里给每个账号随机相位偏移 + 每轮 ±20% 抖动,把请求摊平。
# WS 可用时轮询只是兜底,可以适当放缓(KEFU_IM_POLL_INTERVAL_SECONDS 可调)。
try:
poll_interval = float(os.getenv("KEFU_IM_POLL_INTERVAL_SECONDS", "") or 15)
except ValueError:
poll_interval = 15.0
poll_interval = max(5.0, poll_interval)
if has_ws:
poll_interval = max(poll_interval, 30.0)
# 首轮轮询前的随机延迟(相位偏移),批量启动时错开各账号的首波请求
await asyncio.sleep(random.uniform(0.5, min(10.0, poll_interval)))
if not self._running:
return
try:
await self._poll_conversations()
except Exception as e:
@@ -387,10 +405,12 @@ class DouyinImService:
)
loop_count = 0
next_poll = time.monotonic() + poll_interval * random.uniform(0.8, 1.2)
while self._running:
loop_count += 1
try:
if loop_count % 3 == 1:
if time.monotonic() >= next_poll:
next_poll = time.monotonic() + poll_interval * random.uniform(0.8, 1.2)
await self._poll_conversations()
if loop_count % 6 == 1:
logger.info(f"IM direct tick #{loop_count} account={self.account_id}")