From 33d3303ff12d6cc505f75dbf83c0192f3558e8eb Mon Sep 17 00:00:00 2001 From: John Smith Date: Mon, 27 Nov 2023 21:47:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8A=A5=E9=94=99=E3=80=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=99=90=E6=B5=81=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/open_live.py | 17 ++++++++++++++--- services/open_live.py | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/open_live.py b/api/open_live.py index cbcf702..76e9044 100644 --- a/api/open_live.py +++ b/api/open_live.py @@ -34,7 +34,7 @@ GAME_HEARTBEAT_COMMON_SERVER_URL = COMMON_SERVER_BASE_URL + '/api/internal/open_ _error_auth_code_cache = cachetools.LRUCache(256) # 用于限制请求开放平台的频率 -_open_live_rate_limiter = utils.rate_limit.TokenBucket(5, 9) +_open_live_rate_limiter = utils.rate_limit.TokenBucket(8, 8) class TransportError(Exception): @@ -147,6 +147,8 @@ def _validate_auth_code(auth_code): class _OpenLiveHandlerBase(api.base.ApiHandler): + _LOG_REQUEST = True + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.res: Optional[dict] = None @@ -160,7 +162,8 @@ class _OpenLiveHandlerBase(api.base.ApiHandler): cfg = config.get_config() self.json_args['app_id'] = cfg.open_live_app_id - logger.info('client=%s requesting open live, cls=%s', self.request.remote_ip, type(self).__name__) + if self._LOG_REQUEST: + logger.info('client=%s requesting open live, cls=%s', self.request.remote_ip, type(self).__name__) class _PublicHandlerBase(_OpenLiveHandlerBase): @@ -211,10 +214,14 @@ class _StartGameMixin(_OpenLiveHandlerBase): room_id = self.res['data']['anchor_info']['room_id'] except (TypeError, KeyError): room_id = None + try: + game_id = self.res['data']['game_info']['game_id'] + except (TypeError, KeyError): + game_id = None code = self.res['code'] logger.info( 'client=%s room_id=%s start game res: %s %s, game_id=%s', self.request.remote_ip, room_id, - code, self.res['message'], self.res['data']['game_info']['game_id'] + code, self.res['message'], game_id ) if code == 7007: # 身份码错误 @@ -243,6 +250,8 @@ class EndGamePrivateHandler(_PrivateHandlerBase): class GameHeartbeatPublicHandler(_OpenLiveHandlerBase): + _LOG_REQUEST = False + async def post(self): game_id = self.json_args.get('game_id', None) if not isinstance(game_id, str) or game_id == '': @@ -274,6 +283,8 @@ async def send_game_heartbeat_by_service_or_common_server(game_id): class GameHeartbeatPrivateHandler(_OpenLiveHandlerBase): + _LOG_REQUEST = False + async def post(self): cfg = config.get_config() if not cfg.is_open_live_configured: diff --git a/services/open_live.py b/services/open_live.py index 834805d..314daa1 100644 --- a/services/open_live.py +++ b/services/open_live.py @@ -42,7 +42,7 @@ async def send_game_heartbeat(game_id) -> dict: _game_id_heart_task_map[game_id] = task # 限制一次发送的数量,数量太多了就立即发送 - if len(_game_id_heart_task_map) >= 95: + if len(_game_id_heart_task_map) >= 200: await _flush_game_heartbeat_tasks() return await task.future @@ -56,7 +56,7 @@ async def _game_heartbeat_consumer(): cost_time = (datetime.datetime.now() - start_time).total_seconds() # 如果等待时间太短,请求频率会太高;如果等待时间太长,前端请求、项目心跳会超时 - await asyncio.sleep(5 - cost_time) + await asyncio.sleep(4 - cost_time) except Exception: # noqa logger.exception('_heartbeat_consumer error:')