修复删缓存时的错误、提高容错性

This commit is contained in:
John Smith 2020-03-21 15:23:31 +08:00
parent b73a4e3748
commit 6e8b04ef4f
4 changed files with 32 additions and 22 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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)

View File

@ -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: