From 9d8d6deda732e988fb93676369c58587ea69b6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 19 Feb 2023 20:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Update=20Networking=20backend=20?= =?UTF-8?q?tweaks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 22 +++++++++++++++------- core/bot.py | 12 ++++++++---- core/config.py | 6 +++++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 8110351..d59cbc8 100644 --- a/.env.example +++ b/.env.example @@ -50,13 +50,21 @@ LOGGER_LOCALS_MAX_STRING=80 # 可被 logger 打印的 record 的名称(默认包含了 LOGGER_NAME ) LOGGER_FILTERED_NAMES=["uvicorn","ErrorPush","ApiHelper"] - -# 超时配置 可选配置项 -# TIMEOUT = 10 -# READ_TIMEOUT = 2 -# WRITE_TIMEOUT = 10 -# CONNECT_TIMEOUT = 10 -# POOL_TIMEOUT = 10 +# Request 超时配置 可选配置项 +# READ_TIMEOUT=7 +# 指定等待服务器响应的最长时间 +# WRITE_TIMEOUT=10 +# 指定等待写入操作完成的最长时间(就网络套接字而言,即请求或上传一个文件) +# CONNECT_TIMEOUT=10 +# 指定等待连接到服务器的最长时间 +# POOL_TIMEOUT=10 +# Bot.Update 超时配置 +# TIMEOUT=10 +# 服务器出现故障时Updater的引导阶段重试时间 +# UPDATE_READ_TIMEOUT=42 +# UPDATE_WRITE_TIMEOUT=10 +# UPDATE_CONNECT_TIMEOUT=10 +# UPDATE_POOL_TIMEOUT=10 # genshin.py 缓存配置 可选配置项 # GENSHIN_TTL = 3600 diff --git a/core/bot.py b/core/bot.py index 412b4dc..3321f6b 100644 --- a/core/bot.py +++ b/core/bot.py @@ -259,6 +259,14 @@ class Bot: logger.info("正在初始化BOT") self.app = ( TgApplication.builder() + .read_timeout(self.config.read_timeout) + .write_timeout(self.config.write_timeout) + .connect_timeout(self.config.connect_timeout) + .pool_timeout(self.config.pool_timeout) + .get_updates_read_timeout(self.config.update_read_timeout) + .get_updates_write_timeout(self.config.update_write_timeout) + .get_updates_connect_timeout(self.config.update_connect_timeout) + .get_updates_pool_timeout(self.config.update_pool_timeout) .rate_limiter(AIORateLimiter()) .defaults(Defaults(tzinfo=pytz.timezone("Asia/Shanghai"))) .token(self._config.bot_token) @@ -271,10 +279,6 @@ class Bot: self.app.run_polling( close_loop=False, timeout=self.config.timeout, - read_timeout=self.config.read_timeout, - write_timeout=self.config.write_timeout, - connect_timeout=self.config.connect_timeout, - pool_timeout=self.config.pool_timeout, allowed_updates=Update.ALL_TYPES, ) break diff --git a/core/config.py b/core/config.py index 8c62657..f6e0cbe 100644 --- a/core/config.py +++ b/core/config.py @@ -122,10 +122,14 @@ class BotConfig(Settings): join_groups: Optional[JoinGroups] = JoinGroups.NO_ALLOW timeout: int = 10 - read_timeout: float = 2 + read_timeout: Optional[float] = None write_timeout: Optional[float] = None connect_timeout: Optional[float] = None pool_timeout: Optional[float] = None + update_read_timeout: Optional[float] = None + update_write_timeout: Optional[float] = None + update_connect_timeout: Optional[float] = None + update_pool_timeout: Optional[float] = None genshin_ttl: Optional[int] = None