diff --git a/services/chat.py b/services/chat.py index e02a657..a93ef36 100644 --- a/services/chat.py +++ b/services/chat.py @@ -63,8 +63,10 @@ class LiveClientManager: class LiveClient(blivedm.BLiveClient): + HEARTBEAT_INTERVAL = 10 + def __init__(self, room_id): - super().__init__(room_id, session=utils.request.http_session, heartbeat_interval=10) + super().__init__(room_id, session=utils.request.http_session, heartbeat_interval=self.HEARTBEAT_INTERVAL) @property def tmp_room_id(self): @@ -78,13 +80,20 @@ class LiveClient(blivedm.BLiveClient): class ClientRoomManager: """管理到客户端的连接""" + # 房间没有客户端后延迟多久删除房间,不立即删除防止短时间后重连 + DELAY_DEL_ROOM_TIMEOUT = 10 + def __init__(self): self._rooms: Dict[int, ClientRoom] = {} + # room_id -> timer_handle + self._delay_del_timer_handles: Dict[int, asyncio.TimerHandle] = {} def add_client(self, room_id, client: 'api.chat.ChatHandler'): - room = self.get_or_add_room(room_id) + room = self._get_or_add_room(room_id) room.add_client(client) + self._clear_delay_del_timer(room_id) + def del_client(self, room_id, client: 'api.chat.ChatHandler'): room = self.get_room(room_id) if room is None: @@ -92,12 +101,12 @@ class ClientRoomManager: room.del_client(client) if room.client_count == 0: - self.del_room(room_id) + self.delay_del_room(room_id, self.DELAY_DEL_ROOM_TIMEOUT) def get_room(self, room_id): return self._rooms.get(room_id, None) - def get_or_add_room(self, room_id): + def _get_or_add_room(self, room_id): room = self._rooms.get(room_id, None) if room is None: logger.info('room=%d creating client room', room_id) @@ -108,6 +117,8 @@ class ClientRoomManager: return room def del_room(self, room_id): + self._clear_delay_del_timer(room_id) + room = self._rooms.pop(room_id, None) if room is None: return @@ -117,6 +128,21 @@ class ClientRoomManager: _live_client_manager.del_live_client(room_id) + def delay_del_room(self, room_id, timeout): + self._clear_delay_del_timer(room_id) + self._delay_del_timer_handles[room_id] = asyncio.get_event_loop().call_later( + timeout, self._on_delay_del_room, room_id + ) + + def _clear_delay_del_timer(self, room_id): + timer_handle = self._delay_del_timer_handles.pop(room_id, None) + if timer_handle is not None: + timer_handle.cancel() + + def _on_delay_del_room(self, room_id): + self._delay_del_timer_handles.pop(room_id, None) + self.del_room(room_id) + class ClientRoom: def __init__(self, room_id): diff --git a/services/translate.py b/services/translate.py index 38d7cfc..45ad88e 100644 --- a/services/translate.py +++ b/services/translate.py @@ -25,7 +25,6 @@ NO_TRANSLATE_TEXTS = { '强', '余裕', '余裕余裕', '大丈夫', '再放送', '放送事故', '清楚', '清楚清楚' } -_main_event_loop = asyncio.get_event_loop() _translate_providers: List['TranslateProvider'] = [] # text -> res _translate_cache: Dict[str, str] = {} @@ -103,7 +102,7 @@ def translate(text) -> Awaitable[Optional[str]]: if future is not None: return future # 否则创建一个翻译任务 - future = _main_event_loop.create_future() + future = asyncio.get_event_loop().create_future() # 查缓存 res = _translate_cache.get(key, None)