From 6e8b04ef4fc31d652fb07962e02f20b446d8e68a Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 21 Mar 2020 15:23:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=A0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=94=99=E8=AF=AF=E3=80=81=E6=8F=90=E9=AB=98?= =?UTF-8?q?=E5=AE=B9=E9=94=99=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/chat.py | 41 ++++++++++++++++++++++++++--------------- models/avatar.py | 5 ++--- models/database.py | 3 ++- models/translate.py | 5 ++--- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/api/chat.py b/api/chat.py index d66639c..692bdc5 100644 --- a/api/chat.py +++ b/api/chat.py @@ -113,7 +113,7 @@ class Room(blivedm.BLiveClient): try: client.write_message(body) except tornado.websocket.WebSocketClosedError: - pass + room_manager.del_client(self.room_id, client) def send_message_if(self, can_send_func: Callable[['ChatHandler'], bool], cmd, data): body = json.dumps({'cmd': cmd, 'data': data}) @@ -121,7 +121,7 @@ class Room(blivedm.BLiveClient): try: client.write_message(body) except tornado.websocket.WebSocketClosedError: - pass + room_manager.del_client(self.room_id, client) async def _on_receive_danmaku(self, danmaku: blivedm.DanmakuMessage): asyncio.ensure_future(self.__on_receive_danmaku(danmaku)) @@ -275,7 +275,10 @@ class RoomManager: if not await self._add_room(room_id): client.close() return - room = self._rooms[room_id] + room = self._rooms.get(room_id, None) + if room is None: + return + room.clients.append(client) logger.info('%d clients in room %s', len(room.clients), room_id) if client.auto_translate: @@ -285,13 +288,20 @@ class RoomManager: await client.send_test_message() def del_client(self, room_id, client: 'ChatHandler'): - if room_id not in self._rooms: + room = self._rooms.get(room_id, None) + if room is None: return - room = self._rooms[room_id] - room.clients.remove(client) - logger.info('%d clients in room %s', len(room.clients), room_id) - if client.auto_translate: - room.auto_translate_count -= 1 + + try: + room.clients.remove(client) + except ValueError: + # _add_room未完成,没有执行到room.clients.append + pass + else: + logger.info('%d clients in room %s', len(room.clients), room_id) + if client.auto_translate: + room.auto_translate_count = max(0, room.auto_translate_count - 1) + if not room.clients: self._del_room(room_id) @@ -299,24 +309,25 @@ class RoomManager: if room_id in self._rooms: return True logger.info('Creating room %d', room_id) - room = Room(room_id) - self._rooms[room_id] = room + self._rooms[room_id] = room = Room(room_id) if await room.init_room(): room.start() + logger.info('%d rooms', len(self._rooms)) return True else: self._del_room(room_id) return False def _del_room(self, room_id): - if room_id not in self._rooms: + room = self._rooms.get(room_id, None) + if room is None: return logger.info('Removing room %d', room_id) - room = self._rooms[room_id] for client in room.clients: client.close() room.stop_and_close() - del self._rooms[room_id] + self._rooms.pop(room_id, None) + logger.info('%d rooms', len(self._rooms)) # noinspection PyAbstractClass @@ -460,4 +471,4 @@ class ChatHandler(tornado.websocket.WebSocketHandler): try: self.write_message(body) except tornado.websocket.WebSocketClosedError: - pass + self.on_close() diff --git a/models/avatar.py b/models/avatar.py index 95ae866..6ca23cb 100644 --- a/models/avatar.py +++ b/models/avatar.py @@ -174,9 +174,8 @@ def update_avatar_cache(user_id, avatar_url): def _update_avatar_cache_in_memory(user_id, avatar_url): _avatar_url_cache[user_id] = avatar_url - if len(_avatar_url_cache) > 50000: - for _, key in zip(range(100), _avatar_url_cache): - del _avatar_url_cache[key] + while len(_avatar_url_cache) > 50000: + _avatar_url_cache.pop(next(iter(_avatar_url_cache)), None) def _update_avatar_cache_in_database(user_id, avatar_url): diff --git a/models/database.py b/models/database.py index ca24f5d..884d765 100644 --- a/models/database.py +++ b/models/database.py @@ -16,7 +16,8 @@ DbSession: Optional[Type[sqlalchemy.orm.Session]] = None def init(debug): cfg = config.get_config() global engine, DbSession - engine = sqlalchemy.create_engine(cfg.database_url, echo=debug) + # engine = sqlalchemy.create_engine(cfg.database_url, echo=debug) + engine = sqlalchemy.create_engine(cfg.database_url) DbSession = sqlalchemy.orm.sessionmaker(bind=engine) OrmBase.metadata.create_all(engine) diff --git a/models/translate.py b/models/translate.py index 612653c..25ccd26 100644 --- a/models/translate.py +++ b/models/translate.py @@ -103,9 +103,8 @@ def _on_translate_done(key, future): if res is None: return _translate_cache[key] = res - if len(_translate_cache) > 50000: - for _, key in zip(range(100), _translate_cache): - del _translate_cache[key] + while len(_translate_cache) > 50000: + _translate_cache.pop(next(iter(_translate_cache)), None) class TranslateProvider: