From 8fa63f1d75a3ffd8268eaaeef80afe553175f1c6 Mon Sep 17 00:00:00 2001 From: John Smith Date: Mon, 31 Jul 2023 23:29:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=80=E4=BA=9B=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/config.example.ini | 16 ++++++++-------- main.py | 1 - models/database.py | 11 +++++++++-- services/avatar.py | 17 ++++++++++------- services/translate.py | 4 ++-- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/data/config.example.ini b/data/config.example.ini index f4c8af8..767fa6e 100644 --- a/data/config.example.ini +++ b/data/config.example.ini @@ -2,8 +2,8 @@ # If you want to modify the configuration, copy this file and rename it to "config.ini" and edit [app] -# 数据库配置,见https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls -# See https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls +# 数据库配置,见 https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls +# See https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls database_url = sqlite:///data/database.db # 如果使用了nginx之类的反向代理服务器,设置为true @@ -24,17 +24,17 @@ open_browser_at_startup = true enable_upload_file = true -# 获取头像间隔时间(秒)。如果小于3秒有很大概率被服务器拉黑 +# 获取头像间隔时间(秒)。如果小于5秒有很大概率被服务器拉黑 # Interval between fetching avatars (seconds). At least 3 seconds is recommended -fetch_avatar_interval = 3.5 +fetch_avatar_interval = 5.5 # 获取头像最大队列长度,注意最长等待时间等于 最大队列长度 * 请求间隔时间 # Maximum queue length for fetching avatar -fetch_avatar_max_queue_size = 2 +fetch_avatar_max_queue_size = 1 -# 头像缓存数量 -# Number of avatar caches -avatar_cache_size = 50000 +# 内存中头像缓存数量 +# Number of avatar caches in memory +avatar_cache_size = 10000 # 允许自动翻译到日语 diff --git a/main.py b/main.py index 51a2e81..31937aa 100644 --- a/main.py +++ b/main.py @@ -67,7 +67,6 @@ def init_logging(debug): ) logging.basicConfig( format='{asctime} {levelname} [{name}]: {message}', - datefmt='%Y-%m-%d %H:%M:%S', style='{', level=logging.INFO if not debug else logging.DEBUG, handlers=[stream_handler, file_handler] diff --git a/models/database.py b/models/database.py index 758a8a8..458d81b 100644 --- a/models/database.py +++ b/models/database.py @@ -15,8 +15,15 @@ class OrmBase(sqlalchemy.orm.DeclarativeBase): def init(_debug): cfg = config.get_config() global _engine - # engine = sqlalchemy.create_engine(cfg.database_url, echo=debug) - _engine = sqlalchemy.create_engine(cfg.database_url) + _engine = sqlalchemy.create_engine( + cfg.database_url, + pool_size=5, # 保持的连接数 + max_overflow=5, # 临时的额外连接数 + pool_timeout=3, # 连接数达到最大时获取新连接的超时时间 + # pool_pre_ping=True, # 获取连接时先检测是否可用 + pool_recycle=60 * 60, # 回收超过1小时的连接,防止数据库服务器主动断开不活跃的连接 + # echo=debug, # 输出SQL语句 + ) OrmBase.metadata.create_all(_engine) diff --git a/services/avatar.py b/services/avatar.py index c071e7a..f7bb907 100644 --- a/services/avatar.py +++ b/services/avatar.py @@ -191,7 +191,7 @@ async def _get_avatar_url_from_web_consumer(): if _last_fetch_banned_time is not None: cur_time = datetime.datetime.now() if (cur_time - _last_fetch_banned_time).total_seconds() < 3 * 60 + 3: - # 3分钟以内被ban,解封大约要15分钟 + # 3分钟以内被ban future.set_result(None) continue else: @@ -216,7 +216,7 @@ async def _get_avatar_url_from_web_wrapper(user_id, future): async def _do_get_avatar_url_from_web(user_id) -> Optional[str]: - global _wbi_key, _refresh_wbi_key_future + global _wbi_key, _refresh_wbi_key_future, _last_fetch_banned_time if _wbi_key == '': if _refresh_wbi_key_future is None: _refresh_wbi_key_future = asyncio.create_task(_refresh_wbi_key()) @@ -236,17 +236,20 @@ async def _do_get_avatar_url_from_web(user_id) -> Optional[str]: logger.warning('Failed to fetch avatar: status=%d %s uid=%d', r.status, r.reason, user_id) if r.status == 412: # 被B站ban了 - global _last_fetch_banned_time _last_fetch_banned_time = datetime.datetime.now() return None data = await r.json() except (aiohttp.ClientConnectionError, asyncio.TimeoutError): return None - if data['code'] != 0: - # 这里虽然失败但不会被ban一段时间 - logger.info('Failed to fetch avatar: code=%d %s uid=%d', data['code'], data['message'], user_id) - if data['code'] == -403: + code = data['code'] + if code != 0: + logger.info('Failed to fetch avatar: code=%d %s uid=%d', code, data['message'], user_id) + if code == -401: + # 被B站ban了 + _last_fetch_banned_time = datetime.datetime.now() + elif code == -403: + # 签名错误 _wbi_key = '' return None diff --git a/services/translate.py b/services/translate.py index bd514d2..58a933a 100644 --- a/services/translate.py +++ b/services/translate.py @@ -403,8 +403,8 @@ class TencentTranslateFree(FlowControlTranslateProvider): def _on_fail(self): self._fail_count += 1 - # 为了可靠性,连续失败10次时冷却直到下次重新init - if self._fail_count >= 10: + # 为了可靠性,连续失败5次时冷却直到下次重新init + if self._fail_count >= 5: self._cool_down() def _cool_down(self):