diff --git a/api/chat.py b/api/chat.py index 355aece..f34b734 100644 --- a/api/chat.py +++ b/api/chat.py @@ -32,7 +32,7 @@ class Command(enum.IntEnum): UPDATE_TRANSLATION = 7 -_http_session = aiohttp.ClientSession() +_http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) room_manager: Optional['RoomManager'] = None @@ -576,7 +576,7 @@ class RoomInfoHandler(api.base.ApiHandler): res.status, res.reason) return room_id, 0 data = await res.json() - except aiohttp.ClientConnectionError: + except (aiohttp.ClientConnectionError, asyncio.TimeoutError): logger.exception('room %d _get_room_info failed', room_id) return room_id, 0 @@ -600,7 +600,7 @@ class RoomInfoHandler(api.base.ApiHandler): # res.status, res.reason) # return cls._host_server_list_cache # data = await res.json() - # except aiohttp.ClientConnectionError: + # except (aiohttp.ClientConnectionError, asyncio.TimeoutError): # logger.exception('room %d _get_server_host_list failed', room_id) # return cls._host_server_list_cache # diff --git a/blivedm b/blivedm index 13712f8..4669b2c 160000 --- a/blivedm +++ b/blivedm @@ -1 +1 @@ -Subproject commit 13712f89ebb13b9ff9a2cf50c9d6922200538113 +Subproject commit 4669b2c1c9a1654db340d02ff16c9f88be661d9f diff --git a/frontend/src/api/chat/ChatClientDirect.js b/frontend/src/api/chat/ChatClientDirect.js index 24fa5bb..728bee9 100644 --- a/frontend/src/api/chat/ChatClientDirect.js +++ b/frontend/src/api/chat/ChatClientDirect.js @@ -146,7 +146,11 @@ export default class ChatClientDirect { onReceiveTimeout() { window.console.warn('接收消息超时') this.receiveTimeoutTimerId = null + + // 直接丢弃阻塞的websocket,不等onclose回调了 + this.websocket.onopen = this.websocket.onclose = this.websocket.onmessage = null this.websocket.close() + this.onWsClose() } onWsClose () { diff --git a/frontend/src/main.js b/frontend/src/main.js index 6773acc..6151ce4 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -24,6 +24,7 @@ if (process.env.NODE_ENV === 'development') { // 开发时使用localhost:12450 axios.defaults.baseURL = 'http://localhost:12450' } +axios.defaults.timeout = 10 * 1000 Vue.use(VueRouter) Vue.use(VueI18n) diff --git a/models/avatar.py b/models/avatar.py index bdff425..353f8ed 100644 --- a/models/avatar.py +++ b/models/avatar.py @@ -18,7 +18,7 @@ logger = logging.getLogger(__name__) DEFAULT_AVATAR_URL = '//static.hdslb.com/images/member/noface.gif' _main_event_loop = asyncio.get_event_loop() -_http_session = aiohttp.ClientSession() +_http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) # user_id -> avatar_url _avatar_url_cache: Dict[int, str] = {} # 正在获取头像的Future,user_id -> Future diff --git a/models/translate.py b/models/translate.py index 603af52..f5de373 100644 --- a/models/translate.py +++ b/models/translate.py @@ -21,7 +21,7 @@ NO_TRANSLATE_TEXTS = { } _main_event_loop = asyncio.get_event_loop() -_http_session = aiohttp.ClientSession() +_http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) _translate_providers: List['TranslateProvider'] = [] # text -> res _translate_cache: Dict[str, str] = {} @@ -127,7 +127,6 @@ class TencentTranslate(TranslateProvider): self._reinit_future = None # 连续失败的次数 self._fail_count = 0 - self._cool_down_future = None async def init(self): self._reinit_future = asyncio.ensure_future(self._reinit_coroutine()) @@ -174,14 +173,13 @@ class TencentTranslate(TranslateProvider): try: while True: await asyncio.sleep(30) - while True: - logger.debug('TencentTranslate reinit') - try: - if await self._do_init(): - break - except Exception: - logger.exception('TencentTranslate init error:') - await asyncio.sleep(3 * 60) + logger.debug('TencentTranslate reinit') + try: + await self._do_init() + except asyncio.CancelledError: + raise + except Exception: + logger.exception('TencentTranslate init error:') except asyncio.CancelledError: pass @@ -238,26 +236,14 @@ class TencentTranslate(TranslateProvider): def _on_fail(self): self._fail_count += 1 - # 目前没有测试出被ban的情况,为了可靠性,连续失败20次时冷却并重新init - if self._fail_count >= 20 and self._cool_down_future is None: - self._cool_down_future = asyncio.ensure_future(self._cool_down()) + # 目前没有测试出被ban的情况,为了可靠性,连续失败20次时冷却直到下次重新init + if self._fail_count >= 20: + self._cool_down() - async def _cool_down(self): + def _cool_down(self): logger.info('TencentTranslate is cooling down') self._qtv = self._qtk = '' - try: - while True: - await asyncio.sleep(3 * 60) - logger.info('TencentTranslate reinit') - try: - if await self._do_init(): - self._fail_count = 0 - break - except Exception: - logger.exception('TencentTranslate init error:') - finally: - logger.info('TencentTranslate finished cooling down') - self._cool_down_future = None + self._fail_count = 0 class YoudaoTranslate(TranslateProvider): diff --git a/requirements.txt b/requirements.txt index 720885a..2b489f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -aiohttp==3.5.4 +aiohttp==3.7.4 sqlalchemy==1.3.13 tornado==6.0.2 diff --git a/update.py b/update.py index 7d89566..70b168b 100644 --- a/update.py +++ b/update.py @@ -13,7 +13,7 @@ def check_update(): async def _do_check_update(): try: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as session: async with session.get('https://api.github.com/repos/xfgryujk/blivechat/releases/latest') as r: data = await r.json() if data['name'] != VERSION: